PHP邮件程序多个地址

时间:2010-06-30 13:02:09

标签: php phpmailer

  

可能重复:
  PHPMailer AddAddress()

这是我的代码。

require('class.phpmailer.php');
$mail = new PHPMailer();

$email = 'email1@test.com, email2@test.com, email3@test.com';

    $sendmail = "$email";

    $mail->AddAddress($sendmail,"Subject");
    $mail->Subject = "Subject"; 
    $mail->Body    = $content;      

    if(!$mail->Send()) { # sending mail failed
        $msg="Unknown Error has Occured. Please try again Later.";
    }
    else {
        $msg="Your Message has been sent. We'll keep in touch with you soon.";
    }   
}

问题
如果 $ email 值仅为1.它将发送。但是多个不发送。我该怎么做呢我知道在邮件功能中你必须用逗号分隔多个电子邮件。但不能在phpmailer中工作。

1 个答案:

答案 0 :(得分:228)

您需要为每个收件人调用一次AddAddress方法。像这样:

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

更好的是,将它们添加为Carbon Copy收件人。

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

为了简单起见,你应该循环一个数组来做到这一点。

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