通过phpmailer发送电子邮件时,IF语句上的PHP异常行为具有IF和ELSE的输出

如何解决通过phpmailer发送电子邮件时,IF语句上的PHP异常行为具有IF和ELSE的输出

我要呈现的脚本很难理解,但是下面我将解释基本概念。

<?php

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require_once 'apps/PHPMailer/src/Exception.php';
require_once 'apps/PHPMailer/src/PHPMailer.php';
require_once 'apps/PHPMailer/src/SMTP.php';

include_once('apps/HTMLDOMParser/simple_html_dom.php');

require_once("../db.php");

function clear($var) {
    $var = NULL;
    unset($var);
}

// Time when emails were sent last time
$result_laststamp = $conn->prepare("SELECT sent FROM nl_campaigns_emails ORDER BY sent DESC LIMIT 1",array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$result_laststamp->execute();
if($result_laststamp->rowCount() > 0) {
    while($row_laststamp = $result_laststamp->fetch(PDO::FETCH_ASSOC)) {

        // data for first qeued campaign
        $result_campaign = $conn->prepare("SELECT id,name,text,amount,period,smtp,user FROM nl_campaigns WHERE status='1' ORDER BY id ASC LIMIT 1",array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $result_campaign->execute();
        if($result_campaign->rowCount() > 0) {

            while($row_campaign = $result_campaign->fetch(PDO::FETCH_ASSOC)) {

                // SMTP Data
                if($row_campaign['smtp']==1 && $row_campaign['user']>0) {

                    $result_smtp = $conn->prepare("SELECT * FROM admin_logins WHERE id=:id LIMIT 1",array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
                    $result_smtp->execute(array(":id"=>$row_campaign['user']));
                    if($result_smtp->rowCount() > 0) {

                        while($row_smtp = $result_smtp->fetch(PDO::FETCH_ASSOC)) {

                            // Check if its time to send next group of  emails
                            if($row_laststamp['sent'] < (time()-($row_campaign['period']*3600))) {

                                // get email addresses,send,and edit timestmap in db when they were sent
                                $result_emails = $conn->prepare("SELECT id,email FROM nl_campaigns_emails WHERE (id_campaign=:id_campaign AND sent='0') ORDER BY id ASC LIMIT ".$row_campaign['amount'],array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
                                $result_emails->execute(array(":id_campaign"=>$row_campaign['id']));
                                if($result_emails->rowCount() > 0) {
                                    $array_email_ids = array();
                                    while($row_emails = $result_emails->fetch(PDO::FETCH_ASSOC)) {

                                        try {
                                            $mail = new PHPMailer(true);

                                            $mail->CharSet = 'UTF-8';                                   // UTF8 Encoding
                                            $mail->Encoding = 'base64';                                 // Needs to be set with UTF8
                                            //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                        // Enable verbose debug output
                                            $mail->isSMTP();                                            // Send using SMTP
                                            $mail->Host       = "smtp.somedomain.com";                 // Set the SMTP server to send through
                                            $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
                                            $mail->Username   = "noreply@somedomain.cz";                     // SMTP username
                                            $mail->Password   = "somepassword";                // SMTP password
                                            $mail->SMTPSecure = "tls";
                                            $mail->Port       = 587;                 // TCP port to connect to,use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

                                            //Sender
                                            $mail->setFrom($row_smtp['email'],'somename');
                                            $mail->addReplyTo('info@somedomain.cz','somename');
                                            $mail->Sender = 'noreply@somedomain.cz';

                                            // Recipient
                                            $mail->addAddress($row_emails['email']);


                                            // Attachments
                                            //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
                                            //$mail->addAttachment('/tmp/image.jpg','new.jpg');    // Optional name

                                            $bottom_text  = "<p align=center style=\"font-size:11px; font-family:Arial; color:#8b8b8b;\">This email was sent to address <a href=\"mailto:".$row_emails['email']."\">".$row_emails['email']."</a>.<br>";
                                            $bottom_text .= "To unsubscribe you can click to <a href=\"http://www.somedomain.cz/unsubscribe.php?email=".$row_emails['id']."\">unsubscribe</a>.</p>";
                                            $bottom_text .= "<img src=\"http://www.somedomain.cz/view.php?id_campaign=".$row_campaign['id']."&id_email=".$row_emails['id']."\" style=\"visibility: hidden;\">";

                                            // YOUTUBE LINK
                                            $text = new simple_html_dom();
                                            $text->load($row_campaign['text']);
                                            foreach($text->find('iframe') as $element) {
                                                $youtubeid = substr($element->src,strrpos($element->src,"/")+1);
                                                // Create image instances
                                                $video = imagecreatefromjpeg('https://img.youtube.com/vi/'.$youtubeid.'/maxresdefault.jpg');
                                                $button = imagecreatefrompng('../images/youtube_play.png');
                                                $video = imagescale($video,$element->width,$element->height);
                                                $button = imagescale($button,imagesx($video)/7.5);
                                                    
                                                imagecopy($video,$button,imagesx($video)/2-imagesx($button)/2,imagesy($video)/2-imagesy($button)/2,imagesx($button),imagesy($button));

                                                // Output and free from memory
                                                imagejpeg($video,"../images/campaigns/videos/".$youtubeid.".jpg",95);
                                                imagedestroy($video);
                                                imagedestroy($button);
                                                unset($video);
                                                unset($button);
                                                    
                                                $element->outertext = '<a href="https://www.youtube.com/watch?v='.$youtubeid.'" target="_blank"><img src="http://www.somedomain.cz/images/campaigns/videos/'.$youtubeid.'.jpg" width="'.$element->width.'" height="'.$element->height.'"></a>';
                                            }
                                            $row_campaign['text'] = (string)$text;
                                            $text->clear(); 
                                            clear($text);

                                            // URL LINK FOR CLICKER ANALYZER
                                            $doc = new simple_html_dom();
                                            $doc->load($row_campaign['text']);
                                            foreach ($doc->find('a') as $a) {
                                                $a->href = 'http://www.somedomain.cz/clicker.php?id_campaign='.$row_campaign["id"].'&id_email='.$row_emails["id"].'&url='.urlencode($a->href);
                                            }
                                            $row_campaign['text'] = (string)$doc;
                                            $doc->clear(); 
                                            clear($doc);

                                            // Content
                                            $mail->isHTML(true);                                  // Set email format to HTML
                                            $mail->Subject = $row_campaign['name'];
                                            $mail->Body    = $row_campaign['text'].$bottom_text;
                                            $mail->AltBody = strip_tags($row_campaign['text']);

                                            // MessageID
                                            $data = array(
                                                'campaign_id' => str_pad($row_campaign['id'],6,'0',STR_PAD_LEFT),'email_id' => str_pad($row_emails['id'],11,'sent' => time(),);
                                            $messageId = '<'.base64_encode(json_encode($data)).'@www.somedomain.cz>';
                                            $mail->MessageID = $messageId;
                                            $mail->addCustomHeader('In-Reply-To',$messageId);
                                            clear($data);

                                            // SEND Email
                                            $mail->send();
                                        }
                                        catch (Exception $e) {
                                            echo "Emails were not send because: {$mail->ErrorInfo}";
                                            die();
                                        }

                                        // ADD sent email to array
                                        array_push($array_email_ids,$row_emails['id']);

                                        // UPDATE the "sent" stamp
                                        $update = $conn->prepare("UPDATE nl_campaigns_emails SET sent=:sent WHERE id=:id");
                                        try { $update->execute(array(":id"=>$row_emails['id'],":sent"=>time())); }
                                        catch(Exception $error) { die($error->getMessage()); }
                                        clear($update);

                                        clear($mail);
                                    }
                                    clear($result_emails);

                                    // CHECK IF STATUS COULD BE CLOSED
                                    $result_emails_sent = $conn->prepare("SELECT id FROM nl_campaigns_emails WHERE (id_campaign=:id_campaign AND sent='0') ORDER BY id",array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
                                    $result_emails_sent->execute(array(":id_campaign"=>$row_campaign['id']));
                                    if($result_emails_sent->rowCount() == 0) {
                                        $update = $conn->prepare("UPDATE nl_campaigns SET status=:status WHERE id=:id");
                                        try { $update->execute(array(":status"=>3,":id"=>$row_campaign['id'])); }
                                        catch(Exception $error) { die($error->getMessage()); }
                                        clear($update);
                                    }
                                    clear($result_emails_sent);

                                    // Print the result
                                    echo "<div style=\"color:#1b8d3d; text-align:center; font-size:25px; font-family:Arial;\">Sent total: ".count($array_email_ids)." emails.</div>";
                                    echo "<center><a href=emailing.php style=\"color:black; text-align:center; font-size:25px; font-family:Arial; text-decoration:none;\">REFRESH</a></center>";
                                }
                            }
                            else {
                                echo "<div style=\"color:#1b8d3d; text-align:center; font-size:25px; font-family:Arial;\">Next emails can be send at: ".date("j.n.Y H:i:s",$row_laststamp['sent']+($row_campaign['period']*3600))."</div>";
                                echo "<center><a href=emailing.php style=\"color:black; text-align:center; font-size:25px; font-family:Arial; text-decoration:none;\">REFRESH</a></center>";
                                die();
                            }
                        }

                    }
                    else { die("error resolving SMTP"); }
                    clear($result_smtp);
                }

            }
            clear($result_campaign);
        }
        else {
            echo "<div style=\"color:#1b8d3d; text-align:center; font-size:25px; font-family:Arial;\">No campaign is running</div>";
            echo "<center><a href=emailing.php style=\"color:black; text-align:center; font-size:25px; font-family:Arial; text-decoration:none;\">REFRESH</a></center>";
            die();
        }
    }
}
clear($result_laststamp);
clear($conn);

?>

该脚本看起来非常庞大,但最终它的逻辑很简单:

  1. 获取上次发送电子邮件的时间(例如$ lastTimeSent)
  2. 获取打开的排队活动的数据(例如$ dataCampaign)
  3. 获取用于发送电子邮件的SMTP数据(例如$ SMTP)
  4. 检查当前时间戳记time()是否大于$ lastTimeSent + allowed_period(因为我允许每20分钟发送200封电子邮件)
  5. 循环phpmailer,分别发送电子邮件(每次添加一封电子邮件发送)

问题是,如果我在不允许的时间段内运行脚本(因此我没有遵循20分钟内发送200封电子邮件的规则),则一切正常,脚本会将其评估为false:

if($row_laststamp['sent'] < (time()-($row_campaign['period']*3600)))
$row_laststamp['sent'] is the timestmap of last email sent ($lastTimeSent)
$row_campaign['period'] is allowed period to send the email (value is actually 0.33 as in hours its 20 minutes),and im multiplying it by 3600 to recalculate to seconds so I can compare
  • 因此,从根本上讲,它仅会跳出并执行ELSE语句中的操作,因为它具有逻辑性和良好的行为

但是,当im超出周期,因此同一条语句的评估结果为TRUE时,就会发生非常奇怪的行为。 该脚本将按其应有的状态循环并发送电子邮件,但是实际上不是发送200封电子邮件(其值为$ row_campaign ['amount']的值),而是实际发送64封(有时65封电子邮件),然后它不会 回显已发送总计:“。count($ array_email_ids)。” 电子邮件应符合逻辑,但还会再次显示ELSE输出* 下一封电子邮件可以发送至:“。date(“ jnY H:i:s“,$ row_laststamp ['sent'] +($ row_campaign ['period'] 3600))。

所以我正在处理它立即显示IF和ELSE输出的问题... 换句话说,当语句* if($ row_laststamp ['sent'] 3600))) 为TRUE时,它将部分运行try {}的循环(200次中只有65次),然后仍然显示ELSE {} ...

但是!!!如果我删除/注释$ mail-> send()行;然后所有的作品按预期! (当然,除非发送电子邮件)。

我正在检查内存状态,如果我没有走出内存,发现最终循环的最大内存约为24MB(当limnit设置为256MB时),设置120s时执行时间也约为10秒。

因此,基本上,每次只发送一次时,PHPMailer不会循环发送200封以上的电子邮件吗?

我不介意将它们全部发送在一起而不是循环发送,但是我不希望在电子邮件的“ TO”标头中看到它们,如果我改为使用密件抄送,那么(至少在gmail中)它将跳转为垃圾邮件

有什么想法吗?

编辑: 我简化了脚本,根据https://github.com/PHPMailer/PHPMailer/wiki/Sending-to-lists进行了调整 我也确实放置了调试器 $ mail-> SMTPDebug = SMTP :: DEBUG_SERVER; 以查看发生了什么,并且有趣的是,从客户端到服务器的消息突然停止在中间...服务器无响应,因此传输仅在中间停止... 最后一行甚至还没有结束,只是: 2020-09-02 17:06:40客户端->服务器:NTI1MjUyNTNGaWRfY2FtcGFpZ24lMjUyNTI1MjUyNTI1MjUyNTI1MjUyNTI1MjUyNTI1MjU 因此通信只是无缘无故地停止,没有服务器回复它是否已发送,什么都没有... 有人有主意吗?

解决方法

所以最后的错误确实是我没有在每个send()之后清除收件人, 因此,第一封电子邮件已发送给1个收件人,并且每个循环都添加了1个,但之前的循环未清除... 每次循环解决问题后清除收件人

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res