mirror of
https://github.com/itflow-org/itflow
synced 2026-12-02 00:59:03 +00:00
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:
@@ -868,6 +868,12 @@ function addToMailQueue($data) {
|
||||
$thread_sql = (!empty($email['cc']) ? ", email_cc = '" . mysqli_real_escape_string($mysqli, json_encode(array_values((array) $email['cc']), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) . "'" : '')
|
||||
. (!empty($email['headers']) ? ", email_headers = '" . mysqli_real_escape_string($mysqli, json_encode((array) $email['headers'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) . "'" : '');
|
||||
}
|
||||
// Its Message-ID, for a bounce to find it by, and what a billing email is about - once the 4.3.5 update has made the columns
|
||||
if (mailQueueBounceReady()) {
|
||||
$message_id = trim((string) (((array) ($email['headers'] ?? []))['Message-ID'] ?? ''), "<> \t");
|
||||
$thread_sql .= ($message_id !== '' && strlen($message_id) <= 255 ? ", email_message_id = '" . mysqli_real_escape_string($mysqli, $message_id) . "'" : '')
|
||||
. (!empty($email['about']) && preg_match('/^(invoice|quote):\d+$/', (string) $email['about']) ? ", email_about = '" . $email['about'] . "'" : '');
|
||||
}
|
||||
|
||||
$error = '';
|
||||
try {
|
||||
|
||||
@@ -2558,7 +2558,7 @@ function recurringInvoiceGenerate($recurring_invoice_id, $by = 'Cron', $early =
|
||||
if (billingReminderRules()['attach_pdf'] && ($file = billingEmailPdf(billingPdf('invoice', $invoice)))) {
|
||||
$attach[] = $file;
|
||||
}
|
||||
$sent_to = billingEmailQueue($recipients, $subject, $email, ['attach' => $attach]);
|
||||
$sent_to = billingEmailQueue($recipients, $subject, $email, ['attach' => $attach, 'about' => 'invoice:' . intval($invoice['invoice_id'])]);
|
||||
}
|
||||
if ($sent_to) {
|
||||
mysqli_query($mysqli, "UPDATE invoices SET invoice_status = 'Sent' WHERE invoice_id = $invoice_id");
|
||||
@@ -2783,7 +2783,7 @@ function billingInvoiceIssue($invoice_id, $by) {
|
||||
if (billingReminderRules()['attach_pdf'] && ($file = billingEmailPdf(billingPdf('invoice', $invoice)))) {
|
||||
$attach[] = $file;
|
||||
}
|
||||
$sent_to = billingEmailQueue($recipients, $subject, $email, ['attach' => $attach]);
|
||||
$sent_to = billingEmailQueue($recipients, $subject, $email, ['attach' => $attach, 'about' => 'invoice:' . intval($invoice['invoice_id'])]);
|
||||
}
|
||||
mysqli_query($mysqli, "UPDATE invoices SET invoice_status = 'Sent' WHERE invoice_id = $invoice_id AND invoice_status = 'Draft'");
|
||||
// Texted with its link too, to those emailed who agreed to texts (functions/sms.php)
|
||||
|
||||
@@ -294,15 +294,17 @@ function billingEmailExtraField($id) {
|
||||
* Queue a billing email to each recipient, greeting each by name - nobody
|
||||
* without one - through emailQueue(), whether or not mail is set up yet, as
|
||||
* the Send windows always have. $opts: from ('invoice' or 'quote' -
|
||||
* whose address it comes from) and attach (a list of billingEmailPdf()
|
||||
* files). Who it was queued to, "Name (email)".
|
||||
* whose address it comes from), attach (a list of billingEmailPdf()
|
||||
* files) and about (invoice:12 or quote:7, so a bounce goes in its history -
|
||||
* mailQueueBounced()). Who it was queued to, "Name (email)".
|
||||
*/
|
||||
function billingEmailQueue(array $recipients, $subject, array $email, array $opts = []) {
|
||||
$recipients = array_map(function ($recipient) {
|
||||
$greet = array_key_exists('greet', $recipient) ? $recipient['greet'] : ($recipient['name'] ?? '');
|
||||
return ['greet' => trim((string) $greet) !== '' ? (string) $greet : null] + $recipient;
|
||||
}, $recipients);
|
||||
return emailQueue($recipients, $subject, $email, ['from' => billingEmailFrom($opts['from'] ?? 'invoice'), 'attachments' => (array) ($opts['attach'] ?? []), 'always' => true]);
|
||||
return emailQueue($recipients, $subject, $email, ['from' => billingEmailFrom($opts['from'] ?? 'invoice'), 'attachments' => (array) ($opts['attach'] ?? []), 'always' => true,
|
||||
'about' => (string) ($opts['about'] ?? '')]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -561,6 +561,10 @@ function emailInlineImages($html, array $ticket = []) {
|
||||
* itself) - headers saying so, so it isn't answered (ticketMailAutoHeaders())
|
||||
* bulk an email to many - the contacts or clients list's Email - which the mail queue
|
||||
* sends after everything else (email_queue.email_bulk)
|
||||
* about what a billing email is about - invoice:12, quote:7 (email_queue.email_about) - so
|
||||
* a bounce goes in its history (mailQueueBounced())
|
||||
* Each email has a Message-ID of its own (email_queue.email_message_id, 4.3.5) - a ticket's from its
|
||||
* conversation, the rest made here (emailMessageId()) - so a bounce finds the email it was about.
|
||||
* Who it was queued to, "Name (email)" - the copied in too: only those that went in the queue
|
||||
* (addToMailQueue()). Nothing is queued without mail set up. The subject is cut to fit (emailFit()).
|
||||
*/
|
||||
@@ -607,6 +611,10 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = [])
|
||||
// Its own headers - a List-Unsubscribe for its link - with the conversation's and the automatic ones
|
||||
$headers = ($thread !== null ? ticketMailHeaders($thread_id, (string) $from['email'], $thread) : []) + $auto_headers + (!$together ? (array) ($recipient['headers'] ?? []) : [])
|
||||
+ ($reply_to !== '' ? ['Reply-To' => $reply_to] : []);
|
||||
// Its own Message-ID, for a bounce to find it by - a ticket's conversation made one already
|
||||
if (!isset($headers['Message-ID']) && mailQueueBounceReady()) {
|
||||
$headers['Message-ID'] = '<' . emailMessageId((string) $from['email']) . '>';
|
||||
}
|
||||
$went = [];
|
||||
foreach (array_merge([$recipient], $cc) as $person) {
|
||||
$went[strtolower($person['email'])] = $person['name'] !== '' ? $person['name'] . ' (' . $person['email'] . ')' : $person['email'];
|
||||
@@ -635,6 +643,7 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = [])
|
||||
+ (intval($opts['ticket_reply_id'] ?? 0) > 0 && ticketReplyEmailReady() ? ['ticket_reply_id' => intval($opts['ticket_reply_id'])] : [])
|
||||
+ (!empty($opts['sensitive']) ? ['sensitive' => 1] : [])
|
||||
+ (!empty($opts['bulk']) ? ['bulk' => 1] : [])
|
||||
+ (!empty($opts['about']) ? ['about' => (string) $opts['about']] : [])
|
||||
+ ($cc_list ? ['cc' => $cc_list] : [])
|
||||
+ ($headers ? ['headers' => $headers] : []);
|
||||
// Each on its own, so who it went to is only those queued - one that couldn't be is logged (addToMailQueue())
|
||||
@@ -645,6 +654,20 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = [])
|
||||
return array_values($sent);
|
||||
}
|
||||
|
||||
/*
|
||||
* A new Message-ID - without its <> - at the domain of $from_email, or else ITFlow's own address: itflow.q.<random>@domain.
|
||||
* The conversation of a ticket makes its own (ticketMailHeaders()).
|
||||
*/
|
||||
function emailMessageId($from_email) {
|
||||
global $config_base_url;
|
||||
$from_email = (string) $from_email;
|
||||
$domain = strtolower(trim(substr($from_email, strrpos($from_email, '@') !== false ? strrpos($from_email, '@') + 1 : strlen($from_email))));
|
||||
if (!preg_match('/^[a-z0-9.-]+\.[a-z]{2,}$/', $domain)) {
|
||||
$domain = preg_replace('/[^a-z0-9.-]/', '', strtolower((string) parse_url('https://' . $config_base_url, PHP_URL_HOST))) ?: 'itflow.invalid';
|
||||
}
|
||||
return 'itflow.q.' . bin2hex(random_bytes(12)) . '@' . $domain;
|
||||
}
|
||||
|
||||
/*
|
||||
* $text cut to the $length characters a column of the mail queue holds - its subject and names (255) -
|
||||
* with an ellipsis. A ticket's subject can be 500 characters, and an email about one too long for
|
||||
|
||||
@@ -190,7 +190,7 @@ function invoiceBulkEmail(array $row) {
|
||||
if (billingReminderRules()['attach_pdf'] && ($file = billingEmailPdf(billingPdf('invoice', $invoice)))) {
|
||||
$attach[] = $file;
|
||||
}
|
||||
$sent = billingEmailQueue($recipients, $subject, $email, ['attach' => $attach]);
|
||||
$sent = billingEmailQueue($recipients, $subject, $email, ['attach' => $attach, 'about' => 'invoice:' . intval($invoice['invoice_id'])]);
|
||||
$texted = smsInvoiceNotify($invoice, 'invoice', $recipients);
|
||||
billingLog('invoice', $invoice_id, $invoice['invoice_status'] === 'Draft' ? 'Sent' : $invoice['invoice_status'],
|
||||
'Invoice emailed by ' . stripslashes($session_name) . ' to ' . implode(', ', $sent) . ($attach ? ' with the PDF' : '') . ($texted ? ' - and texted to ' . implode(', ', $texted) : '') . ' (bulk)');
|
||||
|
||||
167
functions/mail_ignore.php
Normal file
167
functions/mail_ignore.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Ignored senders - addresses and domains whose email the ticket mailbox sets aside (database 4.3.5,
|
||||
* email_ignored_senders): no ticket, no reply on one, no answer and no Unprocessed Email entry. It is marked read and
|
||||
* moved to the mailbox's ITFlow folder, as the rest, and each rule counts what it set aside. A mail server's bounce is
|
||||
* still read, whoever it is from, and an email an agent asked to make a ticket or add to one (Tickets > Unprocessed
|
||||
* Email) goes where they said.
|
||||
*
|
||||
* A rule is an address - alerts@vendor.example - or a domain - vendor.example, which takes its subdomains too
|
||||
* (mail.vendor.example). Kept in Admin > Ignored Senders (admin/email_ignored_senders.php), and from Unprocessed Email's
|
||||
* Ignore Sender (agent/post/email_unprocessed.php). Checked by cron/ticket_email_parser.php (mailIgnoreFind()) and by
|
||||
* Try an Email (functions/ticket_mail_try.php).
|
||||
*/
|
||||
|
||||
/** Whether the 4.3.5 update has run - email_ignored_senders is there. */
|
||||
function mailIgnoreReady() {
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
* What was typed, as a rule - ['match' => the address or domain, lower case, 'kind' => address or domain] - or null
|
||||
* when it is neither. "@vendor.example" and "*@vendor.example" are the domain.
|
||||
*/
|
||||
function mailIgnoreClean($input) {
|
||||
$input = strtolower(trim((string) $input));
|
||||
$input = preg_replace('/^\*?@/', '', $input);
|
||||
if (filter_var($input, FILTER_VALIDATE_EMAIL) && mb_strlen($input) <= 255) {
|
||||
return ['match' => $input, 'kind' => 'address'];
|
||||
}
|
||||
if (preg_match('/^(?=.{3,253}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/', $input)) {
|
||||
return ['match' => $input, 'kind' => 'domain'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Every rule - id => row - read once a run. $reload reads them again. */
|
||||
function mailIgnoreRules($reload = false) {
|
||||
global $mysqli;
|
||||
static $rules = null;
|
||||
if ($rules === null || $reload) {
|
||||
$rules = [];
|
||||
if (mailIgnoreReady()) {
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM email_ignored_senders ORDER BY ignored_sender_match");
|
||||
while ($sql && ($row = mysqli_fetch_assoc($sql))) {
|
||||
$rules[intval($row['ignored_sender_id'])] = $row;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/** Whether a rule (its match) takes $email - the address itself, or an address at the domain or one of its subdomains. */
|
||||
function mailIgnoreMatches($match, $email) {
|
||||
$match = strtolower((string) $match);
|
||||
$email = strtolower(trim((string) $email));
|
||||
if ($email === '' || $match === '') {
|
||||
return false;
|
||||
}
|
||||
if (strpos($match, '@') !== false) {
|
||||
return $email === $match;
|
||||
}
|
||||
$domain = substr($email, strrpos($email, '@') + 1);
|
||||
return $domain === $match || str_ends_with($domain, ".$match");
|
||||
}
|
||||
|
||||
/** The rule that takes email from $email - its row, an address rule before a domain one - or null. */
|
||||
function mailIgnoreFind($email) {
|
||||
$found = null;
|
||||
foreach (mailIgnoreRules() as $rule) {
|
||||
if (mailIgnoreMatches($rule['ignored_sender_match'], $email)) {
|
||||
if (strpos($rule['ignored_sender_match'], '@') !== false) {
|
||||
return $rule;
|
||||
}
|
||||
$found = $found ?? $rule;
|
||||
}
|
||||
}
|
||||
return $found;
|
||||
}
|
||||
|
||||
/** A rule set an email aside - counted, with when. */
|
||||
function mailIgnoreHit($id) {
|
||||
global $mysqli;
|
||||
mysqli_query($mysqli, "UPDATE email_ignored_senders SET ignored_sender_matches = ignored_sender_matches + 1, ignored_sender_matched_at = NOW() WHERE ignored_sender_id = " . intval($id));
|
||||
}
|
||||
|
||||
/*
|
||||
* Who a rule would also take that you may not mean it to - ['contacts' => how many contacts have an address it takes,
|
||||
* 'clients' => the names of the clients whose domain it is]. For the warning on saving one.
|
||||
*/
|
||||
function mailIgnoreReach($match) {
|
||||
global $mysqli;
|
||||
$match = strtolower((string) $match);
|
||||
$out = ['contacts' => 0, 'clients' => []];
|
||||
if ($match === '') {
|
||||
return $out;
|
||||
}
|
||||
$match_sql = escapeSql($match);
|
||||
if (strpos($match, '@') !== false) {
|
||||
$out['contacts'] = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM contacts WHERE contact_archived_at IS NULL AND LOWER(contact_email) = '$match_sql'"))[0] ?? 0);
|
||||
return $out;
|
||||
}
|
||||
$like = escapeSql(str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $match));
|
||||
$out['contacts'] = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM contacts WHERE contact_archived_at IS NULL
|
||||
AND (LOWER(contact_email) LIKE '%@$like' OR LOWER(contact_email) LIKE '%.$like')"))[0] ?? 0);
|
||||
$sql = mysqli_query($mysqli, "SELECT DISTINCT client_name FROM domains JOIN clients ON client_id = domain_client_id
|
||||
WHERE domain_archived_at IS NULL AND client_archived_at IS NULL AND (LOWER(domain_name) = '$match_sql' OR LOWER(domain_name) LIKE '%.$like') ORDER BY client_name LIMIT 5");
|
||||
while ($sql && ($row = mysqli_fetch_row($sql))) {
|
||||
$out['clients'][] = (string) $row[0];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/* The warning for a rule that takes contacts' or clients' email - '' when it takes none. HTML. */
|
||||
function mailIgnoreReachText(array $reach) {
|
||||
$parts = [];
|
||||
if ($reach['contacts']) {
|
||||
$parts[] = $reach['contacts'] . ' contact' . ($reach['contacts'] === 1 ? '' : 's');
|
||||
}
|
||||
if ($reach['clients']) {
|
||||
$parts[] = 'the domain of ' . implode(', ', array_map('escapeHtml', $reach['clients']));
|
||||
}
|
||||
return $parts ? 'It takes the email of ' . implode(' and ', $parts) . ' - their tickets and replies by email are set aside too.' : '';
|
||||
}
|
||||
|
||||
/*
|
||||
* Add a rule - $rule from mailIgnoreClean(), $note why (raw) - by $user_id. Its id: the one there already when it
|
||||
* is, as the match is kept once.
|
||||
*/
|
||||
function mailIgnoreAdd(array $rule, $note, $user_id) {
|
||||
global $mysqli;
|
||||
$match_sql = escapeSql($rule['match']);
|
||||
$there = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT ignored_sender_id FROM email_ignored_senders WHERE ignored_sender_match = '$match_sql'"))[0] ?? 0);
|
||||
if ($there) {
|
||||
return $there;
|
||||
}
|
||||
mysqli_query($mysqli, "INSERT INTO email_ignored_senders SET ignored_sender_match = '$match_sql', ignored_sender_note = '" . escapeSql(mb_substr(trim((string) $note), 0, 255)) . "',
|
||||
ignored_sender_created_by = " . intval($user_id));
|
||||
mailIgnoreRules(true);
|
||||
return intval(mysqli_insert_id($mysqli));
|
||||
}
|
||||
|
||||
/*
|
||||
* The emails waiting in Unprocessed Email (functions/mail_unprocessed.php) that a rule takes - dismissed, by $user_id,
|
||||
* as it would have set them aside. How many.
|
||||
*/
|
||||
function mailIgnoreDismissWaiting($match, $user_id) {
|
||||
global $mysqli;
|
||||
if (!function_exists('mailUnprocessedReady') || !mailUnprocessedReady()) {
|
||||
return 0;
|
||||
}
|
||||
$ids = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT email_unprocessed_id, email_unprocessed_from_email FROM email_unprocessed WHERE email_unprocessed_done_at IS NULL AND email_unprocessed_action = ''");
|
||||
while ($sql && ($row = mysqli_fetch_assoc($sql))) {
|
||||
if (mailIgnoreMatches($match, $row['email_unprocessed_from_email'])) {
|
||||
$ids[] = intval($row['email_unprocessed_id']);
|
||||
}
|
||||
}
|
||||
return $ids ? count(mailUnprocessedDecide($ids, 'dismiss', [], $user_id)) : 0;
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
@@ -1053,20 +1053,26 @@ function ticketReplyEmailStates(array $rows) {
|
||||
return array_map(function ($state) { $state['to'] = array_values($state['to']); return $state; }, $states);
|
||||
}
|
||||
$sql = mysqli_query($mysqli, "SELECT email_ticket_reply_id, email_recipient, email_recipient_name, " . (ticketMailThreadReady() ? 'email_cc' : "'' AS email_cc") . ",
|
||||
email_status, email_attempts, email_queued_at, email_failed_at, email_sent_at, email_error
|
||||
email_status, email_attempts, email_queued_at, email_failed_at, email_sent_at, email_error" . mailQueueBounceColumns() . "
|
||||
FROM email_queue WHERE email_ticket_reply_id IN (" . implode(',', array_keys($states)) . ") ORDER BY email_id");
|
||||
while ($sql && ($email = mysqli_fetch_assoc($sql))) {
|
||||
$reply_id = intval($email['email_ticket_reply_id']);
|
||||
$state = mailQueueState($email);
|
||||
// Bounced for some on it - the rest got it (mailQueueBounced())
|
||||
$got_it = $state['key'] === 'bounced' ? mailQueueState(['email_bounced_at' => null] + $email) : $state;
|
||||
// Those copied in went, or didn't, with it
|
||||
$people = array_merge([['email' => (string) $email['email_recipient'], 'name' => (string) $email['email_recipient_name'] !== (string) $email['email_recipient'] ? (string) $email['email_recipient_name'] : '']],
|
||||
mailQueueCc($email['email_cc']));
|
||||
foreach ($people as $person) {
|
||||
$address = strtolower($person['email']);
|
||||
$person_state = $state;
|
||||
if ($state['key'] === 'bounced') {
|
||||
$person_state = array_key_exists($address, $state['bounce']) ? ['error' => mailQueueErrorText($state['bounce'][$address])] + $state : $got_it;
|
||||
}
|
||||
$states[$reply_id]['to'][$address] = [
|
||||
'email' => $person['email'],
|
||||
'name' => $states[$reply_id]['to'][$address]['name'] ?? $person['name'],
|
||||
'state' => $state,
|
||||
'state' => $person_state,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1076,6 +1082,8 @@ function ticketReplyEmailStates(array $rows) {
|
||||
$key = 'gone';
|
||||
} elseif (in_array('failed', $keys, true)) {
|
||||
$key = 'failed';
|
||||
} elseif (in_array('bounced', $keys, true)) {
|
||||
$key = 'bounced';
|
||||
} elseif (array_intersect(['waiting', 'sending', 'retrying'], $keys)) {
|
||||
$key = 'waiting';
|
||||
} elseif (!in_array('sent', $keys, true)) {
|
||||
@@ -1846,6 +1854,9 @@ function ticketReplyEmailHtml($reply_id, array $state, $queue_url = '') {
|
||||
$failed = count(array_filter($to, function ($recipient) {
|
||||
return ($recipient['state']['key'] ?? '') === 'failed';
|
||||
}));
|
||||
$bounced = count(array_filter($to, function ($recipient) {
|
||||
return ($recipient['state']['key'] ?? '') === 'bounced';
|
||||
}));
|
||||
switch ($state['key'] ?? 'gone') {
|
||||
case 'sent':
|
||||
[$icon, $text, $title] = ['fa-check text-success', "Emailed to $first", 'Sent'];
|
||||
@@ -1860,6 +1871,10 @@ function ticketReplyEmailHtml($reply_id, array $state, $queue_url = '') {
|
||||
case 'cancelled':
|
||||
[$icon, $text, $title] = ['fa-ban', "Email to $first cancelled", 'Cancelled'];
|
||||
break;
|
||||
case 'bounced':
|
||||
[$icon, $text, $title] = ['fa-reply text-danger', $bounced === count($to) ? "<span class=\"text-danger\">Email to $first bounced</span>"
|
||||
: "Emailed to $first <span class=\"text-danger\">· $bounced bounced</span>", 'Bounced'];
|
||||
break;
|
||||
default:
|
||||
[$icon, $text, $title] = ['fa-paper-plane', "Emailed to $first", ''];
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
* 0. ITFlow's own email come back (emailOwnAddresses()) is skipped;
|
||||
* - a mail server's bounce (ticketMailIsBounce() - here, by its address alone) is read as one,
|
||||
* whatever the settings - never a ticket;
|
||||
* - an ignored sender (mailIgnoreFind(), functions/mail_ignore.php) is set aside;
|
||||
* 1. an automatic reply (out of office) is a note on the ticket it answers, or set aside;
|
||||
* - an email answering one of a ticket's emails, by its In-Reply-To or
|
||||
* References - a reply on that ticket, whatever its subject. An email
|
||||
@@ -66,6 +67,17 @@ function ticketMailTry($from_email, $subject, $auto_reply = false) {
|
||||
}
|
||||
$steps[] = ['A mail server saying an email couldn\'t be delivered?', 'No'];
|
||||
|
||||
// An ignored sender (Admin > Ignored Senders, functions/mail_ignore.php)
|
||||
if (mailIgnoreReady()) {
|
||||
$ignored = mailIgnoreFind($from_email);
|
||||
if ($ignored) {
|
||||
$steps[] = ['An ignored sender?', 'Yes - ' . $ignored['ignored_sender_match']];
|
||||
return $end('fa-eye-slash', 'secondary', 'Set aside - an ignored sender',
|
||||
'No ticket, no reply on one and no answer - it is moved to the ITFlow folder. Admin > Ignored Senders has the rule.');
|
||||
}
|
||||
$steps[] = ['An ignored sender?', 'No'];
|
||||
}
|
||||
|
||||
if ($auto_reply) {
|
||||
$steps[] = ['An automatic reply?', 'Yes'];
|
||||
$answered = ticketMailSubjectTicket($subject);
|
||||
|
||||
Reference in New Issue
Block a user