CakePHP电子邮件组件检查是否发送了电子邮件 [英] CakePHP Email Component check if email was sent

查看:123
本文介绍了CakePHP电子邮件组件检查是否发送了电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何使用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='<xyz@yahoo.com>';  
$this->Email->to='<abc@gmail.com>';
$this->Email->sendAs='both';
$this->Email->delivery = 'debug';
$this->Email->send();


推荐答案

$ this->如果已成功发送,则Email> 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
}

参考:

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

注意:如果您使用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使用)可以抛出异常。您可以使用 CakePHP本身或者通过折腾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电子邮件组件检查是否发送了电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆