首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHPMailer和Google继电器。接收者收到其他客户电子邮件时过多的重复邮件

PHPMailer和Google继电器。接收者收到其他客户电子邮件时过多的重复邮件
EN

Stack Overflow用户
提问于 2019-01-19 01:22:30
回答 1查看 409关注 0票数 1

我已经创建了一个脚本,通过电子邮件向每个已发送的客户订单发送一些跟踪细节。

该脚本由PHPMailer构建,并通过Google中继服务发送电子邮件。

在测试期间,所有这些都按预期的方式工作。加载测试的100封电子邮件,尽管相同的电子邮件地址,所有执行的预期。没有重复或丢失的电子邮件。

然而,在直播后不久,有人告诉我,几个客户已经收到了数十封电子邮件,其他客户跟踪细节。

对于gmail用户,他们收到另一个客户的跟踪电子邮件,并将他们的电子邮件地址发送到。

对于outlook用户,他们会收到其他客户的电子邮件,在发送到的区域中会有许多电子邮件。

我已经检查了代码,测试并检查了每个函数的输出,完全失去了它可能出错的地方。

下面是脚本的sudo过程。想看看是否有人可以提供一些提示,我可以如何进行疑难解答这个问题。

  1. 将邮件列表检索到一维数组中.
  2. 创建一个PHPMailer实例
  3. 对于数组中的每个电子邮件,发送电子邮件,等待真正的响应,如果是假的,等待5s,再试总共3次,如果超过继续。
  4. 一旦完成,脚本就结束了。

其他注释

脚本由windows任务调度程序触发。

该脚本将运行大约2-10分钟,取决于重试次数,尽管调度程序设置为每5分钟运行一次,但是调度器中的任务已被设置为如果已在运行,则不会运行多个任务实例。

任何建议都会很好。

干杯

代码片段。希望能提供足够的想法。

一般过程是脚本从sendTrackingEmails()调用Dispatcher_cl.php,然后使用来自MyCustomerMailer.php的各种函数。MyCustomerMailer.php或多或少是PHPMailer的一个抽象层。

Dispatcher_cl.php

代码语言:javascript
运行
复制
public function sendTrackingEmails() {
    try{
        $this->processLog[] = __FUNCTION__;

        /*
        1.Get list of emails
        2.Loop and send emails
        */

        $ordersSent         = array();
        $ordersFailed       = array();
        $result             = false;
        $emailListAndInfo   = $this->getEmailListAndInfo();

        if($emailListAndInfo===false){
            //No orders to dispatch emails. So do nothing.
            $result = false;
        }else{
            $mail = new MyCustomMailer_cl();

            if($mail->setSMTPParam('default')===true){

                foreach($emailListAndInfo as $customer){

                    $sentFrom       = array();
                    $replyTo        = array();
                    $emailTo        = array($customer['email'],$customer['name']);
                    $subject        = array();
                    $body           = null;

                    /*
                        Preset email settings for each brand.
                        When adding vendors, remember to add a BODY template and VENDOR ID to the SQL in [m_getEmailListAndInfo]
                    */
                    if($customer['vendor_id']==3){  
                        //Diamondphoto
                        $sentFrom       = array('no-reply@email.com','Your order has been shipped');
                        $replyTo        = array('no-reply@email.com','Your order has been shipped');
                        $subject        = "Tracking for order {$customer['vendorOrderId']}";
                        $body           = $this->getEmailTemplateFor(3,$customer);


                    }else{
                        /*
                            If vendor_id does not match existing setup, set TRYCOUNT to 99 as indicator and skip remaining execution. 
                            Next script-run will not pick up this record due to 99 will be greater than the usual preset TRYCOUNT.
                        */
                        $this->setTryCountForOrder($customer['order_id'],99);
                        continue;
                    }

                    $emailPackage['sentFrom']   = $sentFrom;
                    $emailPackage['replyTo']    = $replyTo;
                    $emailPackage['emailTo']    = $emailTo;
                    $emailPackage['subject']    = $subject;
                    $emailPackage['body']       = $body;
                    $mail->setupEmail($emailPackage);

                    for($i=1;$i<=$this->emailTryCount;$i++){
                        // $emailSentResult = $mail->send();
                        $emailSentResult = false;

                        if($emailSentResult===true){
                            $setFlagResult = $this->setSentFlagForOrder($customer['order_id'],$i);

                            if($setFlagResult===true){
                                $ordersSent[] = $customer['order_id'];

                                break;
                            }else{
                                throw new Exception("Update sent-flag for order {$customer['order_id']} failed.");
                            }
                        }else if($emailSentResult===false AND $i<$this->emailTryCount){

                            $this->setTryCountForOrder($customer['order_id'],$i);
                            // sleep($this->emailWaitTimer);
                            continue;

                        }else if($emailSentResult===false AND $i==$this->emailTryCount){

                            $this->setTryCountForOrder($customer['order_id'],$i);
                            $ordersFailed[] = $customer['order_id'];

                        }
                    }//End sending/attempting to send tracking email.

                }//End of looping through the emailing list.


            }//End of validation SMTP parameters.

            $result = array('sent'  =>$ordersSent
                            ,'failed'=>$ordersFailed);

        }//End of section for EMAIL-LIST exist



        return $result;

    }catch(Exception $e){
        $this->processLog[] = __FUNCTION__.$e->getMessage();
        throw $e;
    }
}

MyCustomerMailer.php

代码语言:javascript
运行
复制
    <?PHP

require 'class/PHPMailer5.2/PHPMailerAutoload.php';

class MyCustomMailer_cl{

public $processLog      = array();
private $dbConnect      = null;

private $phpMailer      = null;
private $SMTPDebug      = null;
private $Debugoutput    = null;
private $Host           = null; //smtp.gmail.com OR smtp-relay.gmail.com
private $Port           = null; //587
private $SMTPSecure     = null; //tls or ssl
private $SMTPAuth       = null; //true or false
private $Username       = null;
private $Password       = null;


private $sentFrom       = array();  //array('example@gmail.com','Joe Doe');
private $replyTo        = array();  //array('example@gmail.com','Joe Doe');
private $emailTo        = array();  //array('example@gmail.com','Joe Doe');
private $subject        = null;     //Plain text
private $body           = null;     //Plain text


public function __construct() {
    try{
        $this->processLog[] = __FUNCTION__;


    }catch(Exception $e){
        $this->processLog[] = __FUNCTION__.$e->getMessage();
        throw $e;
    }
}


/*******************************************************************
                    SET functions
********************************************************************/   

public function setSMTPParam($param) {
    /*
        $param can be a text string 'default' or an array.

        $param = array( 'SMTPDebug'     =>null
                        ,'Debugoutput'  =>null 
                        ,'Host'         =>null 
                        ,'Port'         =>null 
                        ,'SMTPSecure'   =>null 
                        ,'SMTPAuth'     =>null 
                        ,'Username'     =>null 
                        ,'Password'     =>null                          
                        );
    */

    try{
        $this->processLog[] = __FUNCTION__;

        $paramError = 0;
        $result = false;


        if($param=='default'){
            $param = array( 'SMTPDebug'         =>0
                            ,'Debugoutput'      =>'html'
                            ,'Host'             =>'smtp-relay.gmail.com'    //smtp.gmail.com OR smtp-relay.gmail.com
                            ,'Port'             =>587
                            ,'SMTPSecure'       =>'tls'
                            ,'SMTPAuth'         =>true
                            ,'Username'         =>"myEmail@email.com"
                            ,'Password'         =>"myPassword"                          
                        );

        }else if(!is_array($param)){
            throw new Exception('ERROR1901155: SMTP parameter is not an array.');
        }

        //Validation - 2019.01.17 need more work, script randomly fails check here.
        foreach($param as $parameter){
            if($parameter===''  OR is_null($param)){
                $paramError++;
            }
        }


        if($paramError==0){
            $this->SMTPDebug        = $param['SMTPDebug'];
            $this->Debugoutput      = $param['Debugoutput'];
            $this->Host             = $param['Host'];
            $this->Port             = $param['Port'];
            $this->SMTPSecure       = $param['SMTPSecure'];
            $this->SMTPAuth         = $param['SMTPAuth'];
            $this->Username         = $param['Username'];
            $this->Password         = $param['Password'];

            $this->phpMailer = new PHPMailer;
            $this->phpMailer->isSMTP();

            $this->phpMailer->SMTPDebug     = $this->SMTPDebug;
            $this->phpMailer->Debugoutput   = $this->Debugoutput;
            $this->phpMailer->Host          = $this->Host;
            $this->phpMailer->Port          = $this->Port;
            $this->phpMailer->SMTPSecure    = $this->SMTPSecure;
            $this->phpMailer->SMTPAuth      = $this->SMTPAuth;
            $this->phpMailer->Username      = $this->Username;
            $this->phpMailer->Password      = $this->Password;

            $result = true;
        }

        return $result;

    }catch(Exception $e){
        $this->processLog[] = __FUNCTION__.$e->getMessage();
        throw $e;
    }
}   



public function setupEmail($data) {
    try{
        $this->processLog[] = __FUNCTION__;



        $this->sentFrom     = $data['sentFrom'];
        $this->phpMailer->setFrom($this->sentFrom[0],$this->sentFrom[1]);

        $this->replyTo      = $data['replyTo'];
        $this->phpMailer->addReplyTo($this->replyTo[0],$this->replyTo[1]);

        $this->emailTo      = $data['emailTo'];
        $this->phpMailer->addAddress($this->emailTo[0],$this->emailTo[1]);

        $this->subject      = $data['subject'];
        $this->phpMailer->Subject = $this->subject;

        $this->body         = $data['body'];
        $this->phpMailer->msgHTML($this->body);

    }catch(Exception $e){
        $this->processLog[] = __FUNCTION__.$e->getMessage();
        throw $e;
    }
}   

public function send() {
    try{
        $this->processLog[] = __FUNCTION__;
        $result = false;
        if (!$this->phpMailer->send()) {
            // throw new exception("error20190114: Failed to send at final stage.");
            $result = false;
        }else{
            $result = true;
        }

        return $result;


    }catch(Exception $e){
        $this->processLog[] = __FUNCTION__.$e->getMessage();
        throw $e;
    }
}   

public function sendWithRetry($attempt=3,$waitTime=5) {
    try{
        $this->processLog[] = __FUNCTION__;
        $result = false;

        for($i=1;$i<=$attempt;$i++){

            if(!$this->phpMailer->send()){
                if($i==$attempt){
                    break;
                }else{
                    continue;
                }

            }else{
                $result=true;
                break;
            }
        }

        return $result;

    }catch(Exception $e){
        $this->processLog[] = __FUNCTION__.$e->getMessage();
        throw $e;
    }
}   

}

?>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-19 07:11:18

如果您不显示代码,我不确定您如何期望任何人能够调试您的代码,但我想您可能不知道addAddress做了什么(它不是出于某种原因而被称为setAddress ),而且您可能也没有在发送循环中调用clearAddresses

您的发送速度也很差;我希望发送100条消息的时间不会超过几秒钟。

看看PHPMailer提供的邮件列表示例,它解决了所有这些问题。性能建议也可以在PHPMailer项目wiki上找到。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54263337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档