39

I don't know how the data should be formatted for AddAddress PHPMailer function; I need the email to be sent to multiple recipients so I tried

$to = "me@example.com,you@example.net,she@example.it";
$obj->AddAddress($to);

but with no success.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
kmunky
  • 15,383
  • 17
  • 53
  • 64
  • 1
    PHPMailer can now (May 2015) handle this kind of address string via a parsing function. See [this answer](http://stackoverflow.com/a/30377848/333340) – Synchro May 22 '15 at 08:21

5 Answers5

79

You need to call the AddAddress function once for each E-Mail address you want to send to. There are only two arguments for this function: recipient_email_address and recipient_name. The recipient name is optional and will not be used if not present.

$mailer->AddAddress('recipient1@example.com', 'First Name');
$mailer->AddAddress('recipient2@example.com', 'Second Name');
$mailer->AddAddress('recipient3@example.com', 'Third Name');

You could use an array to store the recipients and then use a for loop.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
doamnaT
  • 1,003
  • 8
  • 4
13

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

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

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

$recipients = array(
   'person1@example.com' => 'Person One',
   'person2@example.com' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddAddress($email, $name);
}

Better yet, add them as Carbon Copy recipients.

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

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

$recipients = array(
   'person1@example.com' => 'Person One',
   'person2@example.com' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddCC($email, $name);
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Mahendra Jella
  • 5,450
  • 1
  • 33
  • 38
6

Some great answers above, using that info here is what I did today to solve the same issue:

$to_array = explode(',', $to);
foreach($to_array as $address)
{
    $mail->addAddress($address, 'Web Enquiry');
}
Purple Tentacle
  • 178
  • 2
  • 5
4
foreach ($all_address as $aa) {
    $mail->AddAddress($aa); 
}
JJJ
  • 32,902
  • 20
  • 89
  • 102
0

All answers are great. Here is an example use case for multiple add address: The ability to add as many email you want on demand with a web form:

See it in action with jsfiddle here (except the php processor)

### Send unlimited email with a web form
# Form for continuously adding e-mails:
<button type="button" onclick="emailNext();">Click to Add Another Email.</button>
<div id="addEmail"></div>
<button type="submit">Send All Emails</button>
# Script function:
<script>
function emailNext() {
    var nextEmail, inside_where;
    nextEmail = document.createElement('input');
    nextEmail.type = 'text';
    nextEmail.name = 'emails[]';
    nextEmail.className = 'class_for_styling';
    nextEmail.style.display = 'block';
    nextEmail.placeholder  = 'Enter E-mail Here';
    inside_where = document.getElementById('addEmail');
    inside_where.appendChild(nextEmail);
    return false;
}
</script>
# PHP Data Processor:
<?php
// ...
// Add the rest of your $mailer here...
if ($_POST[emails]){
    foreach ($_POST[emails] AS $postEmail){
        if ($postEmail){$mailer->AddAddress($postEmail);}
    }
} 
?>

So what it does basically is to generate a new input text box on every click with the name "emails[]".

The [] added at the end makes it an array when posted.

Then we go through each element of the array with "foreach" on PHP side adding the:

    $mailer->AddAddress($postEmail);
Tarik
  • 4,270
  • 38
  • 35