【问题标题】:Sending Bulk emails through phpmailer通过 phpmailer 发送批量电子邮件
【发布时间】:2018-09-29 11:29:40
【问题描述】:

我正在使用 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;
}


}

【问题讨论】:

    标签: php loops while-loop phpmailer


    【解决方案1】:

    发生这种情况是因为您没有在每次循环时都清除 to 地址 - 请注意该方法称为 addAddress,而不是 setAddress,它会按照提示执行此操作。您需要在循环结束时调用$mail-&gt;clearAddresses(); 来重置它。否则你做的事情大多是正确的,但你可以做一些事情来提高效率,主要是使用保持活动状态并在循环之前设置所有消息共有的所有属性。

    您可以在the mailing list example provided with PHPMailer 中看到所有这些工作。

    【讨论】:

    • 感谢@Synchro。 $mail->clearAddresses();解决我的问题。我还将更改您在链接中提供的代码。再想一想我应该将 addAddress 更改为 setAddress 吗??
    • 不,那个方法不存在,我只是用来说明名字的。
    【解决方案2】:

    我看到的问题是,用 while 循环内的更新输出替换结果变量。请在while循环中使用不同的变量名

    $upd_query = "update subscribers set flag_email = 1 where customer_id = ".$id.""; $updateResult= mysqli_query($link, $upd_query);

    邮件对象的创建可以移到while循环里面

    $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 循环并尝试
    • 我做到了。我有 3 位订阅者进行测试,我收到 1 封电子邮件给 1 位订阅者 2 封电子邮件给第 2 位订阅者,3 封电子邮件给 3 位订阅者
    • 我收到了 1 封电子邮件给表中的最后一个订阅者,另外 2 封收到了两次电子邮件
    • 不,不要那样做,效率很低。
    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多