How to disable all WordPress emails modularly and programatically?

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