Skip to content

Commit

Permalink
Mail: Add wp_mail_succeeded hook to wp_mail.
Browse files Browse the repository at this point in the history
Adds a new `wp_mail_succeeded` action in `wp_mail` after the mail is sent.  Also, adds a disclaimer to the hook's docblock, clarifying that the hook's firing doesn't necessarily mean the recipient received the mail, only that the mail was processed without any errors.

Props birgire, donmhico, johnbillion.
Fixes #53826.
Built from https://develop.svn.wordpress.org/trunk@52083


git-svn-id: http://core.svn.wordpress.org/trunk@51675 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
dream-encode committed Nov 9, 2021
1 parent 2c874cf commit d6de0e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 20 additions & 5 deletions wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,28 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
*/
do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );

$mail_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );

// Send!
try {
return $phpmailer->send();
} catch ( PHPMailer\PHPMailer\Exception $e ) {
$send = $phpmailer->send();

$mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
$mail_error_data['phpmailer_exception_code'] = $e->getCode();
/**
* Fires after PHPMailer has successfully sent a mail.
*
* The firing of this action does not necessarily mean that the recipient received the
* email successfully. It only means that the `send` method above was able to
* process the request without any errors.
*
* @since 5.9.0
*
* @param array $mail_data An array containing the mail recipient, subject, message, headers, and attachments.
*/
do_action( 'wp_mail_succeeded', $mail_data );

return $send;
} catch ( PHPMailer\PHPMailer\Exception $e ) {
$mail_data['phpmailer_exception_code'] = $e->getCode();

/**
* Fires after a PHPMailer\PHPMailer\Exception is caught.
Expand All @@ -553,7 +568,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
* @param WP_Error $error A WP_Error object with the PHPMailer\PHPMailer\Exception message, and an array
* containing the mail recipient, subject, message, headers, and attachments.
*/
do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_data ) );

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-alpha-52082';
$wp_version = '5.9-alpha-52083';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit d6de0e4

Please sign in to comment.