PHP邮件多地址

2022-08-30 07:46:17

可能的重复项: PHPMailer AddAddress()

这是我的代码。

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

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

    $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

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

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

更好的是,将它们添加为碳副本收件人。

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

为了简化操作,您应该遍历一个数组来执行此操作。

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

答案 2

推荐