如何检查在 CakePHP 中发送电子邮件是否成功?
我可以毫无问题地发送电子邮件,但我希望能够在发送失败时处理错误。我怎样才能做到这一点?
这是我到目前为止所做的:
$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);
最佳答案
您可以使用 try catch block 来检查邮件是否已成功移交给 MTA,您无法真正检测或检查邮件是否已成功传递给收件人。那是另一种情况。
try {
if ( $this->Email->send() ) {
// Success
} else {
// Failure, without any exceptions
}
} catch ( Exception $e ) {
// Failure, with exception
}
或者,如果您在邮件 header 中设置了回复,那么您可以检查是否有任何退回的邮件,这会让您确定邮件未送达。
关于php - 检查在 CakePHP 中发送电子邮件是否成功/不成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15931984/