[英]php - phpMailer Cron add's an email to the queue while it shouldn't
我有一个脚本,该脚本会定期(每隔约5分钟)从API请求一堆数据,并可能发送电子邮件。
但是我最近与服务器管理员联系,发现有大量排队的邮件由于Cron而永远不会发送。
但是,从目前的情况来看,它永远不要发送电子邮件,因为它永远不要通过放置邮件代码的if语句。
脚本或多或少会执行两次相同的操作,但是会发送一些不同的电子邮件:
/* Paging - Every ~15 minutes, during non-working-times, for all dashboards that have pagerservice enabled. */
$queryPagerservice = mysqli_query($dbcon, "SELECT `id`, `text` FROM `dashboard` WHERE `pagerservice`=true AND (`last_pager` < NOW() - INTERVAL ".PAGER_INTERVAL." MINUTE OR `last_pager` IS NULL)");
$timeNow = date("Gi");
while ($pagerservice = mysqli_fetch_array($queryPagerservice, MYSQLI_ASSOC)) {
echo '1.'; //Does the script hit this code?
$issues = new Issues($pagerservice['id'], 'all', $dbcon);
$array = $issues->getIssues();
if ((count($array['aaData']) > 0) && ($timeNow > WORK_START && $timeNow < WORK_END)) {
mysqli_query($dbcon, "UPDATE `dashboard` SET `last_pager`=NOW() WHERE `id`='".$pagerservice['id']."'");
$date = date('d-m-Y H:i:s');
$message = "There are ".count($array['aaData'])." problems in '".$pagerservice['text']."'.";
echo '2.'; //Does the script hit this code?
require_once('phpmailer/PHPMailerAutoload.php');
$pagerMail = new PHPMailer;
$pagerMail->isSMTP();
$pagerMail->Host = MAILSERVER_ADDRESS;
$pagerMail->Port = MAILSERVER_PORT;
$pagerMail->setFrom('pagerservice@example.com', 'EXAMPLE Pager');
$pagerMail->addReplyTo('noreply@example.com', 'No Reply');
$pagerMail->addAddress(PAGE_EMAIL, 'pagerservice');
$pagerMail->addAddress(PAGER_PHONE.'@'.PAGER_PROVIDER, 'pagerservice');
$pagerMail->Subject = 'pagerservice';
$pagerMail->Body = $message;
$pagerMail->AltBody = $message;
$pagerMail->send();
}
}
/* Notifications - Every ~15 minutes, during working hours, for all dashboards that have notifications enabled. */
$queryNotification = mysqli_query($dbcon, "SELECT `id`, `text` FROM `dashboard` WHERE `notification`=true AND (`last_notification` < NOW() - INTERVAL ".NOTIF_INTERVAL." MINUTE OR `last_notification` IS NULL)");
$timeNow = date("Gi");
while ($notifications = mysqli_fetch_array($queryNotification, MYSQLI_ASSOC)) {
echo '3.'; //Does the script hit this code?
$issues = new Issues($notifications['id'], 'all', $dbcon);
$array = $issues->getIssues();
if ((count($array['aaData']) > 0) && ($timeNow > WORK_START && $timeNow < WORK_END)) {
mysqli_query($dbcon, "UPDATE `dashboard` SET `last_notification`=NOW() WHERE `id`='".$notifications['id']."'");
$date = date('d-m-Y H:i:s');
$message = "(Notif) There are ".count($array['aaData'])." problems in '".$pagerservice['text']."'.";
echo '4.'; //Does the script hit this code?
require_once('phpmailer/PHPMailerAutoload.php');
$notifMail = new PHPMailer;
$notifMail->isSMTP();
$notifMail->Host = MAILSERVER_ADDRESS;
$notifMail->Port = MAILSERVER_PORT;
$notifMail->setFrom('notifications@example.com', 'EXMAPLE Notificator');
$notifMail->addReplyTo('noreply@example.com', 'No Reply');
$notifMail->addAddress(NOTIF_EMAIL, 'notifications');
$notifMail->Subject = 'notification';
$notifMail->Body = $message;
$notifMail->AltBody = $message;
$notifMail->send();
}
}
尝试修复:我将require_once()
调用移至if语句内。 这没有解决。
脚本中没有其他与发送电子邮件有关的代码。 而且,与电子邮件相关的代码也不会执行(如未回显1.
, 2.
, 3.
或1.
的事实所示)。
我正在寻找有关什么可能导致cron脚本将SMTP服务器从未发送的电子邮件排队的提示。
Cron作业没有用户界面,因此,如果脚本将任何输出输出到stderror或stdoutput,它将导致cron系统生成一封电子邮件,以包含该输出。 这就是您如何知道自己的cron出现问题,或者如何监视“我在运行时做了10件事”之类的事情。
在我看来,好像您的脚本有某种输出,要么报告错误,要么执行echo '1.'
调试代码或类似的东西。
首先,我将检查您说的所有代码是否正在运行,例如是否存在echo
或print
等内容,以及通常会将信息写入终端的所有内容,然后删除/重新考虑它。
然后更改cron的配置,将这些电子邮件发送到您实际监视的有效电子邮件地址,这样您就可以知道脚本何时试图告诉您一些信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.