我遇到了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;
}
第2个和第5个“ user@example.com”和“ contact@example.com”都两次收到了该消息。
该脚本执行一次,我用$mail->clearAddresses();
清除下一个循环之前的电子邮件地址。
有什么问题以及如何解决?