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:
johnnyq
2026-11-30 17:32:30 -05:00
parent 17d2611b60
commit 039c1b11da
3 changed files with 79 additions and 33 deletions

View File

@@ -211,7 +211,8 @@ class MailQueueSmtp extends SMTP {
$accepted = parent::recipient($address, $dsn);
if (!$accepted) {
$error = $this->getError();
$this->refused[strtolower(trim((string) $address))] = ['code' => (string) ($error['smtp_code'] ?? ''), 'detail' => trim(trim((string) ($error['smtp_code_ex'] ?? '')) . ' ' . trim((string) ($error['detail'] ?? '')))];
$this->refused[strtolower(trim((string) $address))] = ['code' => (string) ($error['smtp_code'] ?? ''), 'code_ex' => trim((string) ($error['smtp_code_ex'] ?? '')),
'detail' => trim(trim((string) ($error['smtp_code_ex'] ?? '')) . ' ' . trim((string) ($error['detail'] ?? '')))];
}
return $accepted;
}
@@ -464,17 +465,18 @@ if ($orphaned_emails > 0) {
/** =======================================================================
* SEND: what is due - waiting (status 0) and failed ones whose wait is over (status 2) - at most the
* pace a minute (mailQueuePerMinute()), bulk email after everything else, oldest first
* pace a minute (mailQueuePerMinute()): a password link, an invite or a shared login first, as someone
* is waiting for it, bulk email after everything else, oldest first
* ======================================================================= */
// The copied in and the headers - once the 4.2.6 update has made their columns
$thread_columns = ticketMailThreadReady() ? 'email_cc, email_headers' : "'' AS email_cc, '' AS email_headers";
$per_minute = mailQueuePerMinute();
$bulk_order = mailQueuePaceReady() ? 'email_bulk, ' : '';
$bulk_order = (mailQueuePaceReady() ? 'email_bulk, ' : '') . (mailQueueHasSensitive() ? 'email_sensitive DESC, ' : '');
$sql_queue = mysqli_query($mysqli, "SELECT email_attachments, email_attempts, email_cal_str, email_content, email_from, email_from_name, email_id,
email_recipient, email_recipient_name, email_status, email_subject, $thread_columns FROM email_queue
WHERE (email_status = 0 AND email_queued_at <= NOW())
OR (email_status = 2 AND email_attempts < " . MAIL_QUEUE_TRIES . " AND " . mailQueueRetryDueSql() . ")
OR (" . mailQueueRetryingSql() . " AND " . mailQueueRetryDueSql() . ")
ORDER BY {$bulk_order}email_queued_at, email_id LIMIT $per_minute");
$mailer = null;
@@ -553,12 +555,11 @@ while ($sql_queue && ($rowq = mysqli_fetch_assoc($sql_queue))) {
}
continue;
}
$temporary_in_a_row = 0;
// Who the mail server turned away for good (5xx) - their contacts show Email bounced (functions/contact_bounce.php), and no email
// to them is tried again
// Who the mail server said doesn't exist (mailQueueRefusedForGood()) - their contacts show Email bounced (functions/contact_bounce.php),
// and no email to them is tried again. Turned away for any other reason, it is tried again
$for_good = array_filter($result['refused'], function ($why) {
return substr($why['code'], 0, 1) === '5';
return mailQueueRefusedForGood($why['code'], $why['code_ex'] ?? '', $why['detail']);
});
foreach ($for_good as $address => $why) {
foreach (contactBounceMark($address, trim($why['code'] . ' ' . $why['detail'])) as $bounced_contact_id => [$bounced_name, $bounced_client_id]) {
@@ -568,6 +569,7 @@ while ($sql_queue && ($rowq = mysqli_fetch_assoc($sql_queue))) {
}
if ($result['accepted']) {
$temporary_in_a_row = 0;
// Sent - one with a link or code that opens something is cleared now (mailQueueSentSql()). Any of the copied in turned away
// are said with it: it isn't sent again, as everyone else has it
$note = $result['refused'] ? 'Sent - but the mail server turned away ' . mailQueueRefusedText($result['refused']) : null;
@@ -577,16 +579,23 @@ while ($sql_queue && ($rowq = mysqli_fetch_assoc($sql_queue))) {
$run['partial']++;
}
} elseif (count($for_good) === count($result['refused'])) {
// Everyone turned away for good - not tried again
// Everyone turned away for good - not tried again. About the addresses, not the server
$temporary_in_a_row = 0;
mysqli_query($mysqli, "UPDATE email_queue SET email_status = 2, email_failed_at = NOW(), email_attempts = 99" . mailQueueErrorSql('Turned away for good - ' . mailQueueRefusedText($result['refused'])) . " WHERE email_id = $email_id");
logApp("Cron-Mail-Queue", "Error", "The mail server turned away email #$email_id for good - " . escapeSql(mailQueueRefusedText($result['refused'])));
$run['turned_away']++;
} else {
// Turned away for now - tried again after a wait
// Turned away for now - tried again after a wait. Two in a row is the server - a relay that won't send for ITFlow, a quota -
// and the rest wait for the next minute, the admins told (below)
$why = 'Turned away for now - ' . mailQueueRefusedText($result['refused']);
mysqli_query($mysqli, "UPDATE email_queue SET email_status = 2, email_failed_at = NOW(), email_attempts = $attempts" . mailQueueErrorSql($why) . " WHERE email_id = $email_id");
$run['last_error'] = $why;
$attempts >= MAIL_QUEUE_TRIES ? $run['gave_up']++ : $run['again']++;
if (++$temporary_in_a_row >= 2) {
$run['stopped'] = 'The mail server turned away two emails in a row - the rest wait for the next minute. It said: ' . mailQueueErrorText($why);
$run['stopped_soft'] = true;
break;
}
}
}