Swiftmailer:发送电子邮件给多个收件人 [英] Swiftmailer: sending email to multiple recipient

查看:228
本文介绍了Swiftmailer:发送电子邮件给多个收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过swiftmailer lib从联系人表单发送电子邮件。我的设置将邮件发送给单个收件人,但是当我尝试发送给多个电子邮件时,它会引发错误:


邮箱中的地址因为[email1 @ gmail.com,email2 @ gmail.com]不符合RFC 2822,3.6.2。



<但是这两封电子邮件根据规范是有效的。

这是代码;

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

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


发送消息
foreach((array)$ recipients as $ to){
$ message-> setTo($ to);
$ sent + = $ mailer-> send($ message,$ failed);
}

print_r($ recipients);
printf(发送%d个消息\\\
,$发送);

当我在输入字段中发送一封电子邮件时, print_r($ recipients )给了我这个数组:(Array([0] => email1@gmail.com)发送1条消息)之前,但现在不是给阵列。



我知道 foreach 需要数组,但是我没有得到一个数组。
$ b

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



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

解决方案

它看起来像 $ _ POST ['recipients'] 是一个逗号分隔字符串。您需要使用 explode() 来分割逗号上的字符串。将它转换为数组不会为你做:

  //我们应该给$收件人一个默认值,以防万一它是空的。 
//否则,当你在foreach循环中使用它的时候你会得到一个错误
$ recipients = [];

if(!empty($ _ POST ['recipients'])){
//爆炸字符串
$ recipients = explode(',',$ _POST ['recipients ]);
}

//发送消息
foreach($ recipients as $ to){
//为了安全起见,我们也应该修剪地址,任何潜在的空间。
$ message - > setTo(trim($ to));
$ sent + = $ mailer-> send($ message,$ failed);
}


I am trying to send email from a contact form through swiftmailer lib. My setup sends mail to a single recipient, but when I try sending to more than one email, it throws an error:

Address in mailbox given [email1@gmail.com,email2@gmail.com] does not comply with RFC 2822, 3.6.2.

but the two emails are valid according to the specification.

Here is the code;

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

When I sent with one email in the input field, print_r($recipients) gave me this array: (Array ( [0] => email1@gmail.com ) Sent 1 messages) before but now it's not giving the array.

I learnt that foreach expects array, but I'm not getting an array.

At one point, I was getting an error that 'recipients' is undefined; that is why I added the if isset() check.

How do I send the each email individually?

解决方案

It looks like $_POST['recipients'] is a comma separated string. You need to use explode() to split the string on the commas. Casting it as an array won't do that for you:

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

这篇关于Swiftmailer:发送电子邮件给多个收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆