首页 > 解决方案 > 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("@", "&#64;", $recipient) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
            echo "Message sent to :"  . ' (' . str_replace("@", "&#64;", $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();在下一个循环之前清除电子邮件地址。

有什么问题以及如何解决?

标签: phpemailsmtpphpmailer

解决方案


推荐阅读