php - phpMailer Cron add's an email to the queue while it shouldn't
<p>I have a script that periodically (every ~5 minutes) requests a bunch of data from an API and possibly sends a email.</p>

<p>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.</p>

<p>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.</p>

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

<pre><code>/* 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` &lt; 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-&gt;getIssues();
    if ((count($array['aaData']) &gt; 0) &amp;&amp; ($timeNow &gt; WORK_START &amp;&amp; $timeNow &lt; 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-&gt;isSMTP();
        $pagerMail-&gt;Host = MAILSERVER_ADDRESS;
        $pagerMail-&gt;Port = MAILSERVER_PORT;
        $pagerMail-&gt;setFrom('pagerservice@example.com', 'EXAMPLE Pager');
        $pagerMail-&gt;addReplyTo('noreply@example.com', 'No Reply');
        $pagerMail-&gt;addAddress(PAGE_EMAIL, 'pagerservice');
        $pagerMail-&gt;addAddress(PAGER_PHONE.'@'.PAGER_PROVIDER, 'pagerservice');
        $pagerMail-&gt;Subject = 'pagerservice';
        $pagerMail-&gt;Body = $message;
        $pagerMail-&gt;AltBody = $message;
        $pagerMail-&gt;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` &lt; 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-&gt;getIssues();
    if ((count($array['aaData']) &gt; 0) &amp;&amp; ($timeNow &gt; WORK_START &amp;&amp; $timeNow &lt; 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-&gt;isSMTP();
        $notifMail-&gt;Host = MAILSERVER_ADDRESS;
        $notifMail-&gt;Port = MAILSERVER_PORT;
        $notifMail-&gt;setFrom('notifications@example.com', 'EXMAPLE Notificator');
        $notifMail-&gt;addReplyTo('noreply@example.com', 'No Reply');
        $notifMail-&gt;addAddress(NOTIF_EMAIL, 'notifications');
        $notifMail-&gt;Subject = 'notification';
        $notifMail-&gt;Body = $message;
        $notifMail-&gt;AltBody = $message;
        $notifMail-&gt;send();
    }
}
</code></pre>

<p><a href="https://i.stack.imgur.com/SNNUh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SNNUh.png" alt="picture of queue"></a></p>

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

<p>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 <code>1.</code>, <code>2.</code>, <code>3.</code> nor <code>1.</code> is echoed).</p>

<p>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.</p>