How to disable all WordPress emails modularly and programatically?

I want to modularly disable emails sent out by WordPress so I can replace them with my own custom emails when necessary.

I tried googling for a list of filter hooks, but couldn’t find anything as comprehensive as I would like. I found these two so far:

/**
 * Disable default WordPress emails.
 */
add_filter( 'send_password_change_email', '__return_false' );
add_filter( 'send_email_change_email', '__return_false' );

Is there a resource or a list of all filter hooks that WordPress uses to send emails?

Update

Another option, as mentioned in kirillrocks’s answer is to completely disable the wp_mail() function. However, this is undesirable for my use case as it disables ALL emails. For my use case, I would like to disable all mails individually, so I can later ‘overwrite’ them with my own emails, but still use the wp_mail() function to send them.

2 Answers
2

Option 1: Remove the ‘to’ argument from wp_mail function in WordPress, it will keep your system running without sending any default WordPress emails.

add_filter('wp_mail','disabling_emails', 10,1);
function disabling_emails( $args ){
    unset ( $args['to'] );
    return $args;
}

The wp_mail is a wrapper for the phpmailer class and it will not send any emails if there is no recipient.

Option 2: Hook to the phpmailer class directly and ClearAllRecipients from there

function my_action( $phpmailer ) {
    $phpmailer->ClearAllRecipients();
}
add_action( 'phpmailer_init', 'my_action' );

Option 3: Keep using wp_mail for your own needs but disable for everything else.

add_filter('wp_mail','disabling_emails', 10,1);
function disabling_emails( $args ){
    if ( ! $_GET['allow_wp_mail'] ) {
        unset ( $args['to'] );
    }
    return $args;
}

and when you are calling wp_mail use it like this:

$_GET['allow_wp_mail'] = true;
wp_mail( $to, $subject, $message, $headers );
unset ( $_GET['allow_wp_mail'] ); // optional

https://react2wp.com/wordpress-disable-email-notifications-pragmatically-in-code-fix/

You are welcome!

Leave a Comment