How to stop the wp_mail function?

I’m using wp_mail filter function.

add_filter('wp_mail','check_the_mail_content');

If my condition satisfied then the mail should go, otherwise I need to stop, my function is

add_filter('wp_mail','check_the_mail_content');
function check_the_mail_content($query){
    if(mycondition){
         //mail should go.
    }
    else{
        //stop the mail.
   }
}

6 Answers
6

Filter 'phpmailer_init', not 'wp_mail'. To prevent sending the mail out reset the PHPMailer object.

Prototype (not tested):

add_action( 'phpmailer_init', 'wpse_53612_conditional_mail_stop' );

function wpse_53612_conditional_mail_stop( $phpmailer )
{
    ! my_condition() and $phpmailer = new stdClass;
}

Leave a Comment