Mail: Set a better error code when triggering `wp_mail_failed`.

This error code is now... wait for it... `wp_mail_failed`. Previously, this would have been the originating PHPMailer error code, which could be `0`, which would then fail (pass?) the `empty()` check in the `WP_Error` constructor, thereby rendering the error object fairly useless. The PHPMailer error code is now located within the `WP_Error` data.

props Kau-Boy, stephenharris.
fixes #35598.


git-svn-id: https://develop.svn.wordpress.org/trunk@39086 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Helen Hou-Sandi 2016-11-02 04:26:18 +00:00
parent 4ff5c83255
commit 5af14c3e07
3 changed files with 36 additions and 3 deletions

View File

@ -472,16 +472,17 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
} catch ( phpmailerException $e ) {
$mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
$mail_error_data['phpmailer_exception_code'] = $e->getCode();
/**
* Fires after a phpmailerException is caught.
*
* @since 4.4.0
*
* @param WP_Error $error A WP_Error object with the phpmailerException code, message, and an array
* @param WP_Error $error A WP_Error object with the phpmailerException message, and an array
* containing the mail recipient, subject, message, headers, and attachments.
*/
do_action( 'wp_mail_failed', new WP_Error( $e->getCode(), $e->getMessage(), $mail_error_data ) );
do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
return false;
}

View File

@ -51,7 +51,7 @@ $multisite = $multisite || ( defined( 'MULTISITE' ) && MULTISITE );
// Override the PHPMailer
require_once( dirname( __FILE__ ) . '/mock-mailer.php' );
$phpmailer = new MockPHPMailer();
$phpmailer = new MockPHPMailer( true );
if ( ! defined( 'WP_DEFAULT_THEME' ) ) {
define( 'WP_DEFAULT_THEME', 'default' );

View File

@ -360,4 +360,36 @@ class Tests_Mail extends WP_UnitTestCase {
$this->assertEquals( '', $mailer->Sender );
}
/**
* @ticket 35598
*/
public function test_phpmailer_exception_thrown() {
$to = 'an_invalid_address';
$subject = 'Testing';
$message = 'Test Message';
$ma = new MockAction();
add_action( 'wp_mail_failed', array( &$ma, 'action' ) );
wp_mail( $to, $subject, $message );
$this->assertEquals( 1, $ma->get_call_count() );
$expected_error_data = array(
'to' => array( 'an_invalid_address' ),
'subject' => 'Testing',
'message' => 'Test Message',
'headers' => array(),
'attachments' => array(),
'phpmailer_exception_code' => 2,
);
//Retrieve the arguments passed to the 'wp_mail_failed' hook callbacks
$all_args = $ma->get_args();
$call_args = array_pop( $all_args );
$this->assertEquals( 'wp_mail_failed', $call_args[0]->get_error_code() );
$this->assertEquals( $expected_error_data, $call_args[0]->get_error_data() );
}
}