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();
}
}
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.