ds342222222 2019-01-22 14:42
浏览 30

PHPMailer将重复的电子邮件发送到某些电子邮件地址

I'm facing an issue with PHPMailer, I sent emails to 6 email addresses, 2 of them received the message twice.

The hosting is Godaddy and I'm using the latest version of PHPMailer.

Here is the code:

//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;
}

Both 'user@example.com' and 'contact@example.com', The 2nd and the 5th received the message twice.

The script was executed once and I use $mail->clearAddresses(); to clear the email addresses before the next loop.

What's the problem and how to solve it?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算