I want a foreach
loop to send email to multiple addresses each time I run the PHP code:
$id = "a@a.com
b@c.com
d@e.com";
$new = explode("\n", $id);
foreach ($new as $addr) {
$mail->addAddress($addr);
}
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
But it put all the email addresses in to
field and then sends the email.
So when someone get the email, he can see all the email addressees in the to
field.
I want a code to send emails one by one.
The problem you have is that you're actually adding multiple recipient addAddress()
before actually sending the email.
So, after your loop...
$mail->addAddress('a@a.com'); // Add a recipient
$mail->addAddress('b@c.com'); // Add a another recipient
$mail->addAddress('d@e.com'); // Add a another recipient
the TO
address is now a@a.com, b@c.com, d@e.com
And then... you're sending the email to all of them.
To send the email one by one
I would initialize the mail
object completely inside the loop.
(or call another function passing the address as an argument).