I have a very simple script that sends a periodic email from the server to confirm that everything is working ok. The email is sent to only one address and every time, two identical emails are sent.There are no loops, no database calls for additional addresses. Everything is set and fixed in the script.
There are a ton of similar questions on this topic. All deal with a slightly different situation - adding addresses, occasional duplicates, etc. - and I've carefully followed the advice in each. I've also followed the advice of @Synchro to read the PHPMailer docs carefully. Specifically I have:
In every case I get the same result - 2 identical emails get sent with the same Message-IDs with exactly the same time stamp.
The simplified test_send_mail.php calling script:
$subject = 'TEST PHP_MAILER';
$msg = 'this is only a test';
$mail = new send_mail('','',$subject,$msg);
$mail->send_it();
exit('sent');
The send_mail class looks like this:
require_once($_SERVER['DOCUMENT_ROOT'].'/core/phpMail_core.php');
set_include_path('c:/Apache24/Core/_common/PHPMailer-master/');
require 'class.phpmailer.php';
require 'class.smtp.php';
class send_mail {
//__CONSTRUCT
function __construct($to='',$from='',$subject='[no subject]',$body='[no content]') {
if(empty($to)) {
$to = DEFAULT_RECIPIENT;
}
if(empty($from)) {
$from = SYS_ADMIN;
}
$this->ready_mail = $this->set_parameters($to,$from,$subject,$body);
}
// SET_PARAMETERS
function set_parameters($to,$from,$subject,$body) {
$mail = new PHPMailer(true); // true = throw exceptions on errors
$mail->IsSMTP();
try {
$mail->clearAddresses();
$mail->Host = SMTP_SERVER;
$mail->SMTPDebug = 0; //0 = none; 1 = some; 2 = all
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Port = SMTP_PORT;
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
//for php5.6 to bypass SSL check
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->WordWrap = 50;
$mail->isHTML(true); // Set email format
$mail->SetFrom($from);
$mail->FromName = 'UHMS SysAdmin';
$mail->addAddress($to);
//for local only
$mail->Subject = $subject.' *local*';
//for production only
//$mail->Subject = $subject;
$mail->Body = $body;
} catch (phpmailerException $e) {
pp('phpMailerException',$e->errorMessage());
} catch (Exception $e) {
pp('send_mail exception',$e->getMessage());
}
return $mail;
}
//SEND_IT
function send_it() {
echo '<br>sending...';
$this->ready_mail->Send();
}
PHPMail_core.php simply defines the default values (CAPITALIZED_TERMS) used in the send_mail class, nothing more.
The environment is: PHP 7.0.13/PHPMailer 5.2.21/Apache 2.4.23
What else could possibly be causing the duplicate emails?
Well the bottom line is that the phenomenon described above was totally caused by an "operator error". Sometime in the past, I had set up an email filter rule in cPanel for the recipient email account. The rule effectively caused a duplicate delivery of this particular email. It's weird that I wasn't getting duplicates of other emails, but indeed, as soon as the offending rule was removed, the duplicate emails described above ceased...and all emails seem to be coming through just fine. So the root cause had nothing to do with PHPMailer, the hosting company, Thunderbird or the code.
The filter rule was created long ago and far away so wasn't the obvious cause. I'm memorializing the conclusion here just in case it helps. Let it be proof that the tracks you lay can sometimes lead you in the wrong direction...
Totally appreciate all the help provided by others in getting to this embarrassing conclusion.