Diasline
Diasline

Reputation: 635

PHP Send emails to multiple addresses with amazon SES

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="[email protected],[email protected]"; 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="[email protected]";
 $from="[email protected]";
 $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";
  }
 ?>

Upvotes: 0

Views: 1687

Answers (2)

Rafael Shkembi
Rafael Shkembi

Reputation: 776

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

$to = $_POST['name'];

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


$emails = array(
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]", 
    "[email protected]", 
    "[email protected]"
);

foreach ($emails as $email){
    $mail->AddAddress($email);
}

Upvotes: 1

awl19
awl19

Reputation: 368

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

$mail->AddAddress('[email protected]');
$mail->AddAddress('[email protected]');
$mail->AddAddress('[email protected]');

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

Upvotes: 1

Related Questions