Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if sending email was successful/unsuccessful in CakePHP?

How can I check if sending an email in CakePHP was successful or not?

I can send emails no problem but I want to be able to handle the error if it fails to send. How can I do this?

This is what I've done so far:

$email = new CakeEmail();
$email->from(array('email' => 'title'));
$email->to($to);
$email->subject('Account activation');     
$activate_url = 'http://' . env('SERVER_NAME') .'/cakephp/users/activate/'.$id.'/'.$token;
$message = "Thank you for signing up. Click on the activation link to activate your account \n";
return $email->send($message.$activate_url);
like image 719
Johnathan Au Avatar asked Apr 10 '13 16:04

Johnathan Au


1 Answers

You could use a try catch block to check whether the mail was successfully handed over to the MTA, you can't really detect or check if the mail was successfully delivered to the recipient. That is a different case.

try {
    if ( $this->Email->send() ) {
        // Success
    } else {
        // Failure, without any exceptions
    }
} catch ( Exception $e ) {
    // Failure, with exception
}

Alternately if you have set reply-to in your mail header, then you can check for any bounced mail which will let you say with certainty that a message hasn't been delivered.

like image 96
Sabari Avatar answered Sep 20 '22 07:09

Sabari