Mail review stage 6: bulk email leaves out contacts marked Email bounced or unsubscribed and says who was left out; each bulk email has an Unsubscribe link and List-Unsubscribe headers for one-click unsubscribe, the link asks first and offers Subscribe Again, unsubscribing covers every contact with the address, and agents and the client portal can change it (database 4.3.0); Send at uses the agent's time zone; bounces of bulk email come back to the ticket mailbox when the mail server and domains allow it; each email in a queue run goes out with its own envelope sender instead of the first one's; 26.12.3 changelog

This commit is contained in:
johnnyq
2026-11-30 19:00:49 -05:00
parent e57faeeb3a
commit 6cec0b8574
19 changed files with 572 additions and 44 deletions

View File

@@ -103,6 +103,72 @@ function mailQueueRefusedForGood($code, $code_ex, $detail) {
&& 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;
}
/*
* The envelope sender (Return-Path) of an email from $from_email - where a mail server sends its bounce - for email
* sent to many (email_bulk): the mailbox ITFlow reads (Admin > Mail, Receiving), so its bounces come back and are read
* - the contacts marked Email bounced, and left out of the next one (functions/contact_bounce.php). They went to the
* From mailbox, which nobody may read. '' leaves it the From address, as always - unless it can be done safely:
* - email to ticket is on, reading a mailbox with an address that isn't the From address
* - it goes by SMTP: Microsoft Graph has no envelope sender of its own
* - ITFlow signs in to the mail server as that mailbox, or doesn't sign in - a mail server takes no other sender
* from a mailbox (Microsoft 365's SendAsDenied)
* - it is at the From address's domain, or one above or below it - else the email fails DMARC for its domain
* A mail server that turns it away all the same is sent the From address for the rest of the run (cron/mail_queue.php).
*/
function mailQueueReturnPath($from_email) {
global $mysqli;
static $settings = null;
if ($settings === null) {
$settings = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT config_ticket_email_parse, config_imap_provider, config_imap_username, config_smtp_provider, config_smtp_username, config_mail_from_email
FROM settings WHERE company_id = 1")) ?: [];
}
$reads = strtolower(trim((string) ($settings['config_imap_username'] ?? '')));
$from = strtolower(trim((string) $from_email));
if (!intval($settings['config_ticket_email_parse'] ?? 0) || trim((string) ($settings['config_imap_provider'] ?? '')) === '' || !filter_var($reads, FILTER_VALIDATE_EMAIL) || $reads === $from) {
return '';
}
$provider = (string) ($settings['config_smtp_provider'] ?? '');
if ($provider === '' || ($provider === 'microsoft_oauth' && !empty(mailGraphState()['send']))) {
return '';
}
// Who ITFlow signs in as - the OAuth providers sign in as the system mailbox when no username is saved (mailQueueMailer())
$signs_in = strtolower(trim((string) ($settings['config_smtp_username'] ?? '')));
if ($signs_in === '' && in_array($provider, ['google_oauth', 'microsoft_oauth'], true)) {
$signs_in = strtolower(trim((string) ($settings['config_mail_from_email'] ?? '')));
}
if ($signs_in !== '' && $signs_in !== $reads) {
return '';
}
$reads_domain = substr($reads, strrpos($reads, '@') + 1);
$from_domain = strpos($from, '@') !== false ? substr($from, strrpos($from, '@') + 1) : '';
$aligned = $from_domain !== '' && ($reads_domain === $from_domain || str_ends_with($reads_domain, ".$from_domain") || str_ends_with($from_domain, ".$reads_domain"));
return $aligned && !mailQueueReturnPathRefused() ? $reads : '';
}
/* Whether the mail server turned the Return-Path (mailQueueReturnPath()) away this run - $refused true says it did. */
function mailQueueReturnPathRefused($refused = null) {
static $was = false;
if ($refused !== null) {
$was = (bool) $refused;
}
return $was;
}
/*
* Where the bounces of email sent to many from $from_email go - the mailbox ITFlow reads, where they are read
* (mailQueueReturnPath()), or the From mailbox itself. For the Email window. ['address', 'read' => bool].
*/
function mailQueueBouncesTo($from_email) {
global $mysqli;
$return_path = mailQueueReturnPath($from_email);
if ($return_path !== '') {
return ['address' => $return_path, 'read' => true];
}
$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_ticket_email_parse, config_imap_provider, config_imap_username FROM settings WHERE company_id = 1"));
$read = $row && intval($row[0]) && trim((string) $row[1]) !== '' && strcasecmp(trim((string) $row[2]), trim((string) $from_email)) === 0;
return ['address' => trim((string) $from_email), 'read' => $read];
}
/* 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;
@@ -457,6 +523,9 @@ const MAIL_QUEUE_HEADERS = [
'References' => '/^(<[^<>\s]+@[^<>\s]+>)( <[^<>\s]+@[^<>\s]+>)*$/',
'Auto-Submitted' => '/^auto-(replied|generated)$/',
'X-Auto-Response-Suppress' => '/^[A-Za-z]+(, ?[A-Za-z]+)*$/',
// Bulk email's link to leave, which mail programs show as their own Unsubscribe - one click (RFC 8058, functions/contact_unsubscribe.php)
'List-Unsubscribe' => '/^<https:\/\/[^<>\s]+>$/',
'List-Unsubscribe-Post' => '/^List-Unsubscribe=One-Click$/',
];
/*