通过phpmailer发送批量电子邮件 [英] Sending Bulk emails through phpmailer

查看:114
本文介绍了通过phpmailer发送批量电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用phpmailer向我的订阅者发送批量电子邮件,但是我面临一个可怕的问题,那就是当我向订阅者发送电子邮件时,每个订阅者都多次收到同一封电子邮件.有些获得4倍,有些获得14倍. 我正在通过Mysql表获取其中flag = 0的订户电子邮件,并在while循环结束时将订户标志更新为1.我知道这是一个循环问题,但是我不知道我在哪里做错甚至不确定我应该使用while循环或其他方法来解决此问题.任何帮助将非常感谢我. 这是我的代码:

I am using phpmailer to send bulk emails to my subscribers but I am facing a terrible problem that is when I send emails to my subscribers, each subscriber is getting the same email more than once. some are getting 4 times and some are getting 14 times. I am fetching subscriber emails where flag = 0 through Mysql table and at the end of the while loop I am updating subscriber flag to 1. I know its a issue of loop but I don't know where I am doing wrong or even not sure should I used while loop or something else to fix this issue. Any help would be highly grateful to me. here is my code:

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


}

推荐答案

之所以会这样,是因为您没有在每次循环时都清除to地址-请注意该方法称为addAddress,而不是setAddress ,它可以完成建议的工作.您需要在循环结束时调用$mail->clearAddresses();以将其重置.否则,您所做的事情大部分都是正确的,但是您可以采取一些措施来提高效率,主要是使用保持活动状态并在循环之前设置所有消息通用的所有属性.

This is happening because you're not clearing the to addresses each time around your loop - notice the method is called addAddress, not setAddress, and it does what that suggests. You need to call $mail->clearAddresses(); at the end of your loop to reset it. You're doing things mostly right otherwise, but there are a few things you can do to improve efficiency, mainly using keep alive and setting all the properties that are common to all messages before your loop.

您可以在随邮件一起提供的示例中看到所有这些工作PHPMailer .

这篇关于通过phpmailer发送批量电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆