Reputation: 49
I want to make an email delivery system to the 2000 email addresses that are registered in the database.
I face the wrong looping problem. So that 1 recipient gets multiple emails.
Please help, thank you
Here is the piece of script:
include "phpmailer/classes/class.phpmailer.php";
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPSecure = 'ssl';
$mail->Host = "domain.com";
$mail->SMTPDebug = 2;
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "passsword";
$mail->SetFrom("[email protected]","No Reply");
$mail->Subject = $mail_subject;
$query_email_list = mysqli_query($koneksi, "SELECT * FROM table");
while($data_email = mysqli_fetch_array($query_email_list)){
QRCode::png($data_email['email'], $tempdir. $data_email['email'].".png", $quality, $ukuran, $padding);
$mail_body = file_get_contents('template/template_email_blast.html');
$mail_body = str_replace('{email}', $data_email['email'], $mail_body);
$mail_body = str_replace('{nama_agent}', $data_email['agent_name'], $mail_body);
$mail_body = str_replace('{tempat_duduk}', $data_email['meja'], $mail_body);
$mail->AddAddress($data_email['email'], $data_email['agent_name']);
$mail->MsgHTML($mail_body);
$sent = $mail->Send();
}
Upvotes: 0
Views: 151
Reputation: 2606
Since AddAddress() is a method it will add an email on every loop.
You will have to clear the addresses or create a new object in very loop.
The better way is to clear the addresses.
$mail->clearAddresses(); // clear all [to] recipients.
$mail->clearAllRecipients(); // clear all recipients [to cc bcc] etc.
Updated Code:
while($data_email = mysqli_fetch_array($query_email_list)){
QRCode::png($data_email['email'], $tempdir. $data_email['email'].".png", $quality, $ukuran, $padding);
$mail_body = file_get_contents('template/template_email_blast.html');
$mail_body = str_replace('{email}', $data_email['email'], $mail_body);
$mail_body = str_replace('{nama_agent}', $data_email['agent_name'], $mail_body);
$mail_body = str_replace('{tempat_duduk}', $data_email['meja'], $mail_body);
// $mail->clearAddresses(); // clear all [to] recipients.
$mail->clearAllRecipients(); // clear all recipients [to cc bcc] etc.
$mail->AddAddress($data_email['email'], $data_email['agent_name']);
$mail->MsgHTML($mail_body);
$sent = $mail->Send();
}
Upvotes: 1