102

Possible Duplicate: PHPMailer AddAddress()

Here is my code.

require('class.phpmailer.php');
$mail = new PHPMailer();

$email = 'email1@test.example, email2@test.example, email3@test.example';

    $sendmail = "$email";

    $mail->AddAddress($sendmail,"Subject");
    $mail->Subject = "Subject";
    $mail->Body    = $content;

    if(!$mail->Send()) { # sending mail failed
        $msg="Unknown Error has Occured. Please try again Later.";
    }
    else {
        $msg="Your Message has been sent. We'll keep in touch with you soon.";
    }
}

The Problem
if $email value is only 1. It will send. But multiple don't send. What should I do for this. I know that in mail function you have to separate multiple emails by comma. But not working in phpmailer.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Jorge
  • 5,610
  • 18
  • 47
  • 67

1 Answers1

296

You need to call the AddAddress method once for every recipient. Like so:

$mail->AddAddress('person1@domain.example', 'Person One');
$mail->AddAddress('person2@domain.example', 'Person Two');
// ..

Better yet, add them as Carbon Copy recipients.

$mail->AddCC('person1@domain.example', 'Person One');
$mail->AddCC('person2@domain.example', 'Person Two');
// ..

To make things easy, you should loop through an array to do this.

$recipients = array(
   'person1@domain.example' => 'Person One',
   'person2@domain.example' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddCC($email, $name);
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Alan Orozco
  • 3,030
  • 1
  • 16
  • 6
  • 22
    BEWARE: using `AddCC()` in place of `AddAddress()` caused the PHPMailer error `Email error: You must provide at least one recipient email address`. PHPMailer seems to have recovered from this by copying the first CC address into the To field. This ended up with emails that are both emailed and cc'ed to the same address. – doub1ejack Dec 06 '12 at 21:11
  • 10
    I just want to add that using AddCC will email all the recipients but they will all see each others emails in the header when they open the email. Use AddBCC instead – badsyntax Jan 28 '16 at 07:45
  • 4
    the question was bad, but this answers all 3 of my questions at once - thanks – RozzA Sep 15 '16 at 03:18
  • 1
    @doub1ejack how to track that mail is send to all the receipients both in to address and also in cc address. if any mail is not send how to log it – Joyson Jan 05 '18 at 13:29
  • Thanks @badsyntax I was wondering about it, should be part of the answer. – Mateus Ribeiro Jun 26 '20 at 21:19
  • 1
    You should always send the email to yourself since all the people you're sending to likely know YOUR email, and then BCC all recipients.... or CC all recipients. Usually you use bcc so the recipients also do not see each others' email. – Mattt Jan 12 '21 at 21:10