From 16b7125de0e04d94455f29815ff29fc2fead01de Mon Sep 17 00:00:00 2001 From: David Baumwald Date: Tue, 9 Nov 2021 22:25:40 +0000 Subject: [PATCH] Mail: Add `wp_mail_succeeded` hook to `wp_mail`. 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. git-svn-id: https://develop.svn.wordpress.org/trunk@52083 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/pluggable.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index e30fad1808..17a23e3cd8 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -537,13 +537,28 @@ if ( ! function_exists( 'wp_mail' ) ) : */ 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. @@ -553,7 +568,7 @@ if ( ! function_exists( 'wp_mail' ) ) : * @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; }