Skip to content

Commit 1187abf

Browse files
committed
Mail: reset Encoding to 8-bit in wp_mail().
The PHPMailer library may automatically switch its encoding based on various internal factors. This commit fixes a bug where the `$phpmailer` global was unintentionally persisting its `Encoding` property from the first `wp_mail()` call to all subsequent calls. This includes unit tests to verify the fix is accurate, and a change to the mock-mailer helper that worked around this bug just-in-time when running the test suite. Props codebuddy, dilip2615, rishabhwp, sajjad67, sirlouen, stephenharris. Fixes #33972. git-svn-id: https://develop.svn.wordpress.org/trunk@61131 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fd41ccd commit 1187abf

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/wp-includes/pluggable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,15 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
384384
$phpmailer->Body = '';
385385
$phpmailer->AltBody = '';
386386

387+
/*
388+
* Reset encoding to 8-bit, as it may have been automatically downgraded
389+
* to 7-bit by PHPMailer (based on the body contents) in a previous call
390+
* to wp_mail().
391+
*
392+
* See https://core.trac.wordpress.org/ticket/33972
393+
*/
394+
$phpmailer->Encoding = PHPMailer\PHPMailer\PHPMailer::ENCODING_8BIT;
395+
387396
// Set "From" name and email.
388397

389398
// If we don't have a name from the input headers.

tests/phpunit/includes/mock-mailer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class MockPHPMailer extends WP_PHPMailer {
2020
public $mock_sent = array();
2121

2222
public function preSend() {
23-
$this->Encoding = '8bit';
2423
return parent::preSend();
2524
}
2625

tests/phpunit/tests/pluggable/wpMail.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,4 +651,22 @@ public function test_wp_mail_string_embeds() {
651651
$this->assertStringContainsString( 'cid:' . $key, $mailer->get_sent()->body, 'The cid ' . $key . ' is not referenced in the mail body.' );
652652
}
653653
}
654+
655+
/**
656+
* Test that the encoding of the email does not bleed between long and short emails.
657+
*
658+
* @ticket 33972
659+
*/
660+
public function test_wp_mail_encoding_does_not_bleed() {
661+
$content = str_repeat( 'A', 1000 );
662+
wp_mail( WP_TESTS_EMAIL, 'Looong line testing', $content );
663+
664+
$mailer = tests_retrieve_phpmailer_instance();
665+
$this->assertEquals( 'quoted-printable', $mailer->Encoding );
666+
667+
wp_mail( WP_TESTS_EMAIL, 'A follow up short email', 'Short email' );
668+
669+
$mailer = tests_retrieve_phpmailer_instance();
670+
$this->assertEquals( '7bit', $mailer->Encoding );
671+
}
654672
}

0 commit comments

Comments
 (0)