简体   繁体   English

PHP使用Amazon SES将电子邮件发送到多个地址

[英]PHP Send emails to multiple addresses with amazon SES

I use phpMailer to send email with amazon SES, my codes work perfectly. 我使用phpMailer发送带有Amazon SES的电子邮件,我的代码工作正常。 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"; 当我尝试影响多个地址,例如$ 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. 它不起作用,并且我收到此消息错误: Mailer错误:您必须至少提供一个收件人电子邮件地址。 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";
  }
 ?>

You have to use addAddress for each email address you want to add. 您必须对要添加的每个电子邮件地址使用addAddress。

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

Source: https://stackoverflow.com/a/3149528/1790315 资料来源: https : //stackoverflow.com/a/3149528/1790315

You can make a array with all the emails and do a foreach loop. 您可以将所有电子邮件组成一个数组并执行foreach循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM