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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题