php - PHPMailer 向某些电子邮件地址发送重复的电子邮件
问题描述
我遇到了 PHPMailer 的问题,我向 6 个电子邮件地址发送了电子邮件,其中 2 个收到了两次消息。
主机是 Godaddy,我使用的是最新版本的 PHPMailer。
这是代码:
//Including PHPMailer files
require_once('phpmailer/src/phpmailer.php');
require_once('phpmailer/src/SMTP.php');
require_once('phpmailer/src/Exception.php');
$msg = '';
//List of email adressess
$recipients = array('help@example.com', 'user@example.com', 'desk@example.com', 'admin@example.com', 'contact@example.com', 'schedule@example.com');
//Initializing PHPMailer
$mail = new PHPMailer\PHPMailer\PHPMailer(); // Passing `true` enables exceptions
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
//Sender data
$mail->setFrom('info@example.com', 'Info');
$mail->addReplyTo('info@example.com', 'Info');
//Content
$mail->isHTML(true);
$mail->Subject = 'Hello World!';
$mail->Body = 'Hello World!';
$mail->AltBody = 'Hello World!';
//Loop throught the email addresses
foreach ($recipients as $recipient) {
$mail->addAddress($recipient);
//Attachments
$mail->AddStringAttachment($pdfString, $filename);
//Check if the message was sent
if (!$mail->send()) {
echo "Mailer Error (" . str_replace("@", "@", $recipient) . ') ' . $mail->ErrorInfo . '<br />';
break; //Abandon sending
} else {
echo "Message sent to :" . ' (' . str_replace("@", "@", $recipient) . ')<br />';
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
$mail->clearAttachments();
}
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
'user@example.com' 和 'contact@example.com',第 2 和第 5 都收到了两次消息。
该脚本执行了一次,我用来$mail->clearAddresses();
在下一个循环之前清除电子邮件地址。
有什么问题以及如何解决?
解决方案
推荐阅读
- charts - Chart.js 从折线图中移除阴影
- php - 将数据从一个视图发送到另一个视图 - 流明 (AMP)
- c - scanf_s() 缓冲区在哪里定义?
- c# - 处理大文本 (JSON) 文件
- python-3.x - 通过不同的行值同时过滤 Pandas
- sql - 将 Windows 系统日期导入 SQL Server
- android - HeifWriter 支持库最低 API 级别为 28
- r - 联合/合并重叠的时间范围
- spring-boot-admin - Spring Boot Admin 在 Cloudfoundry 中的多个服务实例之间不能有区别
- postgresql - 直接在 Google AppEngine 上使用 psycopg2