问题描述
我只是想知道如何在CakePHP中使用EmailComponent时检查电子邮件是发送还是失败?
I am just wondering how can you check if email was sent or it failed whenever using the EmailComponent in CakePHP?
例如,我目前以这种方式使用它: / p>
For example I currently use it this way:
$this->Email->from='<[email protected]>';
$this->Email->to='<[email protected]>';
$this->Email->sendAs='both';
$this->Email->delivery = 'debug';
$this->Email->send();
推荐答案
$ this->如果发送成功,则发送邮件 - > send()
应该返回true。您可以尝试以下方式:
$this->Email->send()
should return true if it was sent successfully. You could try something like:
if ( $this->Email->send() ) {
// Success
} else {
// Failure
}
参考:
注意:如果您使用的是CakePHP 2.x,可以尝试使用CakeEmail上课EmailComponent已被弃用()。如果你使用1.x然后继续。 :p
Note: If you're using CakePHP 2.x you might try using the CakeEmail class instead; EmailComponent is deprecated (Reference). If you're using 1.x then carry on. :p
编辑:
如评论中所述, em>使用2.x你应该记住,CakeEmail(由EmailComponent使用)可以引发异常。您可以使用或通过投掷一个try / catch来处理它:
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. You can handle it with CakePHP itself or by tossing in a try/catch:
try {
if ( $this->Email->send() ) {
// Success
} else {
// Failure, without any exceptions
}
} catch ( Exception $e ) {
// Failure, with exception
}
这篇关于CakePHP电子邮件组件检查电子邮件是否发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!