diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 5edd0c760cbb2..6141669101494 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -294,10 +294,21 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() } else { if ( ! is_array( $headers ) ) { /* - * Explode the headers out, so this function can take - * both string headers and an array of headers. + * Process headers and handle folding in a single pass. + * Lines starting with whitespace (space or tab) are continuations of the previous line. */ - $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) ); + $tempheaders = array(); + $normalized_headers = str_replace( "\r\n", "\n", $headers ); + + foreach ( explode( "\n", $normalized_headers ) as $header_line ) { + if ( ! empty( $tempheaders ) && isset( $header_line[0] ) && ( ' ' === $header_line[0] || "\t" === $header_line[0] ) ) { + // Continuation line - append to previous header. + $last_index = count( $tempheaders ) - 1; + $tempheaders[ $last_index ] .= "\n" . $header_line; + } else { + $tempheaders[] = $header_line; + } + } } else { $tempheaders = $headers; } diff --git a/tests/phpunit/tests/pluggable/wpMail.php b/tests/phpunit/tests/pluggable/wpMail.php index 85e3b46f61a5b..4ed0aeba8fd5a 100644 --- a/tests/phpunit/tests/pluggable/wpMail.php +++ b/tests/phpunit/tests/pluggable/wpMail.php @@ -625,4 +625,24 @@ public function test_wp_mail_string_embeds() { $this->assertStringContainsString( 'cid:' . $key, $mailer->get_sent()->body, 'The cid ' . $key . ' is not referenced in the mail body.' ); } } + + /** + * Tests that wp_mail() correctly handles multiline From headers by unfolding them. + * + * @ticket 28473 + */ + public function test_wp_mail_multiline_header() { + $headers = 'From: =?UTF-8?B?0YLQtdGB0YIg0YLQtdGB0YIg0YLQtdGB0YIg0YLQtdGB0YIg0YLQtdGB0YIg?='; + $headers .= "\n =?UTF-8?B?0YLQtdGB0YIg0YLQtdGB0YI=?= "; + wp_mail( 'test@test.com', 'subject', 'message', $headers ); + + $mailer = tests_retrieve_phpmailer_instance(); + // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $this->assertSame( 'test@example.com', $mailer->From ); + $this->assertSame( + '=?UTF-8?B?0YLQtdGB0YIg0YLQtdGB0YIg0YLQtdGB0YIg0YLQtdGB0YIg0YLQtdGB0YIg?= =?UTF-8?B?0YLQtdGB0YIg0YLQtdGB0YI=?=', + $mailer->FromName + ); + // phpcs:enable + } }