Skip to content

Mail: New Version for Multipart solution #9500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
} elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
$boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
$charset = '';
if ( preg_match( '~multipart/([a-z]+)~i', $content_type, $matches ) ) {
$content_type = 'multipart/' . strtolower( $matches[1] ) . '; boundary="' . $boundary . '"';
}
}

// Avoid setting an empty $content_type.
Expand Down Expand Up @@ -513,10 +516,6 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
}
}
}

if ( false !== stripos( $content_type, 'multipart' ) && ! empty( $boundary ) ) {
$phpmailer->addCustomHeader( sprintf( 'Content-Type: %s; boundary="%s"', $content_type, $boundary ) );
}
}

if ( ! empty( $attachments ) ) {
Expand Down
88 changes: 87 additions & 1 deletion tests/phpunit/tests/pluggable/wpMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function test_wp_mail_custom_boundaries() {

// We need some better assertions here but these catch the failure for now.
$this->assertSameIgnoreEOL( $body, $mailer->get_sent()->body );
$this->assertStringContainsString( 'boundary="----=_Part_4892_25692638.1192452070893"', iconv_mime_decode_headers( ( $mailer->get_sent()->header ) )['Content-Type'][0] );
$this->assertStringContainsString( 'boundary="----=_Part_4892_25692638.1192452070893"', $mailer->get_sent()->header );
$this->assertStringContainsString( 'charset=', $mailer->get_sent()->header );
}

Expand Down Expand Up @@ -554,4 +554,90 @@ public function test_wp_mail_resets_properties() {
$phpmailer = $GLOBALS['phpmailer'];
$this->assertNotSame( 'user1', $phpmailer->AltBody );
}

/**
* Test that wp_mail() can send a multipart/alternative email with plain text and html versions.
*
* @ticket 15448
*/
public function test_wp_mail_plain_and_html() {
$headers = 'Content-Type: multipart/alternative; boundary="TestBoundary"';
$to = '[email protected]';
$subject = 'Test email with plain text and html versions';
$message = <<<EOT
--TestBoundary
Content-Type: text/plain; charset=us-ascii

Here is some plain text.
--TestBoundary
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<html><head></head><body>Here is the HTML with UTF-8 γειά σου Κόσμε;-)<body></html>
--TestBoundary--
EOT;

wp_mail( $to, $subject, $message, $headers );
$mailer = tests_retrieve_phpmailer_instance();

preg_match( '/boundary="(.*)"/', $mailer->get_sent()->header, $matches );
$boundary = $matches[1];
$body = '--' . $boundary . "\n";
$body .= 'Content-Type: text/plain; charset=us-ascii' . "\n";
$body .= "\n";
$body .= 'Here is some plain text.' . "\n";
$body .= '--' . $boundary . "\n";
$body .= 'Content-Type: text/html; charset=UTF-8' . "\n";
$body .= 'Content-Transfer-Encoding: 8bit' . "\n";
$body .= "\n";
$body .= '<html><head></head><body>Here is the HTML with UTF-8 γειά σου Κόσμε;-)<body></html>' . "\n";
$body .= '--' . $boundary . '--' . "\n";

$this->assertSameIgnoreEOL( $body, $mailer->get_sent()->body, 'The body is not as expected.' );
$this->assertStringContainsString(
'Content-Type: multipart/alternative;',
$mailer->get_sent()->header,
'The multipart/alternative header is not present.'
);
}

/*
* 'phpmailer_init' action for test_wp_mail_plain_and_html_workaround().
*/
public function wp_mail_set_alt_body( $mailer ) {
$mailer->AltBody = strip_tags( $mailer->Body );
}

/**
* Check workarounds using phpmailer_init still work around.
*
* @ticket 15448
*/
public function test_wp_mail_plain_and_html_workaround() {
$to = '[email protected]';
$subject = 'Test email with plain text derived from html version';
$message = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><p>Hello World! γειά σου Κόσμε</p></body></html>';

add_action( 'phpmailer_init', array( $this, 'wp_mail_set_alt_body' ) );
wp_mail( $to, $subject, $message );
remove_action( 'phpmailer_init', array( $this, 'wp_mail_set_alt_body' ) );

$mailer = tests_retrieve_phpmailer_instance();

$this->assertStringContainsString(
'Content-Type: multipart/alternative;',
$mailer->get_sent()->header,
'The multipart/alternative header is not present.'
);
$this->assertStringContainsString(
'Content-Type: text/plain; charset=UTF-8',
$mailer->get_sent()->body,
'The text/plain Content-Type header is not present.'
);
$this->assertStringContainsString(
'Content-Type: text/html; charset=UTF-8',
$mailer->get_sent()->body,
'The text/html Content-Type header is not present.'
);
}
}
Loading