Mail queue keeps what a sent email said: only an email with a link or code that opens something - a shared login and its code, a portal or agent invite, a password link, a contract to sign or a task to approve - is cleared once sent, and a day after it was queued if it failed or was cancelled; emailQueue gains a sensitive option set by those senders, cron marks sent email with mailQueueSentSql, nightly runs mailQueueClearSensitive, and a cleared email can't be retried; database 3.7.1 adds email_queue.email_sensitive and marks email waiting at the update; 26.11.9 changelog entries

This commit is contained in:
johnnyq
2026-11-16 18:48:57 -05:00
parent 8784c50abe
commit c2eb55c8dc
17 changed files with 107 additions and 30 deletions

View File

@@ -849,8 +849,10 @@ function addToMailQueue($data) {
// The ticket reply it carries (emailQueue()'s ticket_reply_id), once the 3.4.8 update has made the column
$ticket_reply_sql = !empty($email['ticket_reply_id']) ? ', email_ticket_reply_id = ' . intval($email['ticket_reply_id']) : '';
// One with a link or code that opens something, cleared once it has gone (emailQueue()'s sensitive) - once the 3.7.1 update has made the column
$sensitive_sql = !empty($email['sensitive']) && mailQueueHasSensitive() ? ', email_sensitive = 1' : '';
mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$recipient', email_recipient_name = '$recipient_name', email_from = '$from', email_from_name = '$from_name', email_subject = '$subject', email_content = '$body', email_queued_at = $queued_at, email_cal_str = '$cal_str', email_attachments = '$attachments'$ticket_reply_sql");
mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$recipient', email_recipient_name = '$recipient_name', email_from = '$from', email_from_name = '$from_name', email_subject = '$subject', email_content = '$body', email_queued_at = $queued_at, email_cal_str = '$cal_str', email_attachments = '$attachments'$ticket_reply_sql$sensitive_sql");
}
return true;

View File

@@ -564,7 +564,7 @@ function contractSignatureRequestEmail(array $round, $note = '') {
'note' => trim((string) $note),
'summary' => array_filter(['Contract' => (string) $round['contract_name'], 'For' => (string) $round['client_name'], 'Term' => $term, 'Sent by' => (string) $by]),
'button' => ['label' => 'Review and sign', 'url' => contractSignatureUrl($round)],
], ['from' => contractEmailFrom()]);
], ['from' => contractEmailFrom(), 'sensitive' => true]);
return (bool) $sent;
}

View File

@@ -343,6 +343,9 @@ function emailPlainText($html, $limit = 0) {
* ticket_reply_id the agent's ticket reply it carries - kept with each email
* (email_queue.email_ticket_reply_id), so the conversation says who
* it went to and whether it went (ticketReplyEmailed())
* sensitive it carries a link or code that opens something - a shared login, an
* invite, a password link, a signing or approval link - so the mail queue
* clears what it said once it has gone (email_queue.email_sensitive)
* Who it was queued to, "Name (email)". Nothing is queued without mail set up.
*/
function emailQueue(array $recipients, $subject, array $email, array $opts = []) {
@@ -380,7 +383,8 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = [])
'attachments' => $attachments,
] + (!empty($opts['cal_str']) ? ['cal_str' => (string) $opts['cal_str']] : [])
+ ($queued_at !== '' ? ['queued_at' => $queued_at] : [])
+ (intval($opts['ticket_reply_id'] ?? 0) > 0 && ticketReplyEmailReady() ? ['ticket_reply_id' => intval($opts['ticket_reply_id'])] : []);
+ (intval($opts['ticket_reply_id'] ?? 0) > 0 && ticketReplyEmailReady() ? ['ticket_reply_id' => intval($opts['ticket_reply_id'])] : [])
+ (!empty($opts['sensitive']) ? ['sensitive' => 1] : []);
}
if ($data) {
addToMailQueue($data);

View File

@@ -10,8 +10,11 @@
* 1 being sent this minute
* 2 failed - tried again every MAIL_QUEUE_RETRY_MINUTES until it has had
* MAIL_QUEUE_TRIES; 99 tries means it isn't tried again (a bad address)
* 3 sent - its body, calendar file and attachments are cleared then, as
* they can carry share keys and temporary passwords
* 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),
* 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
* 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
@@ -101,7 +104,8 @@ function mailQueueCan(array $row, $action) {
$tries = intval($row['email_attempts']);
switch ($action) {
case 'retry':
return $status === 2 || $status === MAIL_QUEUE_CANCELLED;
// 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);
case 'delete':
@@ -125,7 +129,7 @@ function mailQueueAct($action, array $email_ids) {
$tries = MAIL_QUEUE_TRIES;
$cancelled = MAIL_QUEUE_CANCELLED;
$where = [
'retry' => "email_status IN (2, $cancelled)",
'retry' => "email_status IN (2, $cancelled) AND email_content <> ''",
'cancel' => "(email_status = 0 OR (email_status = 2 AND email_attempts < $tries))",
'delete' => 'email_status <> 1',
][$action] ?? null;
@@ -201,7 +205,7 @@ function mailQueueHealth() {
'url' => 'cron.php', 'action' => 'Maintenance > Cron', 'waiting' => $waiting];
}
return ['level' => '', 'key' => 'ok', 'title' => '', 'text' => 'Sent by the Mail Queue job each minute - it last ran ' . $ago($job['cron_job_last_run_at'] ?? null) . '. A failed email is tried '
. (MAIL_QUEUE_TRIES - 1) . ' more times, ' . MAIL_QUEUE_RETRY_MINUTES . ' minutes apart. Email is kept ' . MAIL_QUEUE_KEEP_DAYS . ' days; a sent one without what it said.', 'url' => '', 'action' => '', 'waiting' => 0];
. (MAIL_QUEUE_TRIES - 1) . ' more times, ' . MAIL_QUEUE_RETRY_MINUTES . ' minutes apart. 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];
}
/* Whether email_queue has email_error yet - not until the 3.3.7 update has run. */
@@ -242,6 +246,45 @@ function mailQueueErrorColumn() {
return mailQueueHasError() ? 'email_error' : 'NULL AS email_error';
}
/* Is there an email_sensitive column yet? Database 3.7.1 adds it. */
function mailQueueHasSensitive() {
global $mysqli;
static $has = null;
if ($has === null) {
$result = mysqli_query($mysqli, "SHOW COLUMNS FROM email_queue LIKE 'email_sensitive'");
$has = $result && mysqli_num_rows($result) > 0;
}
return $has;
}
/*
* What an email that has gone keeps, for the UPDATE that marks it sent (cron/mail_queue.php):
* everything, unless it carried a link or code that opens something - then what it said, its
* calendar file and attachments are cleared. Before the update that says which those are,
* every one is cleared, as all were.
*/
function mailQueueSentSql() {
if (!mailQueueHasSensitive()) {
return ", email_content = '', email_cal_str = '', email_attachments = ''";
}
return ", email_content = IF(email_sensitive = 1, '', email_content), email_cal_str = IF(email_sensitive = 1, '', email_cal_str),"
. " email_attachments = IF(email_sensitive = 1, '', email_attachments)";
}
/*
* Clear what a failed or cancelled email with a link or code said, a day after it was queued
* - its tries are long over, and a new one is a new link anyway (nightly). How many.
*/
function mailQueueClearSensitive() {
global $mysqli;
if (!mailQueueHasSensitive()) {
return 0;
}
mysqli_query($mysqli, "UPDATE email_queue SET email_content = '', email_cal_str = '', email_attachments = ''
WHERE email_sensitive = 1 AND email_status IN (2, " . MAIL_QUEUE_CANCELLED . ") AND email_content <> '' AND email_queued_at < NOW() - INTERVAL 1 DAY");
return mysqli_affected_rows($mysqli);
}
/*
* The mail queue at a glance, for the pages that point to it - how many are
* waiting (due now), retrying, and given up on in the last day and the last

View File

@@ -345,7 +345,7 @@ function portalMailInvite($user_id, $by = '') {
'summary' => ['Sign in as' => (string) $row['user_email']],
'button' => $button,
'after' => [$after],
]);
], ['sensitive' => $row['user_auth_method'] === 'local']);
}
/** A Forgot password link for a portal user, good for PORTAL_RESET_LINK_MINUTES. False when it can't go. */
@@ -366,7 +366,7 @@ function portalMailPasswordReset($user_id) {
'summary' => ['Account' => (string) $row['user_email']],
'button' => ['label' => 'Set a new password', 'url' => portalPasswordLink($user_id, PORTAL_RESET_LINK_MINUTES)],
'after' => ['The link works once, for the next ' . PORTAL_RESET_LINK_MINUTES . ' minutes. If you did not ask for it, ignore this email - your password stays as it is.'],
]);
], ['sensitive' => true]);
}
/*

View File

@@ -272,7 +272,7 @@ function portalInviteMail($invite_id, $token) {
];
$footnote = "You received this because $who invited this address to the $company_name client portal.";
}
$sent = (bool) emailQueue([['email' => (string) $row['invite_email'], 'name' => $greet, 'greet' => $greet !== '' ? $greet : null, 'footnote' => $footnote]], $subject, $email);
$sent = (bool) emailQueue([['email' => (string) $row['invite_email'], 'name' => $greet, 'greet' => $greet !== '' ? $greet : null, 'footnote' => $footnote]], $subject, $email, ['sensitive' => true]);
if ($sent && !emailCapturing()) {
mysqli_query($mysqli, "UPDATE portal_invites SET invite_sent_at = NOW(), invite_sent_count = invite_sent_count + 1 WHERE invite_id = " . intval($row['invite_id']));
}
@@ -808,7 +808,7 @@ function portalMailSignupExists($user_id) {
'summary' => ['Account' => (string) $row['user_email']],
'button' => $local ? ['label' => 'Set a new password', 'url' => portalPasswordLink($user_id, PORTAL_RESET_LINK_MINUTES)] : ['label' => 'Sign in', 'url' => "https://$config_base_url/login.php"],
'after' => [$local ? 'The link works once, for the next ' . PORTAL_RESET_LINK_MINUTES . ' minutes. If you did not ask, ignore this email - your account stays as it is.' : 'If you did not ask, ignore this email - your account stays as it is.'],
]);
], ['sensitive' => $local]);
}
/** Set whether people at a client can sign up. True when it changed. */

View File

@@ -665,7 +665,7 @@ function shareEmailLink($row, $url, $protect) {
'summary' => $summary,
'callout' => $callout,
'button' => ['label' => $button, 'url' => (string) $url, 'show_url' => true],
], ['from' => shareMailFrom()]);
], ['from' => shareMailFrom(), 'sensitive' => true]);
}
/** The code that opens a link protected by one, to its recipient. True when it was queued. */
@@ -677,7 +677,7 @@ function shareEmailCode($row, $code) {
'title' => "Your code is $code",
'intro' => ['Enter it on the page that asked for it, to open the secure link ' . $company['name'] . ' sent you. It works for ' . SHARE_CODE_MINUTES . ' minutes.'],
'callout' => 'If you did not ask for it, someone else has the link - ignore this email and let us know.',
], ['from' => shareMailFrom()]);
], ['from' => shareMailFrom(), 'sensitive' => true]);
}
/**

View File

@@ -1049,7 +1049,7 @@ function ticketMailTaskApproval($approval_id, $by) {
'button' => ['label' => 'Review and approve', 'url' => "https://$config_base_url/guest/guest_approve_ticket_task.php?task_approval_id=" . intval($approval['approval_id']) . '&url_key=' . rawurlencode((string) $approval['approval_url_key'])],
'after' => ['Want to know more first? Just reply to this email.'],
'contact' => ticketEmailContactLine(),
], ['from' => ticketEmailFrom()]));
], ['from' => ticketEmailFrom(), 'sensitive' => true]));
}
/*

View File

@@ -398,7 +398,7 @@ function userInviteMail($invite_id, $token) {
'summary' => $summary,
'button' => ['label' => 'Accept the invite', 'url' => userInviteUrl($row['invite_id'], $token)],
'after' => ['The invite works for ' . USER_INVITE_LINK_DAYS . ' days. If you were not expecting it, you can ignore this email - nothing is set up unless the link is used.'],
]);
], ['sensitive' => true]);
if ($sent && !emailCapturing()) {
mysqli_query($mysqli, "UPDATE user_invites SET invite_sent_at = NOW(), invite_sent_count = invite_sent_count + 1 WHERE invite_id = " . intval($row['invite_id']));
}