Improve handling of incomplete From and Content-Type headers in wp_mail()
.
When an incomplete header is provided (eg, 'From' with an email address but no name), ensure that the WP defaults are filled in properly. Props valendesigns. Fixes #30266. git-svn-id: https://develop.svn.wordpress.org/trunk@32070 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
5606e42df4
commit
bffb632183
@ -302,30 +302,37 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
|
|||||||
switch ( strtolower( $name ) ) {
|
switch ( strtolower( $name ) ) {
|
||||||
// Mainly for legacy -- process a From: header if it's there
|
// Mainly for legacy -- process a From: header if it's there
|
||||||
case 'from':
|
case 'from':
|
||||||
if ( strpos($content, '<' ) !== false ) {
|
$bracket_pos = strpos( $content, '<' );
|
||||||
// So... making my life hard again?
|
if ( $bracket_pos !== false ) {
|
||||||
$from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
|
// Text before the bracketed email is the "From" name.
|
||||||
|
if ( $bracket_pos > 0 ) {
|
||||||
|
$from_name = substr( $content, 0, $bracket_pos - 1 );
|
||||||
$from_name = str_replace( '"', '', $from_name );
|
$from_name = str_replace( '"', '', $from_name );
|
||||||
$from_name = trim( $from_name );
|
$from_name = trim( $from_name );
|
||||||
|
}
|
||||||
|
|
||||||
$from_email = substr( $content, strpos( $content, '<' ) + 1 );
|
$from_email = substr( $content, $bracket_pos + 1 );
|
||||||
$from_email = str_replace( '>', '', $from_email );
|
$from_email = str_replace( '>', '', $from_email );
|
||||||
$from_email = trim( $from_email );
|
$from_email = trim( $from_email );
|
||||||
} else {
|
|
||||||
|
// Avoid setting an empty $from_email.
|
||||||
|
} elseif ( '' !== trim( $content ) ) {
|
||||||
$from_email = trim( $content );
|
$from_email = trim( $content );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'content-type':
|
case 'content-type':
|
||||||
if ( strpos( $content, ';' ) !== false ) {
|
if ( strpos( $content, ';' ) !== false ) {
|
||||||
list( $type, $charset ) = explode( ';', $content );
|
list( $type, $charset_content ) = explode( ';', $content );
|
||||||
$content_type = trim( $type );
|
$content_type = trim( $type );
|
||||||
if ( false !== stripos( $charset, 'charset=' ) ) {
|
if ( false !== stripos( $charset_content, 'charset=' ) ) {
|
||||||
$charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
|
$charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
|
||||||
} elseif ( false !== stripos( $charset, 'boundary=' ) ) {
|
} elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
|
||||||
$boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
|
$boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
|
||||||
$charset = '';
|
$charset = '';
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
// Avoid setting an empty $content_type.
|
||||||
|
} elseif ( '' !== trim( $content ) ) {
|
||||||
$content_type = trim( $content );
|
$content_type = trim( $content );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -156,4 +156,94 @@ class Tests_Mail extends WP_UnitTestCase {
|
|||||||
// Fatal errors
|
// Fatal errors
|
||||||
$this->assertFalse( wp_mail( 'invalid.address', 'subject', 'body', '', array() ) );
|
$this->assertFalse( wp_mail( 'invalid.address', 'subject', 'body', '', array() ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 30266
|
||||||
|
*/
|
||||||
|
public function test_wp_mail_with_valid_from_header() {
|
||||||
|
$to = "address@tld.com";
|
||||||
|
$subject = "Testing";
|
||||||
|
$message = "Test Message";
|
||||||
|
$headers = "From: Foo <bar@example.com>";
|
||||||
|
$expected = "From: Foo <bar@example.com>";
|
||||||
|
|
||||||
|
wp_mail( $to, $subject, $message, $headers );
|
||||||
|
|
||||||
|
$this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 30266
|
||||||
|
*/
|
||||||
|
public function test_wp_mail_with_empty_from_header() {
|
||||||
|
$to = "address@tld.com";
|
||||||
|
$subject = "Testing";
|
||||||
|
$message = "Test Message";
|
||||||
|
$headers = "From: ";
|
||||||
|
$expected = "From: WordPress <wordpress@example.com>";
|
||||||
|
|
||||||
|
wp_mail( $to, $subject, $message, $headers );
|
||||||
|
|
||||||
|
$this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 30266
|
||||||
|
*/
|
||||||
|
public function test_wp_mail_with_empty_from_name_for_the_from_header() {
|
||||||
|
$to = "address@tld.com";
|
||||||
|
$subject = "Testing";
|
||||||
|
$message = "Test Message";
|
||||||
|
$headers = "From: <wordpress@example.com>";
|
||||||
|
$expected = "From: WordPress <wordpress@example.com>";
|
||||||
|
|
||||||
|
wp_mail( $to, $subject, $message, $headers );
|
||||||
|
|
||||||
|
$this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 30266
|
||||||
|
*/
|
||||||
|
public function test_wp_mail_with_valid_content_type_header() {
|
||||||
|
$to = "address@tld.com";
|
||||||
|
$subject = "Testing";
|
||||||
|
$message = "Test Message";
|
||||||
|
$headers = "Content-Type: text/html; charset=iso-8859-1";
|
||||||
|
$expected = "Content-Type: text/html; charset=iso-8859-1";
|
||||||
|
|
||||||
|
wp_mail( $to, $subject, $message, $headers );
|
||||||
|
|
||||||
|
$this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 30266
|
||||||
|
*/
|
||||||
|
public function test_wp_mail_with_empty_content_type_header() {
|
||||||
|
$to = "address@tld.com";
|
||||||
|
$subject = "Testing";
|
||||||
|
$message = "Test Message";
|
||||||
|
$headers = "Content-Type: ";
|
||||||
|
$expected = "Content-Type: text/plain; charset=UTF-8";
|
||||||
|
|
||||||
|
wp_mail( $to, $subject, $message, $headers );
|
||||||
|
|
||||||
|
$this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 30266
|
||||||
|
*/
|
||||||
|
public function test_wp_mail_with_empty_charset_for_the_content_type_header() {
|
||||||
|
$to = "address@tld.com";
|
||||||
|
$subject = "Testing";
|
||||||
|
$message = "Test Message";
|
||||||
|
$headers = "Content-Type: text/plain;";
|
||||||
|
$expected = "Content-Type: text/plain; charset=UTF-8";
|
||||||
|
|
||||||
|
wp_mail( $to, $subject, $message, $headers );
|
||||||
|
|
||||||
|
$this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user