我已经创建了一个脚本,通过电子邮件向每个已发送的客户订单发送一些跟踪细节。
该脚本由PHPMailer构建,并通过Google中继服务发送电子邮件。
在测试期间,所有这些都按预期的方式工作。加载测试的100封电子邮件,尽管相同的电子邮件地址,所有执行的预期。没有重复或丢失的电子邮件。
然而,在直播后不久,有人告诉我,几个客户已经收到了数十封电子邮件,其他客户跟踪细节。
对于gmail用户,他们收到另一个客户的跟踪电子邮件,并将他们的电子邮件地址发送到。
对于outlook用户,他们会收到其他客户的电子邮件,在发送到的区域中会有许多电子邮件。
我已经检查了代码,测试并检查了每个函数的输出,完全失去了它可能出错的地方。
下面是脚本的sudo过程。想看看是否有人可以提供一些提示,我可以如何进行疑难解答这个问题。
其他注释
脚本由windows任务调度程序触发。
该脚本将运行大约2-10分钟,取决于重试次数,尽管调度程序设置为每5分钟运行一次,但是调度器中的任务已被设置为如果已在运行,则不会运行多个任务实例。
任何建议都会很好。
干杯
代码片段。希望能提供足够的想法。
一般过程是脚本从sendTrackingEmails()调用Dispatcher_cl.php,然后使用来自MyCustomerMailer.php的各种函数。MyCustomerMailer.php或多或少是PHPMailer的一个抽象层。
Dispatcher_cl.php
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
<?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;
}
}
}
?>
发布于 2019-01-19 07:11:18
如果您不显示代码,我不确定您如何期望任何人能够调试您的代码,但我想您可能不知道addAddress
做了什么(它不是出于某种原因而被称为setAddress
),而且您可能也没有在发送循环中调用clearAddresses
。
您的发送速度也很差;我希望发送100条消息的时间不会超过几秒钟。
看看PHPMailer提供的邮件列表示例,它解决了所有这些问题。性能建议也可以在PHPMailer项目wiki上找到。
https://stackoverflow.com/questions/54263337
复制相似问题