简体   繁体   English

CakePHP电子邮件组件检查是否已发送电子邮件

[英]CakePHP Email Component check if email was sent

I am just wondering how can you check if email was sent or it failed whenever using the EmailComponent in CakePHP? 我只是想知道如何在CakePHP中使用EmailComponent时检查电子邮件是否已发送或失败?

For example I currently use it this way: 例如,我目前以这种方式使用它:

$this->Email->from='<xyz@yahoo.com>';  
$this->Email->to='<abc@gmail.com>';
$this->Email->sendAs='both';
$this->Email->delivery = 'debug';
$this->Email->send();

$this->Email->send() should return true if it was sent successfully. $this->Email->send()如果成功发送则应该返回true。 You could try something like: 你可以尝试类似的东西:

if ( $this->Email->send() ) {
    // Success
} else {
    // Failure
}

Reference: 参考:

http://api.cakephp.org/2.3/class-EmailComponent.html http://api.cakephp.org/2.3/class-EmailComponent.html

Note: If you're using CakePHP 2.x you might try using the CakeEmail class instead; 注意:如果您正在使用CakePHP 2.x,您可以尝试使用CakeEmail类; EmailComponent is deprecated ( Reference ). 不推荐使用EmailComponent( 参考 )。 If you're using 1.x then carry on. 如果你使用1.x然后继续。 :p :p

Edit: 编辑:

As noted in the comments, if you are using 2.x you should keep in mind that CakeEmail (which is used by EmailComponent) can throw an exception. 正如在评论中指出,如果你使用 2.x的,你应该记住,CakeEmail(这是使用EmailComponent)可引发异常。 You can handle it with CakePHP itself or by tossing in a try/catch: 您可以使用CakePHP本身或通过尝试/捕获来处理它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM