Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer AddAddress()

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 = "[email protected],[email protected],[email protected]";
$obj->AddAddress($to);

but with no success.

like image 765
kmunky Avatar asked Nov 20 '09 14:11

kmunky


People also ask

How to Add multiple recipient in PHPMailer?

You need to call the AddAddress method once for every recipient. Like so: $mail->AddAddress('[email protected]', 'Person One'); $mail->AddAddress('[email protected]', 'Person Two'); // .. Better yet, add them as Carbon Copy recipients.

How to send multiple mail using PHPMailer?

You have to create a completely different PHPMailer object for the second email, or you could just use AddAddress to send the same email to multiple addresses. If you don't want the senders to see each other, use BCC.

How many emails can I send with PHPMailer?

Its called SMTP Relays and it is defined on a per (sending) email basis but usually defaults to 250.


3 Answers

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('[email protected]', 'First Name');
$mailer->AddAddress('[email protected]', 'Second Name');
$mailer->AddAddress('[email protected]', 'Third Name');

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

like image 111
doamnaT Avatar answered Oct 09 '22 12:10

doamnaT


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

$mail->AddAddress('[email protected]', 'Person One');
$mail->AddAddress('[email protected]', 'Person Two');
// ..

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

$recipients = array(
   '[email protected]' => 'Person One',
   '[email protected]' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddAddress($email, $name);
}

Better yet, add them as Carbon Copy recipients.

$mail->AddCC('[email protected]', 'Person One');
$mail->AddCC('[email protected]', 'Person Two');
// ..

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

$recipients = array(
   '[email protected]' => 'Person One',
   '[email protected]' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddCC($email, $name);
}
like image 26
Mahendra Jella Avatar answered Oct 09 '22 13:10

Mahendra Jella


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');
}
like image 6
Purple Tentacle Avatar answered Oct 09 '22 13:10

Purple Tentacle