When I run below code it fetches email from the database and sends mail 3 times to each user. I don't know what's wrong in my script. Can anyone help me to solve this issue?
<?php
date_default_timezone_set('Asia/Kolkata');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor\autoload.php';
$mail = new PHPMailer(TRUE);
include 'connection.php';
$result = mysqli_query($conn,"SELECT * FROM `newspaper_subscriber` WHERE
status = '1'");
if (mysqli_num_rows($result) > 0) {
while($rows = mysqli_fetch_object($result)) {
$emails[$rows->subscriber_email] = $rows->subscriber_email;
}
}
foreach ($emails as $row) {
$mail->setFrom('[email protected]', 'xyz');
$mail->Subject = 'Weekly Newspaper';
$mail->Body = 'For unsubscribe email us at
[email protected]';
$mail->AddAddress($row);
$mail->isSMTP();
$path = __DIR__."\/01.pdf";
$mail->addAttachment($path, "newspaper.pdf");
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'ssl';
$mail->Username = '[email protected]';
$mail->Password = 'xyzxyz';
$mail->Port = 465;
$mail->send();
}
you have to clear address as in loop it will preserve existing address
so use something like this in loop also
foreach ($emails as $row) {
$mail->ClearAddresses();
$mail->ClearCCs();
$mail->ClearBCCs();
//rest of the code