Rosamunda Rosamunda - 2 months ago 19
PHP Question

PHPMailer shows all recipients in the list when sending an email?

I'm using PHPMailer to send emails. I've use it inside a foreach loop in order to send an email to a list of recipients.

It is working, because the email is being sent, but each recipient receives the whole list of recipients!

foreach ($id as $item) {
$mail->addAddress($item);
$mail->Subject = $correo['mailAsunto'];
$mail->Body = $correo['mailMensaje'];
if(!$mail->send()) {
echo 'Error: ' . $mail->ErrorInfo;
} else {
echo 'Mail sent to '.$item.'<br>';
}
}


I've tried:
$mail->AddBCC($item);
instead of
addAddress
but the email can still be seen in the "TO" header.

What may I do? This can have serious privacy implications.

UPDATE



Just in case anyone has a similar issue, after the responses received, here's how the code is now:

foreach ($id as $item) {
$mail->addAddress($item);
$mail->Subject = $correo['mailAsunto'];
$mail->Body = $correo['mailMensaje'];
if(!$mail->send()) {
echo 'Error: ' . $mail->ErrorInfo;
} else {
echo 'Mail sent to '.$item.'<br>';
$mail->ClearAddresses();
$mail->ClearAllRecipients();
}
}

Answer

You are adding the addresses to the same object, so they are being accumulated. Try deleting each address after sending the message to it:

foreach ($id as $item) {
        $mail->addAddress($item);
        $mail->Subject = $correo['mailAsunto'];
        $mail->Body    = $correo['mailMensaje'];
        if(!$mail->send()) {
            echo 'Error: ' . $mail->ErrorInfo;
        } else { 
          echo 'Mail sent to '.$item.'<br>';
        }
        $mail->ClearAllRecipients();    ◄■■■■■■■■■■■■■■■■■■■■■■■■■
}