This script results in the email being send twice (not always). I have read that it could be the browsers fault, so Ive run chrome without the extensions. Still the same.. The subject line holds a random number, so I can see it's the same mail. So I have the same number in my inbox twice.
Is there a browser that is safe to send mails in bulk (mailing) via PHPmailer?
Here is my code (which is not the problem probably)
if(!isset($_SESSION[$reciever]))
{
$_SESSION[$reciever] = 1;
require "mail_inhoud.php"; // <== IMPORTANT INCLUDE
$maand = date('Ym'); // 202103
$datum = date('Ymd'); // '20210406'
$datum_tijd = date('Ymd H:i'); // '20210406'
$maand_ENG = 'March';
$teller = 1;
$raw_results = mysql_query("SELECT EMAIL FROM EMAILS_TESTBANK") or die(mysql_error());
//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->CharSet = 'UTF-8';
//$mail->isSMTP();
$mail->Mailer = 'sendmail';
$mail->SMTPAuth = true;
$mail->Host = 'xxxxxx';
$mail->Port = 587;
$mail->Username = "xxxxx" ;
$mail->Password = "xxxxxx";
$mail->SMTPSecure = "tls";
$mail->setFrom('[email protected]', 'MERS Antique Books Antwerp');
$mail->Sender = '[email protected]';
$mail->addReplyTo('[email protected]', 'MERS Antique Books Antwerp');
$mail->Subject = 'Test Mail ' . rand();
$ONDERWERP = "Catalogue for $maand_ENG";
$mail->Encoding = 'base64';
$today = date('Ymd');
if(mysql_num_rows($raw_results) > 0){
while($row = mysql_fetch_array($raw_results)){
$email = $row['EMAIL'];
$email_user = $row['EMAIL'];
/* We stellen de INHOUD OP */
$message = "";
$message .= "<html>";
$message .= "<body>";
$message .= "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
$message .= "<tr><td align='center' style='width:600px'>";
$htmlContent = file_get_contents("$maand/INTERNATIONAAL.html");
$message .= $htmlContent;
$message .= "<br><br>";
$message .= "<div style='text-align:center'>";
$message .= "<span>This email was sent to $email</span> | <a href='https://www.mers.be/unsubscribe.php?EMAIL=$email' target='new' style='color:#666; text-decoration:none'>Unsubscribe</a>";
$message .= "<br><br>";
$message .= "<div style='visibility:hidden !important; opacity:0 !important; display:none !important' >";
$message .= "<img style='visibility:hidden !important' src='https://www.mers.be/backend/MAILING_NEW/mailcheck.php?user=$email&subject=$ONDERWERP'>";
$message .= "</div>";
$message .= "</div>";
$message .= "</td></tr>";
$message .= "</table>";
//$message .= $teller;
$message .= "</body>";
$message .= "</html>";
$body = $message;
$mail->msgHTML($body);
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$raw_check = mysql_query("SELECT EMAIL FROM EMA_CONTROLE WHERE EMAIL = '$email_user' AND DATE = '$today'") or die(mysql_error());
if(mysql_num_rows($raw_check) > 0){
// DO NOTHING, EMAIL WAS ALREADY SENT
echo 'EMAIL ALREADY SENT<br>';
}else{
echo 'NOT YET SEND<br>';
$mail->addAddress($email, 'Receiver name');
$mail->send(); // SEND EMAIL
$mail->clearAddresses();
$mail->clearAttachments();
echo 'Message sent to : (' . htmlspecialchars($row['EMAIL']) . ')<br>';
//Mark it as sent in the DB
$OND = "NEW ARRIVALS MARCH DEEL 1";
mysql_query('SET NAMES UTF8');
mysql_query("INSERT INTO `MAILING_VERZONDEN`
(`DATUM`, `EMAIL`, WELKE_MAILING, `TAAL`, `RECEIVER`) VALUES
('$datum_tijd', '$email','$OND', 'INTERNATIONAAL', 'KLANT' )") or die(mysql_error());
mysql_query("INSERT INTO `EMA_CONTROLE` ( `EMAIL`, `DATE`) VALUES ('$email','$today')") or die(mysql_error());
}
echo $teller; echo '<br>';
$teller++;
}
}
}
else{
echo "doing it more than once";
}
$conn = null;
$con = null;
exit;
I tried sending this mailing many times. I expect it to arrive once, not 2 or more times
Docs on diagnosing this are here. Using a random string in the subject is to check whether the script is being called more than once, which doesn't seem to be your issue here.
I suggest not sending via
sendmail(its' not clear why you think you need to do that), but use SMTP (i.e. don't comment outisSMTP()), as most of your code seems to want, and set debug output with$mail->SMTPDebug = 2so that you can see exactly what your script is sending to the server. For example, if your SQL is resulting in duplicate rows, it will cause your symptom.You should not be using any
mysql_functions; that extension is long obsolete, though that's not the problem here.