I want to do something after WordPress sent an email. For example, after sending "Reset Password" email using wp_mail() function.

1 Answer 1


Using the PHPMailer class with an action callback:

I did some digging into the PHPMailer class and found that it supports a custom action.

Here's how the callback is activated with the doCallback() method in the class.

There's also a PHPMailer test on GitHub using this feature via the callbackAction() callback.

We can set it up in WordPress with:

$phpmailer->action_function = 'wpse_mail_action';

where wpse_mail_action() is the action callback.

Here's an example how we can apply this:

 * Custom PHPMailer action callback
function wpse_mail_action( $is_sent, $to, $cc, $bcc, $subject, $body, $from )
    do_action( 'wpse_mail_action', $is_sent, $to, $cc, $bcc, $subject, $body, $from );
    return $is_sent; // don't actually need this return!

 * Setup a custom PHPMailer action callback
add_action( 'phpmailer_init', function( $phpmailer )
    $phpmailer->action_function = 'wpse_mail_action';
} );

Now we have access to the wpse_mail_action hook.

We could then add our own mail logger and check if the mails were successfully sent or not.


Here's an (untested) example how we could do something after "Password Reset" posts are sent:

 * Do something after the "Password Reset" post has been successfully sent:
add_action( 'wpse_mail_action', function( $is_sent, $to, $cc, $bcc, $subject, $body, $from )
    if( $is_sent && false !== stripos( $subject, 'Password Reset' ) )
        // do stuff

}, 10, 7 );

where we could add some further restrictions and wrap into other actions if neccessary, like the retrieve_password hook.

  • Thank you at all. I've just made the plugin and its now accessible from wordpress plugin directory: wordpress.org/plugins/telegram-for-wp Jun 25, 2015 at 22:32
  • 1
    You're welcome and congratulation with the new plugin @AmirMousavi
    – birgire
    Jun 25, 2015 at 23:46
  • There is a conflict with Contact Form 7 plugin. It can't receive the result of sending. Its Ajax loader has been spinning forever; even when email sent successfully. Jul 5, 2015 at 11:06
  • 2
    I tested this method with the CF7 and it worked as expected. If the ajaxloader is spinning forever, it could indicate unrelated javascript/PHP/Server errors and therefore wp_mail() might never be called within CF7. Note that 3rd party plugins are off topic here on WPSE. @AmirMousavi
    – birgire
    Jul 5, 2015 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.