【发布时间】:2016-12-28 05:26:39
【问题描述】:
我正在使用 phpmailer,我想通过使用 for 循环一次将其发送到多个电子邮件。这是我目前的编码:
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'yyy.com'; // SMTP username
$mail->Password = 'yyy'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$person = array(0 => 'xxx.com', 'xxx2.com');
for ($x = 0; $x < 2; $x++) {
$mail->addAddress($person[$x]);
}
$mail->setFrom('yyy.com');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = "xxx";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
现在,代码可以运行,但是,当您在电子邮件客户端查看“TO”时,它还会输出其他电子邮件,包括在旁边 - 我不希望这样。就像,当你给 xxx.com 发邮件时,xxx2.com 可以在他的“收件人”字段中看到TO <name1> <xxx.com>, <name2> <xxx2.com>。我该如何避免呢?我应该从$mail = new PHPMailer; 开始创建一个循环吗?
【问题讨论】:
-
您只是在发送前将电子邮件地址添加到收件人列表中。您还需要将
$mail->send()放入循环中。 -
我的代码现在应该是什么样子?
-
在下面查看我的答案