Mail review stage 8: Admin > Ignored Senders lists addresses and domains whose email the ticket mailbox sets aside with no ticket, reply, answer or Unprocessed Email entry, a domain taking its subdomains, each counting what it set aside and saving one that takes a contact's or client's email saying so; Ignore Sender in Unprocessed Email ignores the address or its domain and dismisses the others waiting, leaving contacts, client domains and public domains to an admin; Try an Email has the step; every queued email gets its own Message-ID and invoice and quote emails say what they are about, so a bounce marks the email it came back about as Bounced with the mail server's reason in a new Mail Queue tab, on the ticket reply for those it names and in the invoice or quote history, matched by address and subject when the report has no Message-ID; .gitignore leaves out uploads/mail_unprocessed and uploads/kb (database 4.3.5); 26.12.3 changelog

This commit is contained in:
johnnyq
2026-12-01 00:06:27 -05:00
parent 812c4ae9c2
commit d2d9732efb
31 changed files with 878 additions and 26 deletions

View File

@@ -20,6 +20,8 @@
* cleared as it goes - and a failed or cancelled one like it a day after
* it was queued (nightly, mailQueueClearSensitive())
* 4 cancelled by an admin - never picked up again
* A sent email that came back - a mail server's bounce for it (mailQueueBounced(), 4.3.5) - is Bounced: still 3, with
* email_bounced_at and email_bounce, who it bounced for and why ({"address": "why"}).
* email_error says why it last failed, or who cancelled it - cleared when it
* is sent or retried. Sent, failed and cancelled email is deleted after 90
* days (nightly).
@@ -181,6 +183,78 @@ function mailQueuePaceReady() {
return $ready;
}
/** Whether the 4.3.5 update has run - each email keeps its Message-ID and whether it bounced. */
function mailQueueBounceReady() {
global $mysqli;
static $ready = null;
if ($ready === null) {
$version = defined('CURRENT_DATABASE_VERSION') ? CURRENT_DATABASE_VERSION
: (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1"))[0] ?? '0');
$ready = version_compare($version, '4.3.5', '>=');
}
return $ready;
}
/** The SELECT columns for whether an email bounced - NULL before the 4.3.5 update. With its leading comma. */
function mailQueueBounceColumns() {
return mailQueueBounceReady() ? ', email_bounced_at, email_bounce' : ', NULL AS email_bounced_at, NULL AS email_bounce';
}
/*
* A mail server's bounce (addBounce(), cron/ticket_email_parser.php) for the email it was about - found by its Message-ID
* ($message_id, cleaned), else the latest sent in the last week to one of the addresses it failed for, with its subject
* ($subject, raw) - only ones that went (Sent). $failed is [address => why]. Marked Bounced, for those addresses - and a
* billing email's history says so. The row as it was, or null when none is found.
*/
function mailQueueBounced($message_id, array $failed, $subject = '') {
global $mysqli;
if (!mailQueueBounceReady()) {
return null;
}
$columns = "email_id, email_recipient, email_cc, email_subject, email_about, email_ticket_reply_id, email_bounce";
$row = null;
if (trim((string) $message_id) !== '') {
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT $columns FROM email_queue WHERE email_status = 3 AND email_message_id = '" . mysqli_real_escape_string($mysqli, (string) $message_id) . "' ORDER BY email_id DESC LIMIT 1"));
}
if (!$row && $failed && trim((string) $subject) !== '') {
$addresses = implode(',', array_map(function ($address) use ($mysqli) {
return "'" . mysqli_real_escape_string($mysqli, strtolower((string) $address)) . "'";
}, array_keys($failed)));
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT $columns FROM email_queue WHERE email_status = 3 AND email_sent_at > NOW() - INTERVAL 7 DAY
AND email_subject = '" . mysqli_real_escape_string($mysqli, emailFit((string) $subject)) . "' AND LOWER(email_recipient) IN ($addresses) ORDER BY email_id DESC LIMIT 1"));
}
if (!$row) {
return null;
}
// Who it bounced for - the addresses it was to that the report names, or everyone on it when it names none
$on_it = array_merge([strtolower((string) $row['email_recipient'])], array_map(function ($person) {
return strtolower((string) $person['email']);
}, mailQueueCc($row['email_cc'] ?? '')));
$bounce = (array) json_decode((string) ($row['email_bounce'] ?? ''), true);
foreach ($failed ?: array_fill_keys($on_it, '') as $address => $why) {
$address = strtolower((string) $address);
if (in_array($address, $on_it, true)) {
$bounce[$address] = mailQueueErrorText($why);
}
}
if (!$bounce) {
$bounce = array_fill_keys($on_it, '');
}
mysqli_query($mysqli, "UPDATE email_queue SET email_bounced_at = NOW(), email_bounce = '" . mysqli_real_escape_string($mysqli, json_encode($bounce, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) . "'
WHERE email_id = " . intval($row['email_id']));
// A billing email - its history says it bounced
if (preg_match('/^(invoice|quote):(\d+)$/', (string) $row['email_about'], $about) && function_exists('billingLog')) {
$status = (string) (mysqli_fetch_row(mysqli_query($mysqli, $about[1] === 'invoice'
? "SELECT invoice_status FROM invoices WHERE invoice_id = " . intval($about[2]) : "SELECT quote_status FROM quotes WHERE quote_id = " . intval($about[2])))[0] ?? '');
if ($status !== '') {
$why = implode('; ', array_filter($bounce));
billingLog($about[1], intval($about[2]), $status, 'Email to ' . implode(', ', array_keys($bounce)) . ' bounced' . ($why !== '' ? ' - ' . mb_strimwidth($why, 0, 300, '...') : ''));
}
}
return $row;
}
/* The most emails the queue sends a minute (Admin > Settings > Mail, Sending) - 30 before the 4.2.9 update. */
function mailQueuePerMinute() {
global $mysqli;
@@ -267,7 +341,8 @@ function mailQueueTabs() {
'sending' => ['Sending', 'fa-paper-plane', 'email_status = 1'],
'retrying' => ['Retrying', 'fa-redo-alt', mailQueueRetryingSql()],
'failed' => ['Failed', 'fa-exclamation-circle', mailQueueFailedSql()],
'sent' => ['Sent', 'fa-check', 'email_status = 3'],
'sent' => ['Sent', 'fa-check', 'email_status = 3' . (mailQueueBounceReady() ? ' AND email_bounced_at IS NULL' : '')],
] + (mailQueueBounceReady() ? ['bounced' => ['Bounced', 'fa-reply', 'email_status = 3 AND email_bounced_at IS NOT NULL']] : []) + [
'cancelled' => ['Cancelled', 'fa-ban', 'email_status = ' . MAIL_QUEUE_CANCELLED],
];
}
@@ -280,8 +355,13 @@ function mailQueueTabs() {
*/
function mailQueueState(array $row) {
$state = mailQueueStateOf($row);
// Why it failed, or who cancelled it - not once it has gone, or while it waits again
// Why it failed, or who cancelled it - not once it has gone, or while it waits again. Bounced: what the mail server said, for whom
$state['error'] = in_array($state['key'], ['retrying', 'failed', 'cancelled'], true) ? mailQueueErrorText($row['email_error'] ?? '') : '';
if ($state['key'] === 'bounced') {
$state['error'] = mailQueueErrorText(implode('; ', array_map(function ($address, $why) {
return $why !== '' ? "$address: $why" : $address;
}, array_keys($state['bounce']), $state['bounce'])));
}
return $state;
}
@@ -317,6 +397,12 @@ function mailQueueStateOf(array $row) {
'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 && !empty($row['email_bounced_at'])) {
$bounced = (string) $row['email_bounced_at'];
return ['key' => 'bounced', 'label' => 'Bounced', 'badge' => 'text-bg-danger', 'icon' => 'fa-reply',
'detail' => 'Came back ' . timeAgo($bounced), 'title' => 'Sent, then bounced ' . $when($bounced),
'bounce' => (array) json_decode((string) ($row['email_bounce'] ?? ''), true)];
}
if ($status === 3) {
$sent = (string) ($row['email_sent_at'] ?? '');
return ['key' => 'sent', 'label' => 'Sent', 'badge' => 'text-bg-success', 'icon' => 'fa-check',