Skip to content

Commit

Permalink
Mail: PHPMailer swap to use is_email for the default validator.
Browse files Browse the repository at this point in the history
Prior to the PHPMailer update in 5.5, old version of the PHPMailer was setting the validator to 'auto' resulting in a sophisticated logic for determining what email address validation should be used. But the new version defaults to 'php', possibly leading to rejection of email addresses which were fine prior to the upgrade. Let's use the WordPress core function `is_email()` so that it can be fully pluggable.

Fixes #50720.
Props david.binda, ayeshrajans, Synchro, SergeyBiryukov, whyisjake.


git-svn-id: https://develop.svn.wordpress.org/trunk@48645 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
whyisjake committed Jul 27, 2020
1 parent e03be98 commit 8c9b33c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/wp-includes/pluggable.php
Expand Up @@ -216,6 +216,10 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
$phpmailer = new PHPMailer\PHPMailer\PHPMailer( true );

$phpmailer::$validator = static function ( $email ) {
return (bool) is_email( $email );
};
}

// Headers.
Expand Down
7 changes: 6 additions & 1 deletion tests/phpunit/includes/mock-mailer.php
Expand Up @@ -96,7 +96,12 @@ function tests_retrieve_phpmailer_instance() {
function reset_phpmailer_instance() {
$mailer = tests_retrieve_phpmailer_instance();
if ( $mailer ) {
$GLOBALS['phpmailer'] = new MockPHPMailer( true );
$mailer = new MockPHPMailer( true );
$mailer::$validator = static function ( $email ) {
return (bool) is_email( $email );
};

$GLOBALS['phpmailer'] = $mailer;
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/tests/mail.php
Expand Up @@ -407,4 +407,12 @@ public function test_phpmailer_exception_thrown() {
$this->assertEquals( 'wp_mail_failed', $call_args[0]->get_error_code() );
$this->assertEquals( $expected_error_data, $call_args[0]->get_error_data() );
}

/**
* @ticket 50720
*/
function test_phpmailer_validator() {
$phpmailer = $GLOBALS['phpmailer'];
$this->assertTrue( $phpmailer->validateAddress( 'foo@192.168.1.1' ), 'Assert PHPMailer accepts IP address email addresses' );
}
}

0 comments on commit 8c9b33c

Please sign in to comment.