通过phpmailer发送大量邮件

3
我正在使用phpmailer向我的订阅者发送批量邮件,但我遇到了一个可怕的问题,就是当我向我的订阅者发送电子邮件时,每个订阅者都会收到相同的电子邮件多次。有些人会收到4次,有些人会收到14次。
我通过Mysql表获取标志为0的订阅者电子邮件,在while循环结束时,我将订阅者标志更新为1。我知道这是一个循环的问题,但我不知道我错在哪里,甚至不确定是否应该使用while循环或其他方法来解决这个问题。任何帮助都将非常感激。
以下是我的代码:
<?php

$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$link= mysqli_connect("$db_host","$db_username","$db_pass", "mydb") or die ("could not connect to mysql"); 

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Simply:

//Load Composer's autoloader
require 'vendor/autoload.php';

if(isset($_POST['send'])){

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
     $query = "select customer_id, customer_name, customer_email from subscribers where flag_email = 0";
     $result = mysqli_query($link, $query) or die("No customer in the table");;

     while($values = mysqli_fetch_array($result)){
         $id = $values['customer_id'];
         $name = $values['customer_name'];
         $toemail = $values['customer_email'];


    //Server settings
    $mail->SMTPDebug = 1;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp server';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'username';                 // SMTP username
    $mail->Password = 'password';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('username', 'username');


    $mail->addReplyTo('myemail', 'name');

    $mail->addBCC(''.$toemail.'', ''.$name.'');

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'Here is the message';


    $mail->send();
    echo 'Message has been sent';
    $upd_query = "update subscribers set flag_email = 1 where customer_id = ".$id."";
    $result= mysqli_query($link, $upd_query);
}

} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}


}
2个回答

3
这是因为您在循环过程中没有清除to地址 - 请注意该方法被称为addAddress,而不是setAddress,并且它会执行这个动作。您需要在循环结束时调用$mail->clearAddresses();来重置它。除此之外,您做得大多正确,但还有一些可以提高效率的事情,主要是使用keep alive,并在循环之前设置所有消息共同的属性。
您可以在PHPMailer提供的邮件列表示例中查看所有这些工作:邮件列表示例

感谢@Synchro。$mail->clearAddresses();解决了我的问题。我还会根据你在链接中提供的代码进行更改。还有一个问题,我需要将addAddress更改为setAddress吗? - Haris Khan
不,那个方法并不存在,我只是用它来说明名称。 - Synchro

0
我看到的问题是,在 while 循环内将 result 变量替换为 update output。请在 while 循环内使用不同的变量名称。
可以将邮件对象创建移动到 while 循环内部。 $upd_query = "update subscribers set flag_email = 1 where customer_id = ".$id.""; $updateResult= mysqli_query($link, $upd_query);
$query = "select customer_id, customer_name, customer_email from subscribers where flag_email = 0";
     $result = mysqli_query($link, $query) or die("No customer in the table");;

     while($values = mysqli_fetch_assoc($result)){
        $mail = new PHPMailer(true); 
        ....
     }

请将您的邮件对象创建移动到 while 循环中并尝试。 - Santhy K
我做到了。我有3个测试订阅者,我向第一个订阅者发送了1封电子邮件,向第二个订阅者发送了2封电子邮件,向第三个订阅者发送了3封电子邮件。 - Haris Khan
我在表中的最后一个订阅者那里收到了1封电子邮件,而其他2个人则收到了两封电子邮件。 - Haris Khan
不要这样做,这非常低效。 - Synchro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接