如何在CakePHP中使用模型中的电子邮件组件? [英] How do I use the email component from a model in CakePHP?
问题描述
我有一个很简单的模型。我想为模型的方法添加一个发送电子邮件程序:
I have a very simple model. I want to add a send email routine to on of the methods for the model:
$this->Email->delivery = 'smtp';
$this->Email->template = 'default';
$this->Email->sendAs = 'text';
$this->Email->from = 'email';
$this->Email->to = 'email';
$this->Email->subject = 'Error';
我尝试过
App::import('Component', 'Email');
,无效。我得到的错误是:
at the top, to no avail. The error I get is:
致命错误:调用未定义的方法stdClass :: send()在E:\xampp\htdocs8080 \app\models\debug.php第23行
Fatal error: Call to undefined method stdClass::send() in E:\xampp\htdocs8080\app\models\debug.php on line 23
任何想法?
我正在运行CakePHP 1.2
I'm running CakePHP 1.2
推荐答案
您应该在您的AppController中放置电子邮件发送例程:
Well, you're doing it wrong way. You should place email send routine in your AppController:
function _sendMail($to,$subject,$template) {
$this->Email->to = $to;
// $this->Email->bcc = array('secret@example.com'); // copies
$this->Email->subject = $subject;
$this->Email->replyTo = 'noreply@domain.com';
$this->Email->from = 'MyName <noreply@domain.com>';
$this->Email->template = $template;
$this->Email->sendAs = 'text'; //Send as 'html', 'text' or 'both' (default is 'text')
$this->Email->send();
}
然后使用ANY控制器,如下:
Then use it from ANY controller like this:
$this->_sendMail($this->data['User']['email'],'Thanks for registering!','register');
并且不要忘记放置
var $components = array('Email');
在控制器中,您可以使用_sendMail函数。
in controllers, in which you 're using _sendMail function.
这篇关于如何在CakePHP中使用模型中的电子邮件组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!