Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.
   }
}
share|improve this question

4 Answers 4

up vote 2 down vote accepted

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;
}
share|improve this answer
    
The wp_mail() function is built to catch exceptions of a particular type ... are you sure calling $phpmailer->Send() on an object of type stdClass won't explode? –  EAMann May 29 '12 at 15:29
1  
@EAMann The try {} catch {} block should prevent that. –  toscho May 29 '12 at 15:33
    
Hi toscho, Thanks for your answer it's helpful. –  Chella Durai May 29 '12 at 17:04
    
@toscho Good to know. I'm used to catch(type){} only catching exceptions of that type ... glad to know PHP is a bit more flexible than that. –  EAMann May 29 '12 at 17:53

Unfortunately, that particular filter isn't verified after it's used. Here is the use of that filter in core:

extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );

So all that filter does is populate the $to, $subject, $message, $headers, and $attechments varaibles. It's not an action hook, so while you probably could throw some kind of termination operation in there, you really shouldn't. Ideally, you'd be able to return false from your filtering function to kill operation, but the function isn't set up that way.

Instead, I recommend hooking to the phpmailer_init action. It's the last action in the wp_mail() function and it passes a reference to the actual $phpmailer object that does the mailing.

This untested function should prevent mail from sending:

class fakemailer {
    public function Send() {
        throw new phpmailerException( 'Cancelling mail' );
    }
}

if ( ! class_exists( 'phpmailerException' ) ) :
class phpmailerException extends Exception {
    public function errorMessage() {
        $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
        return $errorMsg;
    }
}

add_action( 'phpmailer_init', 'wpse_53612_fakemailer' );
function wpse_53612_fakemailer( $phpmailer ) {
    if ( ! /* condition */ ) 
        $phpmailer = new fakemailer();
}

This should replace the $phpmailer object with an instance of your fake mailer class. This fake class only contains a Send() method that immediately throws an exception of type phpmailerException. The wp_mail() function will catch this exception and return false by default.

Not the most performant solution in the world ... you should really be checking conditions before even calling wp_mail() (as suggested by @Zaidar), but if you must use a hook, this is one way to do it.

share|improve this answer
    
Hi EAMann, Thanks for your answer with clear explanations. It's works. Thanks a lot... –  Chella Durai May 29 '12 at 17:01

Rather than hooking on phpmailer_init, why not just set $query['to'] = ''; and return $query; inside the wp_mail hook?

share|improve this answer
if(mycondition){
    wp_mail(themail@youwant, $subject, $message)
}else{
    // NOTHING SPECIAL
}
share|improve this answer
    
Hi, Zaidar, Thanks for the answer. But the code have some problem, if condition is satisfied then the wp_mail filter called again and again since we called wp_mail again. –  Chella Durai May 29 '12 at 16:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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