mirror of
https://github.com/itflow-org/itflow
synced 2026-12-02 09:09:01 +00:00
Mail review stage 4: a failed email is retried only while its last try is within a day, so emails given up on under the old four tries stay Failed instead of being sent again and Failed shows their own count of tries; only a recipient address that doesn't exist is turned away for good and marks its contact Email bounced - relay refusals, full mailboxes and other policy refusals are tried again and two in a row stop the run with a notice to admins; a dismissed notice counts toward the hour; password links and invites go first; 26.12.3 changelog
This commit is contained in:
@@ -9,10 +9,11 @@
|
||||
* 0 waiting - or scheduled, while its email_queued_at is still ahead
|
||||
* 1 being sent this minute
|
||||
* 2 failed - tried again after a wait that grows (MAIL_QUEUE_RETRY_AFTER, about eight
|
||||
* hours in all) until it has had MAIL_QUEUE_TRIES; 99 tries means it isn't tried
|
||||
* again - an address the mail server turned away for good. A domain with no mail
|
||||
* server is a failed try like any other (mailQueueDomainProblem()), as a lookup
|
||||
* can fail for a moment
|
||||
* hours in all) until it has had MAIL_QUEUE_TRIES, and not once its last try is
|
||||
* MAIL_QUEUE_GIVE_UP_HOURS behind it (mailQueueRetryingSql()); 99 tries means it isn't
|
||||
* tried again - an address the mail server said doesn't exist (mailQueueRefusedForGood()).
|
||||
* A domain with no mail server is a failed try like any other (mailQueueDomainProblem()),
|
||||
* as a lookup can fail for a moment
|
||||
* 3 sent - it keeps what it said, its calendar file and attachments, except
|
||||
* one that carried a link or code that opens something (email_sensitive:
|
||||
* a shared login, an invite, a password link, a signing or approval link),
|
||||
@@ -29,7 +30,8 @@
|
||||
* copied-in itself.
|
||||
*
|
||||
* The job sends at most config_mail_send_per_minute a run (database 4.2.9 - 30 until then) over one
|
||||
* connection, bulk email (email_bulk) after everything else, and stops for the minute when the mail
|
||||
* connection - one with a link or code that opens something (email_sensitive: a password link, an invite) first,
|
||||
* as someone is waiting for it, and bulk email (email_bulk) after everything else - and stops for the minute when the mail
|
||||
* server turns two away in a row for now - a host's limit - so the rest wait their turn rather than
|
||||
* use up their tries. A mail server it can't reach or sign in to costs no email a try.
|
||||
*/
|
||||
@@ -37,6 +39,9 @@
|
||||
// Minutes before each try again - after the first failed try, the second... about eight hours in all
|
||||
const MAIL_QUEUE_RETRY_AFTER = [5, 10, 20, 40, 60, 120, 240];
|
||||
const MAIL_QUEUE_TRIES = 8;
|
||||
// A failed email isn't picked up again by itself once its last try is this far behind it - the longest wait is 4 hours.
|
||||
// An email given up on before the tries went from 4 to 8 has fewer than 8, and was sent again, however old
|
||||
const MAIL_QUEUE_GIVE_UP_HOURS = 24;
|
||||
const MAIL_QUEUE_KEEP_DAYS = 90;
|
||||
const MAIL_QUEUE_CANCELLED = 4;
|
||||
// Pictures that were in an email as data, kept once each for the queue to attach inline (emailInlineImages()) - the web can't reach uploads/tmp
|
||||
@@ -64,6 +69,40 @@ function mailQueueRetryDueSql() {
|
||||
return "email_failed_at <= NOW() - INTERVAL (CASE email_attempts$cases ELSE $last END) MINUTE";
|
||||
}
|
||||
|
||||
/* SQL - a failed email still being tried: under MAIL_QUEUE_TRIES, and its last try within MAIL_QUEUE_GIVE_UP_HOURS. */
|
||||
function mailQueueRetryingSql() {
|
||||
return "(email_status = 2 AND email_attempts < " . MAIL_QUEUE_TRIES . " AND email_failed_at >= NOW() - INTERVAL " . MAIL_QUEUE_GIVE_UP_HOURS . " HOUR)";
|
||||
}
|
||||
|
||||
/* SQL - a failed email given up on: out of tries, not tried again (99), or its last try too long ago (mailQueueRetryingSql()). */
|
||||
function mailQueueFailedSql() {
|
||||
return "(email_status = 2 AND (email_attempts >= " . MAIL_QUEUE_TRIES . " OR email_failed_at IS NULL OR email_failed_at < NOW() - INTERVAL " . MAIL_QUEUE_GIVE_UP_HOURS . " HOUR))";
|
||||
}
|
||||
|
||||
/* Whether a failed email ($row - email_status, email_attempts, email_failed_at) is still being tried (mailQueueRetryingSql()). */
|
||||
function mailQueueRetrying(array $row) {
|
||||
$failed = (string) ($row['email_failed_at'] ?? '');
|
||||
return intval($row['email_status']) === 2 && intval($row['email_attempts']) < MAIL_QUEUE_TRIES
|
||||
&& $failed !== '' && strtotime($failed) >= time() - MAIL_QUEUE_GIVE_UP_HOURS * 3600;
|
||||
}
|
||||
|
||||
/*
|
||||
* Whether a mail server's refusal of a recipient says the address doesn't exist - for good, so the email isn't tried
|
||||
* again and the contact shows Email bounced. Only the enhanced codes for a recipient's address (RFC 3463): 5.1.1 no
|
||||
* such mailbox, 5.1.2 no such domain, 5.1.3 not an address, 5.1.6 moved with nowhere to forward to, 5.1.10 a domain
|
||||
* that takes no mail (RFC 7505) - or, from a server that gives no enhanced code, 550, 551 or 553 saying there is no
|
||||
* such recipient. Any other refusal is tried again: the server's own policy (5.7.x - a relay that won't send for
|
||||
* you), a full mailbox (5.2.2), a sending quota. Every 5xx used to count, and marked real contacts bounced.
|
||||
*/
|
||||
function mailQueueRefusedForGood($code, $code_ex, $detail) {
|
||||
$code_ex = trim((string) $code_ex);
|
||||
if ($code_ex !== '') {
|
||||
return in_array($code_ex, ['5.1.1', '5.1.2', '5.1.3', '5.1.6', '5.1.10'], true);
|
||||
}
|
||||
return in_array(trim((string) $code), ['550', '551', '553'], true)
|
||||
&& preg_match("/user unknown|unknown user|no such (user|mailbox|recipient|address)|does ?n[o']?t exist|recipient (not found|unknown)|unknown recipient|mailbox not found|invalid (recipient|mailbox)/i", (string) $detail) === 1;
|
||||
}
|
||||
|
||||
/* Whether the 4.2.9 update has run - the sending pace (config_mail_send_per_minute) and bulk email (email_bulk). */
|
||||
function mailQueuePaceReady() {
|
||||
global $mysqli;
|
||||
@@ -124,8 +163,8 @@ function mailQueueTextPart($html) {
|
||||
|
||||
/*
|
||||
* Tell the admins about the mail queue - $text, and where to look ($action). Once an hour at most: an
|
||||
* admin with a notice from the queue that is still there from the last hour isn't told again, as a run
|
||||
* a minute with a mail server down would fill their bell. It used to tell every agent, once an email.
|
||||
* admin told in the last hour isn't told again - read or not, as one dismissed came back the next minute
|
||||
* while a mail server was down. It used to tell every agent, once an email.
|
||||
*/
|
||||
function mailQueueNotifyAdmins($text, $action = '/admin/mail_queue.php') {
|
||||
global $mysqli;
|
||||
@@ -133,7 +172,7 @@ function mailQueueNotifyAdmins($text, $action = '/admin/mail_queue.php') {
|
||||
$action_sql = logSqlText($action, 250);
|
||||
$sql = mysqli_query($mysqli, "SELECT user_id FROM users JOIN user_roles ON role_id = user_role_id
|
||||
WHERE role_is_admin = 1 AND user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL
|
||||
AND user_id NOT IN (SELECT notification_user_id FROM notifications WHERE notification_type = 'Mail Queue' AND notification_dismissed_at IS NULL
|
||||
AND user_id NOT IN (SELECT notification_user_id FROM notifications WHERE notification_type = 'Mail Queue'
|
||||
AND notification_timestamp >= NOW() - INTERVAL 1 HOUR)");
|
||||
$told = 0;
|
||||
while ($row = mysqli_fetch_row($sql)) {
|
||||
@@ -156,13 +195,12 @@ function mailQueueDomainProblem($recipient) {
|
||||
|
||||
/* The tabs of the list - key => [label, icon, SQL for what is in it]. */
|
||||
function mailQueueTabs() {
|
||||
$tries = MAIL_QUEUE_TRIES;
|
||||
return [
|
||||
'' => ['All', 'fa-inbox', '1 = 1'],
|
||||
'waiting' => ['Waiting', 'fa-hourglass-half', 'email_status = 0'],
|
||||
'sending' => ['Sending', 'fa-paper-plane', 'email_status = 1'],
|
||||
'retrying' => ['Retrying', 'fa-redo-alt', "email_status = 2 AND email_attempts < $tries"],
|
||||
'failed' => ['Failed', 'fa-exclamation-circle', "email_status = 2 AND email_attempts >= $tries"],
|
||||
'retrying' => ['Retrying', 'fa-redo-alt', mailQueueRetryingSql()],
|
||||
'failed' => ['Failed', 'fa-exclamation-circle', mailQueueFailedSql()],
|
||||
'sent' => ['Sent', 'fa-check', 'email_status = 3'],
|
||||
'cancelled' => ['Cancelled', 'fa-ban', 'email_status = ' . MAIL_QUEUE_CANCELLED],
|
||||
];
|
||||
@@ -202,15 +240,15 @@ function mailQueueStateOf(array $row) {
|
||||
return ['key' => 'sending', 'label' => 'Sending', 'badge' => 'text-bg-warning', 'icon' => 'fa-paper-plane',
|
||||
'detail' => 'Going now', 'title' => ''];
|
||||
}
|
||||
if ($status === 2 && $tries < MAIL_QUEUE_TRIES) {
|
||||
$next = $failed !== '' ? strtotime($failed) + mailQueueRetryMinutes($tries) * 60 : time();
|
||||
if (mailQueueRetrying($row)) {
|
||||
$next = strtotime($failed) + mailQueueRetryMinutes($tries) * 60;
|
||||
return ['key' => 'retrying', 'label' => 'Retrying', 'badge' => 'text-bg-warning', 'icon' => 'fa-redo-alt',
|
||||
'detail' => 'Tried ' . max(1, $tries) . ' of ' . MAIL_QUEUE_TRIES . ' · next ' . ($next > time() ? 'about ' . date('g:i A', $next) : 'any minute'),
|
||||
'title' => $failed !== '' ? 'Last failed ' . $when($failed) : ''];
|
||||
}
|
||||
if ($status === 2) {
|
||||
return ['key' => 'failed', 'label' => 'Failed', 'badge' => 'text-bg-danger', 'icon' => 'fa-exclamation-circle',
|
||||
'detail' => ($tries > MAIL_QUEUE_TRIES ? 'Not tried again' : 'Gave up after ' . MAIL_QUEUE_TRIES . ' tries') . ($failed !== '' ? ' · ' . timeAgo($failed) : ''),
|
||||
'detail' => ($tries > MAIL_QUEUE_TRIES ? 'Not tried again' : 'Gave up after ' . max(1, $tries) . ' ' . ($tries === 1 ? 'try' : 'tries')) . ($failed !== '' ? ' · ' . timeAgo($failed) : ''),
|
||||
'title' => $failed !== '' ? 'Failed ' . $when($failed) : ''];
|
||||
}
|
||||
if ($status === 3) {
|
||||
@@ -235,7 +273,7 @@ function mailQueueCan(array $row, $action) {
|
||||
// Not one whose link was cleared - there is nothing left to send
|
||||
return ($status === 2 || $status === MAIL_QUEUE_CANCELLED) && empty($row['email_cleared']);
|
||||
case 'cancel':
|
||||
return $status === 0 || ($status === 2 && $tries < MAIL_QUEUE_TRIES);
|
||||
return $status === 0 || mailQueueRetrying($row);
|
||||
case 'delete':
|
||||
return $status !== 1;
|
||||
}
|
||||
@@ -254,11 +292,10 @@ function mailQueueAct($action, array $email_ids) {
|
||||
return [];
|
||||
}
|
||||
$list = implode(',', $ids);
|
||||
$tries = MAIL_QUEUE_TRIES;
|
||||
$cancelled = MAIL_QUEUE_CANCELLED;
|
||||
$where = [
|
||||
'retry' => "email_status IN (2, $cancelled) AND email_content <> ''",
|
||||
'cancel' => "(email_status = 0 OR (email_status = 2 AND email_attempts < $tries))",
|
||||
'cancel' => "(email_status = 0 OR " . mailQueueRetryingSql() . ")",
|
||||
'delete' => 'email_status <> 1',
|
||||
][$action] ?? null;
|
||||
if ($where === null) {
|
||||
@@ -332,7 +369,7 @@ function mailQueueHealth() {
|
||||
return ['level' => 'warning', 'key' => 'slow', 'title' => 'Email is waiting longer than it should', 'text' => trim($held) . ' The Mail Queue job last ran ' . $ago($job['cron_job_last_run_at'] ?? null) . '.' . $error,
|
||||
'url' => 'cron.php', 'action' => 'Maintenance > Cron', 'waiting' => $waiting];
|
||||
}
|
||||
return ['level' => '', 'key' => 'ok', 'title' => '', 'text' => 'Sent by the Mail Queue job each minute, up to ' . mailQueuePerMinute() . ' a minute, bulk email last - it last ran '
|
||||
return ['level' => '', 'key' => 'ok', 'title' => '', 'text' => 'Sent by the Mail Queue job each minute, up to ' . mailQueuePerMinute() . ' a minute - password links and invites first, bulk email last. It last ran '
|
||||
. $ago($job['cron_job_last_run_at'] ?? null) . '. A failed email is tried ' . (MAIL_QUEUE_TRIES - 1) . ' more times over about '
|
||||
. round(array_sum(MAIL_QUEUE_RETRY_AFTER) / 60) . ' hours, and one the mail server turns away for good isn\'t tried again. Email is kept ' . MAIL_QUEUE_KEEP_DAYS . ' days - with what it said, apart from one with a link or code that opens something, cleared once it has gone.', 'url' => '', 'action' => '', 'waiting' => 0];
|
||||
}
|
||||
@@ -529,12 +566,11 @@ function mailQueueSummary() {
|
||||
if ($summary !== null) {
|
||||
return $summary;
|
||||
}
|
||||
$tries = MAIL_QUEUE_TRIES;
|
||||
$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT
|
||||
COALESCE(SUM(email_status = 0 AND email_queued_at <= NOW()), 0),
|
||||
COALESCE(SUM(email_status = 2 AND email_attempts < $tries), 0),
|
||||
COALESCE(SUM(email_status = 2 AND email_attempts >= $tries AND email_failed_at >= NOW() - INTERVAL 1 DAY), 0),
|
||||
COALESCE(SUM(email_status = 2 AND email_attempts >= $tries AND email_failed_at >= NOW() - INTERVAL 7 DAY), 0),
|
||||
COALESCE(SUM(" . mailQueueRetryingSql() . "), 0),
|
||||
COALESCE(SUM(" . mailQueueFailedSql() . " AND email_failed_at >= NOW() - INTERVAL 1 DAY), 0),
|
||||
COALESCE(SUM(" . mailQueueFailedSql() . " AND email_failed_at >= NOW() - INTERVAL 7 DAY), 0),
|
||||
MAX(CASE WHEN email_status = 3 THEN email_sent_at END)
|
||||
FROM email_queue WHERE email_queued_at >= NOW() - INTERVAL " . MAIL_QUEUE_KEEP_DAYS . " DAY"));
|
||||
$summary = ['waiting' => intval($row[0] ?? 0), 'retrying' => intval($row[1] ?? 0), 'failed_day' => intval($row[2] ?? 0), 'failed_week' => intval($row[3] ?? 0), 'last_sent' => $row[4] ?? null];
|
||||
|
||||
Reference in New Issue
Block a user