douxuanou2787
2017-01-05 17:22
浏览 104
已采纳

PHPMailer每次只向1个收件人发送相同的重复电子邮件

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:

  • simplified the interactions down to the simplest code - forget the chron job, forget gathering the data for the email, etc. (see below)
  • upgraded to the latest version of PHPMailer
  • looked for use of $mail->send() in a conditional statement per this post (could not find any)
  • turned Apache's rewrite engine off, thinking that a possible server redirect was duplicating the request (and restarted server)
  • used firebug to detect duplicate get requests for the page (only one request is shown)
  • disabled all plugins and extensions in firefox and restarted firefox
  • tried the same script in Chrome
  • set the debug notice level for smtp to 1; nothing earth shattering here, the script runs once
  • compared the email Message-IDs. The Message-ID in the smtp debug message and BOTH emails are identical
  • moved relevant scripts to different dev environment

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudiaozhi6658 2017-01-07 00:39
    已采纳

    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.

    点赞 打赏 评论

相关推荐 更多相似问题