WordPress.org

Make WordPress Core

Changeset 39086


Ignore:
Timestamp:
11/02/16 04:26:18 (11 months ago)
Author:
helen
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/pluggable.php

    r39051 r39086  
    473473 
    474474        $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); 
     475        $mail_error_data['phpmailer_exception_code'] = $e->getCode(); 
    475476 
    476477        /** 
     
    479480         * @since 4.4.0 
    480481         * 
    481          * @param WP_Error $error A WP_Error object with the phpmailerException code, message, and an array 
     482         * @param WP_Error $error A WP_Error object with the phpmailerException message, and an array 
    482483         *                        containing the mail recipient, subject, message, headers, and attachments. 
    483484         */ 
    484         do_action( 'wp_mail_failed', new WP_Error( $e->getCode(), $e->getMessage(), $mail_error_data ) ); 
     485        do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) ); 
    485486 
    486487        return false; 
  • trunk/tests/phpunit/includes/bootstrap.php

    r38908 r39086  
    5252// Override the PHPMailer 
    5353require_once( dirname( __FILE__ ) . '/mock-mailer.php' ); 
    54 $phpmailer = new MockPHPMailer(); 
     54$phpmailer = new MockPHPMailer( true ); 
    5555 
    5656if ( ! defined( 'WP_DEFAULT_THEME' ) ) { 
  • trunk/tests/phpunit/tests/mail.php

    r38286 r39086  
    361361        $this->assertEquals( '', $mailer->Sender ); 
    362362    } 
     363 
     364    /** 
     365     * @ticket 35598 
     366     */ 
     367    public function test_phpmailer_exception_thrown() { 
     368        $to       = 'an_invalid_address'; 
     369        $subject  = 'Testing'; 
     370        $message  = 'Test Message'; 
     371 
     372        $ma = new MockAction(); 
     373        add_action( 'wp_mail_failed', array( &$ma, 'action' ) ); 
     374 
     375        wp_mail( $to, $subject, $message ); 
     376 
     377        $this->assertEquals( 1, $ma->get_call_count() ); 
     378 
     379        $expected_error_data = array( 
     380            'to'          => array( 'an_invalid_address' ), 
     381            'subject'     => 'Testing', 
     382            'message'     => 'Test Message', 
     383            'headers'     => array(), 
     384            'attachments' => array(), 
     385            'phpmailer_exception_code' => 2, 
     386        ); 
     387 
     388        //Retrieve the arguments passed to the 'wp_mail_failed' hook callbacks 
     389        $all_args = $ma->get_args(); 
     390        $call_args = array_pop( $all_args ); 
     391 
     392        $this->assertEquals( 'wp_mail_failed', $call_args[0]->get_error_code() ); 
     393        $this->assertEquals( $expected_error_data, $call_args[0]->get_error_data() ); 
     394    } 
    363395} 
Note: See TracChangeset for help on using the changeset viewer.