diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 2ec21126e0..5ab3eef749 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -307,7 +307,7 @@ if ( ! function_exists( 'wp_mail' ) ) : if ( false !== $bracket_pos ) { // Text before the bracketed email is the "From" name. if ( $bracket_pos > 0 ) { - $from_name = substr( $content, 0, $bracket_pos - 1 ); + $from_name = substr( $content, 0, $bracket_pos ); $from_name = str_replace( '"', '', $from_name ); $from_name = trim( $from_name ); } diff --git a/tests/phpunit/tests/mail.php b/tests/phpunit/tests/mail.php index 718a585673..c130658e3f 100644 --- a/tests/phpunit/tests/mail.php +++ b/tests/phpunit/tests/mail.php @@ -193,6 +193,28 @@ class Tests_Mail extends WP_UnitTestCase { $this->assertStringContainsString( $expected, $mailer->get_sent()->header ); } + /** + * @ticket 19847 + */ + public function test_wp_mail_with_from_header_missing_space() { + $to = 'address@tld.com'; + $subject = 'Testing'; + $message = 'Test Message'; + $from = 'bar@example.com'; + $from_name = 'Foo'; + $headers = "From: {$from_name}<{$from}>"; + $corrected = "From: {$from_name} <{$from}>"; + + wp_mail( $to, $subject, $message, $headers ); + + $mailer = tests_retrieve_phpmailer_instance(); + // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $this->assertSame( $from, $mailer->From ); + $this->assertSame( $from_name, $mailer->FromName ); + // phpcs:enable + $this->assertStringContainsString( $corrected, $mailer->get_sent()->header ); + } + /** * @ticket 30266 */