1

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Mario brown
  • 73
  • 1
  • 1
  • 7

1 Answers1

1

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);
}
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • yeah thats it! I was doing the exploding thing and one delivered with the ,Array ( [0] => email1@gmail.com but after other errors I commented out the lines and forgot to turn back on. That was my problem. thanks my problem is solved. – Mario brown Jul 02 '17 at 15:34
  • @Mariobrown If it solved your issue, feel free to upvote and accept the answer. – M. Eriksson Jul 02 '17 at 15:36
  • Can I ask one more thing here about the file upload on the form , It works well when there is attacted file, but when I leave it empty , it says path can not be empty. Done the Voting as adviced. – Mario brown Jul 02 '17 at 15:40
  • I don't really understand what you mean. You should create a new question for that. We would need to see the code for it. Unless the solution is to simply not calling the `addAttachement()`-method if no file is uploaded. – M. Eriksson Jul 02 '17 at 15:43
  • Yes that the solution am looking for , not calling addAttachement() if no file is attached. Any sudo code on how to handle that? – Mario brown Jul 02 '17 at 15:48
  • @Mariobrown You should be able to do that yourself with a simple `if`-statement. If not, you should read some basic PHP-tutorials. – M. Eriksson Jul 03 '17 at 05:31