php - phpMailer Cron添加了一个电子邮件到队列,而它不应该

时间:2016-05-12 08:31:34

标签: php email cron queue phpmailer

我有一个定期(每隔约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();
    }
}

picture of queue

尝试修复:我将require_once()调用移到了if语句中。这没有解决它。

脚本中没有其他任何与发送电子邮件相关的代码。并且不会执行与电子邮件相关的代码(正如1.2.3.1.都未得到回应这一事实所示。

我正在寻找有关什么原因导致cron脚本对从未发送过SMTP服务器的电子邮件进行排队的提示。

1 个答案:

答案 0 :(得分:1)

Cron作业没有用户界面,因此如果脚本向stderror或stdoutput输出任何内容,则会导致cron系统生成电子邮件,以包含该输出。这就是你如何知道你的cron遇到问题,或者你是如何监控的事情,例如&#34;我做了10件这样的事情&#34;东西的类型。

我认为您的脚本有某种输出报告错误或执行FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc]init]; content.contentURL = [NSURL URLWithString:@"http://google.com"]; // Random content.contentTitle = @"Test title"; // Random content.contentDescription = @"Test description"; // Random content.imageURL = [NSURL URLWithString:@"http://www.gettyimages.pt/gi-resources/images/Homepage/Hero/PT/PT_hero_42_153645159.jpg"]; FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init]; dialog.fromViewController = self; dialog.shareContent = content; dialog.delegate = self; dialog.mode = FBSDKShareDialogModeNative; if (![dialog canShow]) { dialog.mode = FBSDKShareDialogModeFeedBrowser; } [dialog show]; FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc]init]; content.contentURL = [NSURL URLWithString:@"http://google.com"]; // Random content.contentTitle = @"Test title"; // Random content.contentDescription = @"Test description"; // Random content.imageURL = [NSURL URLWithString:@"http://www.gettyimages.pt/gi-resources/images/Homepage/Hero/PT/PT_hero_42_153645159.jpg"]; // FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init]; dialog.fromViewController = self; dialog.shareContent = content; dialog.delegate = self; dialog.mode = FBSDKShareDialogModeNative; if (![dialog canShow]) { dialog.mode = FBSDKShareDialogModeFeedBrowser; } [dialog show]; 调试代码或类似的东西。

首先,我会检查您说它正在运行的所有代码,例如echo '1.'echo等等,通常会将信息写入终端的任何内容,并删除/重新考虑它。

然后更改cron的配置,将这些电子邮件发送到您实际监控的有效电子邮件地址,这样您就知道脚本何时试图告诉您某些内容。