diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index fcc92fb71c..ef731793ef 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -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; } diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index a5b3ed4908..29d109bb7a 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -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' ); diff --git a/tests/phpunit/tests/mail.php b/tests/phpunit/tests/mail.php index d3e7698f07..1522594b0d 100644 --- a/tests/phpunit/tests/mail.php +++ b/tests/phpunit/tests/mail.php @@ -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() ); + } }