我试图使用SwiftMailer在Kohana发送电子邮件,但一直遇到一个关于数组到字符串转换的错误。
我的代码是这样的:
$mailer = Email::connect();
$to = 'boboz@gmail.com';
$from = 'no-reply@yahoo.com';
$subject = 'Hey, say hello!';
$body = 'Hello World!';
$message_swift = Swift_Message::newInstance($subject, $body)
->setFrom($from)
->setTo($to);
if ($mailer->send($message_swift))
{
echo 'Massage Send! Bravo!';
}
else
{
echo 'Message failed! Booo!';
}
显示的错误:
MODPATH/kohana-email/vender/swift/classes/swift/Transport/MailTransport.php[183]错误:ErrorException[注意]:数组到字符串的转换
它所指的SwiftMailer部分在这里:
178 $headers = str_replace("'r'n.", "'r'n..", $headers);
179 $body = str_replace("'r'n.", "'r'n..", $body);
180 }
181
182 if ($this->_invoker->mail($to, $subject, $body, $headers,
183 sprintf($this->_extraParams, $reversePath)))
184 {
185 if ($evt)
186 {
187 $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
188 $evt->setFailedRecipients($failedRecipients);
为什么我会得到这个变量转换错误?
您需要确保在config文件夹中名为email.php的配置文件中将驱动程序设置为正确的值。
$to
应该是一个数组:
$to = array($email => $name);
或者只是
$to = array($email);
你可以做:
$message_swift = Swift_Message::newInstance($subject, $body)
->setFrom(array($from))
->setTo(array($to));
setFrom
也是如此。