开发者

SwiftMailer email in Kohana triggers array to string conversion

开发者 https://www.devze.com 2023-04-05 20:21 出处:网络
I\'m trying to email in Kohana using SwiftMailer but keep coming across an error about an array to string conversion.

I'm trying to email in Kohana using SwiftMailer but keep coming across an error about an array to string conversion.

My code is thus:

$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!';
}

The error displayed:

MODPATH/kohana-email/vendor/swift/classes/Swift/Transport/MailTransport.php [ 183 ] Error: ErrorException [ Notice ]: Array to string conversion

The part of SwiftMailer it's referring to is here:

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);

Why am I getting this variable conversion error?


You need to make sure the driver is set to the right value in the config file called email.php found in the config folder.


$to should be an array:

$to = array($email => $name);

or just

$to = array($email);

you could do:

$message_swift = Swift_Message::newInstance($subject, $body)
->setFrom(array($from))
->setTo(array($to));

Same thing goes with setFrom.

0

精彩评论

暂无评论...
验证码 换一张
取 消