doudou8893 2016-05-12 08:31
浏览 25
已采纳

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

I have a script that periodically (every ~5 minutes) requests a bunch of data from an API and possibly sends a email.

However I recently got contacted by the server administrator that there a huge amount of queued mails which will never be sent because of the Cron.

However as it stands right now it should never send the emails because it should never pass the if-statements in which the mailing code is placed.

Script more or less does the same thing twice, but with some different emails:

/* 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

Attempted fix: I moved the require_once() call to within the if-statement. This didn't fix it.

There is no other code in the script that is in any way related to sending email's. And the code that is e-mail related isn't executed (as shown by the fact that neither 1., 2., 3. nor 1. is echoed).

I am looking for any tips as to what can cause the cron script to queue an email that is never sent by the SMTP server.

  • 写回答

1条回答 默认 最新

  • dongqian2021 2016-05-12 08:58
    关注

    Cron jobs have no user interface, so if the script outputs anything to the stderror or stdoutput it will cause the cron system to generate an email, to contain that output. This is how you would know your cron is having problems, or how you monitor things like "I did 10 things this run" type of stuff.

    It looks to me as if your script has some kind of output either reporting errors or doing your echo '1.' debugging code or something similiar.

    First I would check all the code you say it is running for anything like echo or print etc etc, anything that would normally write info to the terminal, and remove/rethink it.

    Then change the cron's config to send these emails to a valid email address that you actually monitor, so you know when your script is trying to tell you something.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统