我的cakePHP版本2.2.3
我在向某人发送电子邮件时收到错误消息。
代码:
$to = 'xxxxxxxx@xxxxxx.xx';
$from='noreply@xxxx.xx';
$sendMessage ='hello world!';
$Email = new CakeEmail();
$Email->template('default')
->emailFormat('html')
->to($to)
->from($from)
->subject('My Subject')
->send($sendMessage);
错误讯息 -
致命错误:在 /home/chatfun/app/Model/User.php 行 78
我的代码问题在哪里?
答案 0 :(得分:4)
来自源摘录:
556: public function subject($subject = null) {
557: if ($subject === null) {
558: return $this->_subject;
559: }
560: $this->_subject = $this->_encode((string)$subject);
561: return $this;
562: }
正如您所知,当您提供null
作为主题时,它将返回主题,而不是实例。
因此,请确保您的$getSetting['Setting']['subject']
不为空。现在是什么情况
答案 1 :(得分:0)
我建议自己调用setter方法。主要是因为如果你打算为此编写一个测试用例,它将使你的生活更轻松;
<强>模型强>
// /Model/User.php
function sendEmail() {
if (!$this->Email) {
$this->Email = new CakeEmail();
}
$this->Email->to('xxxxxxxx@xxxxxx.xx');
$this->Email->from('noreply@xxxx.xx');
$this->Email->emailFormat('html');
$this->Email->template('default');
$this->Email->subject('My Subject');
$this->Email->send();
}
<强>测试强>
// /Test/Case/Model/UserTest.php
function testSendEmail() {
$this->User->Email = $this->getMock('CakeEmail');
$this->User->Email->expects($this->once())
->method('to')
->with($this->equalTo('xxxxxxxx@xxxxxx.xx'));
$this->User->Email->expects($this->once())
->method('subject')
->with($this->equalTo('My subject'));
$this->User->Email->expects($this->once())
->method('send');
$this->User->sendEmail();
}