【问题标题】:Check if sending email was successful/unsuccessful in CakePHP?检查在 CakePHP 中发送电子邮件是否成功/失败?
【发布时间】:2013-04-02 15:45:26
【问题描述】:

如何检查在 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 了吗?
  • 充其量您可以检测到邮件是否已正确传递到 smtp 服务器。您无法直接检测接收服务器是否拒绝/发送垃圾邮件,或者您的 smtp 服务器是否延迟/排队邮件。

标签: php email cakephp error-handling


【解决方案1】:

您可以使用 try catch 块来检查邮件是否已成功移交给 MTA,您无法真正检测或检查邮件是否已成功交付给收件人。那是另一种情况。

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

或者,如果您在邮件标题中设置了回复,那么您可以检查是否有任何退回的邮件,这会让您确定邮件尚未送达。

【讨论】:

    猜你喜欢
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多