【发布时间】:2014-02-28 22:08:43
【问题描述】:
我正在尝试使用 phpmailer 将电子邮件发送到数据库中找到的每个电子邮件地址,但作为唯一的电子邮件。出于某种原因,它正在发送重复的电子邮件,并且它以与我的查询返回行一样多的副本发送出去。因此,如果我的查询返回 5 行,每个收件人将收到 5 封电子邮件(发送的电子邮件总数为 25)。我不能为多个收件人使用同一个电子邮件,因为电子邮件内容是个性化的。
我的代码做错了什么?请帮忙...
这是我的代码:
$customers_query = "SELECT customer_name, customer_email, customer_id FROM customers";
$customers = mysql_query($customers_query);
if (!$customers) {
$message = 'Error notice: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
require_once('class.phpmailer.php');
// create an instance
while ($row = mysql_fetch_array($customers)) {
$email = $row['customer_email'];
$name = $row['customers_name'];
$id = $row['customer_id'];
$mail = new PHPMailer();
// use sendmail for the mailer
$mail->IsSendmail();
$mail->SingleTo = true;
$mail->IsHTML(true);
$mail->From = "noreply@domain.com";
$mail->FromName = "MyWebsite";
$mail->Subject = "Welcome to MyWebsite";
$mail->AddAddress($email);
$mail->Body = "Dear ".$name.", welcome to MyWebsite. Your ID is: ".$id.". Enjoy your stay.";
$mail->Send();
}
那么,这里缺少什么?为什么它会发送这么多电子邮件?
【问题讨论】:
-
它发送的所有电子邮件是完全重复的,还是发送多封电子邮件,每个电子邮件的名称和 ID 都不同?
-
附带说明:mysql 函数已被弃用。请改用 mysqli 或 PDO,因为它们更稳定、更安全!