0

I use phpMailer to send email with amazon SES, my codes work perfectly. I'm stuck to send email to multiples addresses, when i try to affect more than one address like this $to="abc@gmail.com,def@gmail.com"; it's not working and i have this message error : Mailer Error: You must provide at least one recipient email address. but when i have only one address it's worked. How can send email to multiple addresses ?

$account="username";
$password="password";
 $to="abc@gmail.com";
 $from="peter@gmail.com";
 $from_name="Peter";
 $msg="<strong>test smtp with amazon.</strong>"; // HTML message
 $subject="HTML message";
require 'class.phpmailer.php';

 $mail = new PHPMailer();
 $mail->IsSMTP();
 $mail->CharSet = 'UTF-8';
 $mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->SMTPAuth= true;
 $mail->Port = 465; // Or 587
$mail->Username= $account;
$mail->Password= $password;
$mail->SMTPSecure = 'ssl';
$mail->From = $from;
$mail->FromName= $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->addAddress($to);

if(!$mail->send()){
  echo "Mailer Error: " . $mail->ErrorInfo;
 }else{
 echo "E-Mail has been sent";
  }
 ?>
Diasline
  • 625
  • 3
  • 32
  • 1
    Why not use the [SES PHP SDK](http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-ses.html)? – Ankh Jul 07 '16 at 13:17

2 Answers2

1

You have to use addAddress for each email address you want to add.

$mail->AddAddress('person1@domain.com');
$mail->AddAddress('person2@domain.com');
$mail->AddAddress('person3@domain.com');

Source: https://stackoverflow.com/a/3149528/1790315

Community
  • 1
  • 1
awl19
  • 366
  • 4
  • 10
1

You can make a array with all the emails and do a foreach loop.

$to = $_POST['name'];

$emails = explode(',',$to);


$emails = array(
    "email@email.com",
    "email2@email.com",
    "email3@email.com",
    "email4@email.com",
    "email5@email.com", 
    "email6@email.com", 
    "email7@email.com"
);

foreach ($emails as $email){
    $mail->AddAddress($email);
}
Rafael Shkembi
  • 786
  • 6
  • 16