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 [[email protected],[email protected]] 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] => [email protected] ) 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?