I wrote a mass mailer program and am having problems with it sending duplciate emails to the same email address. Sometimes it will send as many as 4 or 5 emails to the same address.
I am using the phpmailer class version 1.73. I also have it set up to use a POP3 account. I am going to be changing it so that I use either sendmail or qmail depending on the server I set it up on.
I have tried writign the program several different ways to try to stop it from sending duplicates, but it still does. When I run a test with 2 or 3 emails in the test table in the database it only sends one message to each email address, but when I send out to several hundred or more email addresses it sends multiple emails to each address. It sends to all the addresses in the query one row at a time, then it proceeds to send to all of them again.
I tested the mailer and did an echo of each email address instead of sending an email to each email address and it only echod each address to the screen one time, then displayed a message to the screen saying that the mailing was complete.
This is the query that I wrote. Each email address can only be on one row of the result.
Please help me. I am not sure why it is sending out duplicate emails to each address when the query only has each email address on one row.
// This case does the query for the mass mailing of the newsletter
if ($_POST['selectMaillist'] == "optins"){
$query = "SELECT DISTINCT email from tbl_maillist where opt_in_status = 'true'";
}
else if ($_POST['selectMaillist'] == "test"){
$query = "SELECT DISTINCT email from tbl_maillist_test where opt_in_status = 'true'";
}
else{
$message = "You must choose a mailing list to send to.";
$xtpl->assign("message", $message);
$xtpl->parse("main.message");
}
if ($_POST['selectMaillist'] != "Choose"){
if (!($result = @ mysql_query($query, $connection)))
showerror();
$_SESSION['result'] = array();
$_SESSION['result'] = $result;
}
Here is where I set the configurations for php mailer, call the query function and send the emails. At first I wrote this loop with a while loop that just read each row from the query and then sent the emails out one row at a time.
$mail->IsSMTP(); // set mailer to use smtp
$mail->IsHTML(true); // set the mailer to use html emails
$mail->Subject = $_POST['subject'];
$mail->AddReplyTo($emailAddress, $name); // reply to address
// call the mass mail query function
massMailQuery(&$connection, "massMailQuery", &$xtpl);
// get the result from the query
$result = $_SESSION['result'];
unset($_SESSION['result']);
// send the emails one row at a time
for ($counter = 0; $row = mysql_fetch_row($result); $counter++){
foreach($row as $key => $email){
// create the body of the email for html email clients
// then create an alternative for non html email clients
$mail->Body = $message2;
$mail->AltBody = $message;
// set up the to address
$mail->AddAddress($email, $row['firstName'] . " " . $row['lastName']);
// if the mailing is not successful display an error message
if(!$mail->Send()){
$message = "Message could not be sent. <br>";
$message .= "Mailer Error: " . $mail->ErrorInfo;
$xtpl->assign("message", $message);
$xtpl->parse("main.message");
}
$mail->ClearAddresses();
}
}
// display a message say the mailing is complete
$message = "The mailing is complete.";
$xtpl->assign("message", $message);
$xtpl->parse("main.message");