Search code examples
phpphpmailer

PHP mailer multiple address


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.


Solution

  • 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);
    }