2017-07-02 112 views
0

我想通过swiftmailer lib从联系人表单发送电子邮件。我的设置发送邮件到一个收件人,但是当我尝试发送到多个电子邮件,它抛出一个错误:Swiftmailer:发送电子邮件给多个收件人

Address in mailbox given [[email protected],[email protected]] does not comply with RFC 2822, 3.6.2.

但两封邮件根据规范有效。

下面是代码;

$failed = []; 
$sent = 0; 
$to = []; 

if (isset($_POST['recipients'])) { 
    $recipients = $_POST['recipients']; 
} 

// Send the message 
foreach ((array) $recipients as $to) { 
    $message->setTo($to); 
    $sent += $mailer->send($message, $failed); 
} 

print_r($recipients); 
printf("Sent %d messages\n", $sent); 

当我输入字段一个电子邮件发送,print_r($recipients)给了我这个数组:(Array ([0] => [email protected]) Sent 1 messages)之前,但现在它不给数组。我知道foreach期望数组,但我没有得到数组。

有一次,我收到一个错误,'收件人'是未定义的;这就是为什么我添加了如果isset()检查。

如何分别发送每封电子邮件?

回答

0

看起来像$_POST['recipients']是一个逗号分隔的字符串。您需要使用explode()将逗号分隔字符串。作为一个阵列铸造将不会为你这样做:

// We should give $recipients a default value, in case it's empty. 
// Otherwise, you would get an error when trying to use it in your foreach-loop 
$recipients = []; 

if(!empty($_POST['recipients'])){ 
    // Explode the string 
    $recipients = explode(',', $_POST['recipients']); 
}  

// Send the message 
foreach ($recipients as $to) { 
    // To be safe, we should trim the addresses as well, removing any potential spaces. 
    $message ->setTo(trim($to)); 
    $sent += $mailer->send($message, $failed); 
} 
+0

是啊这就是它!我正在做爆炸的事情,一个与阵列([0] => [email protected])交付,但在其他错误后,我评论了线路,并忘记回头。这是我的问题。谢谢我的问题解决了。 –

+0

@Mariobrown如果它解决您的问题,可随时给予好评,并接受了答案。 –

+0

我可以一两件事在这里请教一下表单上的文件上传,它运作良好,当有attacted文件,但是当我离开它空,它表示路径不能为空根据提示完成投票 –

相关问题