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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With