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