我正在尝试使用CakeEmail发送电子邮件,但$email->to()
方法失败了。可以在此图片上看到错误消息:http://i47.tinypic.com/240yq86.png
基本上,我遇到了这两个错误:
Warning (2): preg_match() expects parameter 2 to be string, object given [CORE/Cake/Utility/Validation.php, line 815]
Warning (4096): Object of class CakeEmail could not be converted to string [CORE/Cake/basics.php, line 566]
这是我的sendEmail()
方法:
function sendEmail($id, $email, $token)
{
print_r($email);
$email = new CakeEmail();
$email->from('admin@gmail.com');
$email->to($email);
$email->subject('Activate your account');
$activate_url = 'http://' . env('SERVER_NAME') .'/users/activate/'.$id.'/'.$token;
$message = "Thank you for signing up. Click on the activation link to activate your account \n";
return $email->send($message.$activate_url);
}
答案 0 :(得分:3)
你有一些基本的php错误:
将功能参数2名称更改为$to
;
将print_r($email)
更改为print_r($to)
;
将$ email->更改为($ email); to
$ email->更改为($ to);`
答案 1 :(得分:1)
所以,我一直在寻找可能会扰乱课程配置的其他问题......
根据http://book.cakephp.org/2.0/en/core-utility-libraries/email.html,from()
接受一个数组(虽然没有明确声明您 使用数组)。
请尝试使用此行:
$email->from(array('admin@gmail.com' => 'Admin at gMail'));
答案 2 :(得分:0)
请注意,$email
覆盖了参数中的$email = new CakeEmail()
,将其更改为其他变量$to
function sendEmail($id, $to, $token)
{
print_r($email);
$email = new CakeEmail();
$email->from('admin@gmail.com');
$email->to($to);
$email->subject('Activate your account');
$activate_url = 'http://' . env('SERVER_NAME') .'/users/activate/'.$id.'/'.$token;
$message = "Thank you for signing up. Click on the activation link to activate your account \n";
return $email->send($message.$activate_url);
}