kgongonowdoe
kgongonowdoe

Reputation: 435

php - phpMailer Cron add's an email to the queue while it shouldn't

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('[email protected]', 'EXAMPLE Pager');
        $pagerMail->addReplyTo('[email protected]', '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('[email protected]', 'EXMAPLE Notificator');
        $notifMail->addReplyTo('[email protected]', '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.

Upvotes: 0

Views: 559

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94642

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.

Upvotes: 1

Related Questions