delacc
delacc

Reputation: 71

How to use phpmailer to send email to multiple addresses one by one?

I want a foreach loop to send email to multiple addresses each time I run the PHP code:

$id = "[email protected]
    [email protected]
    [email protected]";

$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.

Upvotes: 5

Views: 8704

Answers (5)

OndraTom
OndraTom

Reputation: 82

Use method clearAddresses() (https://goo.gl/r5TR2B) in each loop to clear list of recipients:

$id = "[email protected]
    [email protected]
    [email protected]";

$new = explode("\n", $id);

foreach ($new as $addr) 
{
    $mail->clearAddresses();
    $mail->addAddress($addr);

    if (!$mail->send()) 
    {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } 
    else 
    {
        echo "Message sent!";
    }
}

So You'll have the same object with the same body, subject and other settings.

Upvotes: 5

Ayman Hussein
Ayman Hussein

Reputation: 3857

Initiate new PHPMailer inside foreach and send the email after that.

$id = "[email protected]
[email protected]
[email protected]";

$new = explode("\n", $id);

foreach ($new as $addr) {
   $mail = new PHPMailer();
   $mail->addAddress($addr);

   if (!$mail->send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
   } else {
      echo "Message sent!";
   }
}

Upvotes: 0

gmo
gmo

Reputation: 9000

The problem you have is that you're actually adding multiple recipient addAddress() before actually sending the email.

So, after your loop...

$mail->addAddress('[email protected]');    // Add a recipient
$mail->addAddress('[email protected]');    // Add a another recipient
$mail->addAddress('[email protected]');    // Add a another recipient

the TO address is now [email protected], [email protected], [email protected]
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).

Upvotes: 0

Samvel Avanesov
Samvel Avanesov

Reputation: 422

You should loop through creating and sending mails - so create a new mail message for each receiver. That way they wouldn't be able to see the receiver. Example:

<?php
$people = array("[email protected]", "[email protected]", "[email protected]");

foreach($people as $p) {
    $message = "Line 1\r\nLine 2\r\nLine 3";
    mail($p, 'My Subject', $message);
};

?>

Also you can use BCC field (this is hidden carbon copy).

PHPMailer as suggested before is nice, but you should note, that CC (plain carbon copy) will still be visible to other people in the mailing list.

Upvotes: 0

Vlastislav Nov&#225;k
Vlastislav Nov&#225;k

Reputation: 349

Use for example PHPMailer instead. You can use CC (EDIT: BCC) field with that. Nobody will see the other recipients then.

$mail = new PHPMailer();
$mail->AddBCC('[email protected]');

Upvotes: 2

Related Questions