From b208ed0069b66d1433760a04de3f365361c05bb1 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Mon, 30 Nov 2026 12:37:03 -0500 Subject: [PATCH] Stop ITFlow's own mail looping back: its own addresses are never made watchers or emailed about a ticket, mail from them is set aside in the ITFlow folder without a ticket, and Email new tickets to can't be the mailbox ITFlow reads; take email someone opened in the hour before the parser got to it; a domain whose mail server can't be found is retried like any failed email and one taking mail at its own address is accepted; 26.12.3 changelog entries --- CHANGELOG.md | 7 ++++ admin/post/settings_ticket.php | 3 ++ admin/settings_ticket.php | 2 +- agent/post/ticket.php | 2 +- cron/mail_queue.php | 20 ++++++++---- cron/ticket_email_parser.php | 46 +++++++++++++++++++++++++-- functions/email.php | 44 +++++++++++++++++++++++++ functions/mail_graph_mailbox.php | 39 +++++++++++++++++++---- functions/mail_queue.php | 21 +++++++++++- functions/ticket.php | 15 +++++++-- functions/ticket_mail_try.php | 7 ++++ functions/ticket_reply_recipients.php | 3 +- 12 files changed, 187 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 639408aeb..d34a0dca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,10 @@ Update from Maintenance > Update as usual - this release has four database chang - A client page opened without a client in its address, such as an old bookmark to a client's overview, was a blank page. It now says That client isn't here, as it does for a client that's been deleted, with the menu and a way back to Clients. - The notice that white-labelling was turned off opens Admin > Settings > Modules, instead of a page that isn't there. - The old Access Modules page, already off the Admin menu, is gone. What each role can do is set on the role, in Admin > Roles. +- A client who CCs your support mailbox no longer makes it a watcher. As one, every email about the ticket went back into the mailbox, came back in as a note and reopened the ticket - a resolved ticket reopened a minute later. Mail from ITFlow's own addresses is now set aside in the ITFlow folder without a ticket, and nothing about a ticket is emailed to them, so a support mailbox already watching a ticket stops getting copies too. +- Email new tickets to (Admin > Settings > Tickets) can't be the mailbox ITFlow reads tickets from any more: each notice could come back in as a new email and raise another ticket, whose notice raised another, every minute. One saved before isn't emailed. +- Email someone opened before ITFlow got to it - Outlook marks an email read as soon as it shows it - is made a ticket now, when it came in the last hour. It used to stay in the mailbox for good. Read email older than that, such as what was there when the mailbox was connected, is left alone. +- An email to a domain whose mail server couldn't be found is tried again like any other failed email, and a domain with no MX record that takes mail at its own address is found. One failed lookup used to fail the email for good. ### Developer Updates @@ -93,6 +97,9 @@ Update from Maintenance > Update as usual - this release has four database chang - agent/includes/inc_all_client.php: without a client_id, or with one that isn't there, it draws That client isn't here in the agent layout and exits, before anything queries with no client - it was an SQL error (or a tagPills() TypeError) and a blank page. admin/modules.php and its commented-out menu entry are removed. - Tabs in agent/modals/product/product_form.php, service/service_form.php, calendar/calendar_event_form.php, expense/expense_form.php and ticket/ticket_add.php, and in admin/modals/payment_provider/payment_provider_edit.php, rmm/rmm_provider_edit.php, identity_provider/identity_provider_edit.php and ticket_status/ticket_status_form.php - the same field names, so the posts and their handlers are unchanged. js/app.js has window.itflowRevealTab(field), used by the invalid-field handler and by the provider windows' server-check errors, to open the tab a field is on. - admin/modals/role/role_form.php draws a module as a list-group row with data-role-allows (its levels' text as JSON); js/role_form.js writes what the level picked allows into [data-role-can]. agent/modals/ticket/ticket_edit.php takes ?tab=planning. ticket_invoice_add.php posts scope as a hidden field, so add_invoice_from_ticket is unchanged. .itflow-tag-icons is three rows high (6.6rem), and js/app.js scrolls its .active icon into the box on shown.bs.modal and shown.bs.tab. +- Mail loop: emailOwnAddresses() (functions/email.php - the From mailboxes and config_imap_username) is skipped by cron/ticket_email_parser.php before anything else (moved to the ITFlow folder, App Logs info), left out of CC watchers there and in ticketAddWatchers(), filtered out of ticketMailSend()'s recipients and ticketColleagueRecipients(), and not offered in the reply To line (ticketReplyRecipients()). emailReadMailbox() - the Receiving username or the ticket mailbox, while email-to-ticket is on - is refused for config_ticket_new_ticket_notification_email and skipped by ticketMailNewTicketAddress(). Try an Email (functions/ticket_mail_try.php) asks the same first. +- Read mail: the parser's unseen query is followed, when it has room, by seen() unflagged() since(yesterday) newest(), kept when the Date header is within $read_mail_minutes (60). MailGraphMessageQuery (functions/mail_graph_mailbox.php) now honours seen(), unseen(), since() and newest() - isRead eq true, receivedDateTime desc. +- cron/mail_queue.php: mailQueueDomainProblem() (functions/mail_queue.php - MX, else A or AAAA, once a domain a run) is a normal failed try in the send path (attempts 1, not 99) and is asked again in the retry path before each send; --no-mx-validation still skips it. ## [26.12.2] diff --git a/admin/post/settings_ticket.php b/admin/post/settings_ticket.php index 13fd355c1..7670b6e74 100644 --- a/admin/post/settings_ticket.php +++ b/admin/post/settings_ticket.php @@ -97,6 +97,9 @@ if (isset($_POST['edit_ticket_settings'])) { if ($problem = settingsEmailProblem($after['config_ticket_new_ticket_notification_email'])) { $errors['config_ticket_new_ticket_notification_email'] = $problem; + } elseif (emailReadMailbox($after['config_ticket_new_ticket_notification_email'])) { + // Read back in, each notice would be an email to raise a ticket - which sends another notice + $errors['config_ticket_new_ticket_notification_email'] = 'That is the mailbox ITFlow reads tickets from, so each notice would come back as an email and raise another ticket. Use a team or agent address.'; } $hours = $after['config_ticket_autoclose_hours']; if ($hours !== intval($before['config_ticket_autoclose_hours']) && ($hours < 24 || $hours > 8760)) { diff --git a/admin/settings_ticket.php b/admin/settings_ticket.php index 43e9b5fac..69b2a58e0 100644 --- a/admin/settings_ticket.php +++ b/admin/settings_ticket.php @@ -57,7 +57,7 @@ echo settingsPageStart('ticket'); 'fa-bell', 'type' => 'email', 'maxlength' => 200, - 'placeholder' => 'support@company.com', 'help' => 'A shared mailbox or list works best. Empty, nobody is emailed.']) ?> + 'placeholder' => 'techs@company.com', 'help' => 'A team mailbox or list works best - not the one ITFlow reads tickets from, as each notice would come back as an email. Empty, nobody is emailed.']) ?>
AI triage needs an AI model for Tickets, or one for everything - AI Providers.
diff --git a/agent/post/ticket.php b/agent/post/ticket.php index a374ddf98..b737d383d 100644 --- a/agent/post/ticket.php +++ b/agent/post/ticket.php @@ -507,7 +507,7 @@ if (isset($_POST['add_ticket_watcher'])) { } $count = count($added); - flashAlert($count ? "Added $count watcher" . ($count === 1 ? '' : 's') : 'No new watchers - those addresses are watching already, or are not email addresses', $count ? 'success' : 'error'); + flashAlert($count ? "Added $count watcher" . ($count === 1 ? '' : 's') : 'No new watchers - those addresses are watching already, are not email addresses, or are ITFlow\'s own', $count ? 'success' : 'error'); redirect(); diff --git a/cron/mail_queue.php b/cron/mail_queue.php index 6135d3cf7..ae1d338fe 100644 --- a/cron/mail_queue.php +++ b/cron/mail_queue.php @@ -446,15 +446,16 @@ if (mysqli_num_rows($sql_queue) > 0) { continue; } - // More intelligent recipient MX check (if not disabled with --no-mx-validation) - $domain = escapeSql(substr($email_recipient, strpos($email_recipient, '@') + 1)); - if (!in_array('--no-mx-validation', $argv) && !checkdnsrr($domain, 'MX')) { - mysqli_query($mysqli, "UPDATE email_queue SET email_status = 2, email_attempts = 99, email_failed_at = NOW()" - . mailQueueErrorSql('No mail server takes email for ' . substr($email_recipient, strpos($email_recipient, '@') + 1) . ' - it has no MX record') . " WHERE email_id = $email_id"); + // A domain with no mail server (if not disabled with --no-mx-validation) - a failed try like any other, tried + // again as they are, as a lookup can fail for a moment. It used to end the email there and then + $domain_problem = in_array('--no-mx-validation', $argv) ? '' : mailQueueDomainProblem($email_recipient); + if ($domain_problem !== '') { + mysqli_query($mysqli, "UPDATE email_queue SET email_status = 2, email_attempts = 1, email_failed_at = NOW()" . mailQueueErrorSql($domain_problem) . " WHERE email_id = $email_id"); $email_to_logging = escapeSql($email_recipient); $email_subject_logging = escapeSql($rowq['email_subject']); - logApp("Cron-Mail-Queue", "Error", "Failed to send email: $email_id to $email_to_logging due to invalid recipient domain (no MX). Email subject was: $email_subject_logging"); - appNotify("Mail", "Failed to send email #$email_id to $email_to_logging due to invalid recipient domain (no MX): Email subject was: $email_subject_logging"); + logApp("Cron-Mail-Queue", "Error", "Failed to send email: $email_id to $email_to_logging - " . escapeSql($domain_problem) . ". Email subject was: $email_subject_logging"); + appNotify("Cron-Mail-Queue", "Failed to send email #$email_id to $email_to_logging - " . escapeSql($domain_problem) . " - it is tried again in " . MAIL_QUEUE_RETRY_MINUTES . " minutes", + "/admin/mail_queue.php?show=retrying", 0, $email_id); continue; } @@ -541,6 +542,11 @@ if (mysqli_num_rows($sql_failed_queue) > 0) { } try { + // Its domain asked again - a failed try, as one that won't send is, until the last of them + $domain_problem = in_array('--no-mx-validation', $argv) ? '' : mailQueueDomainProblem($email_recipient); + if ($domain_problem !== '') { + throw new \RuntimeException($domain_problem); + } sendQueueEmail( ($config_smtp_provider ?: 'standard_smtp'), (string)$config_smtp_host, diff --git a/cron/ticket_email_parser.php b/cron/ticket_email_parser.php index c2d98aa29..8503a4704 100644 --- a/cron/ticket_email_parser.php +++ b/cron/ticket_email_parser.php @@ -70,6 +70,7 @@ $allowed_extensions = array('jpg', 'jpeg', 'gif', 'png', 'webp', 'pdf', 'txt', ' $max_emails_per_run = 50; // Cap per cron run to bound memory usage (cron catches up on the next run) $max_attachment_bytes = 15728640; // 15 MB - larger attachments are skipped & logged $max_inline_embed_bytes = 2097152; // 2 MB - larger inline images are saved as regular attachments instead of shown in the ticket's text (kept as its files - functions/ticket_pictures.php) +$read_mail_minutes = 60; // Mail someone opened before this got to it is taken when it came in this long ago at most /** ------------------------------------------------------------------ * Ticket / Reply helpers (unchanged) @@ -653,6 +654,36 @@ $messages = $inbox->messages() ->unflagged() ->limit($max_emails_per_run) ->get(); +$messages = is_array($messages) ? $messages : iterator_to_array($messages, false); + +// And mail someone opened before ITFlow got to it - Outlook marks an email read as it shows it, so one opened in +// the minute before this ran was never taken. Only what came in the last $read_mail_minutes: mail read before +// that - in a mailbox used by hand for years, or while this wasn't running - stays where it is. Taken mail leaves +// the inbox for the ITFlow folder, so nothing is taken twice. Newest first, as SINCE goes by whole days, in the mail +// server's time zone - a day besides is asked for, and by its time what is older is left +if (count($messages) < $max_emails_per_run) { + $read_after = time() - $read_mail_minutes * 60; + $read_messages = $inbox->messages() + ->withHeaders() + ->withBody() + ->withFlags() + ->leaveUnread() + ->seen() + ->unflagged() + ->since(date('Y-m-d', $read_after - 86400)) + ->newest() + ->limit($max_emails_per_run - count($messages)) + ->get(); + foreach (is_array($read_messages) ? $read_messages : iterator_to_array($read_messages, false) as $read_message) { + $read_date = $read_message->date(); + if ($read_date && $read_date->getTimestamp() >= $read_after) { + $messages[] = $read_message; + } + } +} + +// ITFlow's own addresses - mail from one of them is its own come back (emailOwnAddresses()) +$own_addresses = emailOwnAddresses(); // Counters $processed_count = 0; @@ -678,6 +709,16 @@ foreach ($messages as $message) { // ticket_subject holds 500 characters - a longer one failed the insert and the email was tried again every run $subject = escapeSql(mb_substr((string)$message->subject() ?: 'No Subject', 0, 500)); + // ITFlow's own email come back - a copy to a watcher that is this mailbox, a notice sent to it. Taken, it would + // go on the ticket as a note and reopen it, or raise a ticket that sends another, round and round + if (in_array(strtolower(stripslashes($from_email)), $own_addresses, true)) { + logApp("Cron-Email-Parser", "info", "Email parser skipped an email from ITFlow's own address $from_email ($subject) - mail it sent came back to this mailbox"); + $processed_count++; + $message->markSeen(); + $message->move($targetFolderPath); + continue; + } + // Skip vacation/out-of-office auto-responders to prevent mail loops (RFC 3834) // Some* NDRs use "auto-generated" and are still handled by the NDR logic below // Todo: Combine with the NDR logic so we can update the ticket too @@ -703,11 +744,12 @@ foreach ($messages as $message) { $raw_message = (string)$message; // head + body, CRLF separated file_put_contents("../uploads/tmp/{$original_message_file}", $raw_message); - // CC (deduplicated, excluding the sender) + // CC (deduplicated, excluding the sender) - and ITFlow's own addresses: this mailbox CC'd beside a tech made a + // watcher would get every email about the ticket, each come back as a note that reopened it $ccs = array(); foreach ($message->cc() as $cc_addr) { $cc_mail = strtolower($cc_addr->email()); - if ($cc_mail && $cc_mail !== strtolower($from_email) && !in_array($cc_mail, $ccs)) { + if ($cc_mail && $cc_mail !== strtolower($from_email) && !in_array($cc_mail, $ccs) && !in_array($cc_mail, $own_addresses, true)) { $ccs[] = $cc_mail; } } diff --git a/functions/email.php b/functions/email.php index 2dd1f0d2c..269c5e294 100644 --- a/functions/email.php +++ b/functions/email.php @@ -273,6 +273,50 @@ function emailMailboxes() { return $boxes; } +/** + * ITFlow's own addresses, lower case - the mailboxes it sends from (emailMailboxes()) and the one + * it reads (Admin > Mail > Receiving). Mail from one of them is ITFlow's own come back - a copy to a + * watcher that is the ticket mailbox, say - so the ticket mailbox reader skips it, and nothing about + * a ticket is sent to one (emailOwnAddress()). + */ +function emailOwnAddresses() { + global $mysqli; + static $own = null; + if ($own === null) { + $own = []; + $reads = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_imap_username FROM settings WHERE company_id = 1"))[0] ?? ''); + foreach (array_merge(array_column(emailMailboxes(), 'email'), [$reads]) as $address) { + $address = strtolower(trim((string) $address)); + if (filter_var($address, FILTER_VALIDATE_EMAIL)) { + $own[$address] = true; + } + } + $own = array_keys($own); + } + return $own; +} + +/** Whether $email is one of ITFlow's own addresses (emailOwnAddresses()). */ +function emailOwnAddress($email) { + return in_array(strtolower(trim((string) $email)), emailOwnAddresses(), true); +} + +/* + * Whether ITFlow reads tickets from $email - email-to-ticket is on and it is the Receiving username, + * or the ticket mailbox, which Admin > Mail says to make the same one. A notice sent there would be + * read back in as an email to it. + */ +function emailReadMailbox($email) { + global $mysqli; + $email = strtolower(trim((string) $email)); + $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT config_ticket_email_parse, config_imap_provider, config_imap_username, config_ticket_from_email + FROM settings WHERE company_id = 1")) ?: []; + if ($email === '' || !intval($row['config_ticket_email_parse'] ?? 0) || trim((string) ($row['config_imap_provider'] ?? '')) === '') { + return false; + } + return in_array($email, [strtolower(trim((string) $row['config_imap_username'])), strtolower(trim((string) $row['config_ticket_from_email']))], true); +} + /** * Who an email comes from, read from the settings as saved (cron scripts keep * SQL-escaped copies): system - the mailbox for everything else (Admin > diff --git a/functions/mail_graph_mailbox.php b/functions/mail_graph_mailbox.php index 0d64e29dd..daa4c84d7 100644 --- a/functions/mail_graph_mailbox.php +++ b/functions/mail_graph_mailbox.php @@ -5,7 +5,8 @@ * cron/ticket_email_parser.php (mailGraphMailbox() in functions/mail_graph.php). * * These stand in for the parts of DirectoryTree\ImapEngine the parser uses - the - * mailbox, its folders, the query for unread mail and each message - so the + * mailbox, its folders, the queries for unread mail and for mail opened in the + * last hour, and each message - so the * parser reads either the same way. A message is downloaded whole (its MIME * source) and read by ImapEngine's own FileMessage; marking it read or flagged * and moving it go to Graph. mailGraphMailbox() loads ImapEngine first. @@ -170,12 +171,18 @@ class MailGraphFolder { } } -/* Unread, unflagged mail in a folder, oldest first - the parser's IMAP query */ +/* + * Unflagged mail in a folder - the parser's IMAP queries: unread, oldest first; or read (seen()) since + * a day (since()), newest first (newest()) - mail someone opened before the parser got to it + */ class MailGraphMessageQuery { private $mailbox; private $folder_id; private $limit = 50; + private $read = false; + private $since = '1900-01-01T00:00:00Z'; + private $order = 'asc'; public function __construct(MailGraphMailbox $mailbox, $folder_id) { $this->mailbox = $mailbox; @@ -187,7 +194,27 @@ class MailGraphMessageQuery { return $this; } - /* withHeaders(), withBody(), withFlags(), leaveUnread(), unseen(), unflagged() - always so here */ + public function seen() { + $this->read = true; + return $this; + } + + public function unseen() { + $this->read = false; + return $this; + } + + public function since($date) { + $this->since = gmdate('Y-m-d\TH:i:s\Z', strtotime(is_string($date) ? $date : $date->format('Y-m-d H:i:s'))); + return $this; + } + + public function newest() { + $this->order = 'desc'; + return $this; + } + + /* withHeaders(), withBody(), withFlags(), leaveUnread(), unflagged() - always so here */ public function __call($name, $arguments) { return $this; } @@ -196,9 +223,9 @@ class MailGraphMessageQuery { // Oldest first, as IMAP reads them (Graph wants the $orderby property first in the $filter). Flagged mail - what // the parser couldn't take - is skipped here, so pages are read on (up to 20) until there are enough to work on: // a pile of flagged mail at the oldest end mustn't hide newer mail for good - $filter = rawurlencode('receivedDateTime ge 1900-01-01T00:00:00Z and isRead eq false'); + $filter = rawurlencode("receivedDateTime ge $this->since and isRead eq " . ($this->read ? 'true' : 'false')); $path = '/mailFolders/' . rawurlencode($this->folder_id) - . "/messages?\$filter=$filter&\$orderby=receivedDateTime%20asc&\$select=id,receivedDateTime,flag&\$top=" . min(1000, $this->limit * 4); + . "/messages?\$filter=$filter&\$orderby=receivedDateTime%20$this->order&\$select=id,receivedDateTime,flag&\$top=" . min(1000, $this->limit * 4); $rows = []; $skipped = 0; $next = ''; @@ -223,7 +250,7 @@ class MailGraphMessageQuery { } $next = (string) ($list['json']['@odata.nextLink'] ?? ''); } - if (!$rows && $skipped >= $this->limit * 4) { + if (!$this->read && !$rows && $skipped >= $this->limit * 4) { logApp("Cron-Email-Parser", "warning", "Microsoft Graph: $skipped unread emails in the mailbox are flagged - ones the parser couldn't take - look at them and clear the flags"); } diff --git a/functions/mail_queue.php b/functions/mail_queue.php index 4cb1ea54b..625fea4b4 100644 --- a/functions/mail_queue.php +++ b/functions/mail_queue.php @@ -9,7 +9,9 @@ * 0 waiting - or scheduled, while its email_queued_at is still ahead * 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) + * MAIL_QUEUE_TRIES; 99 tries means it isn't tried again (a bad address). + * A domain with no mail server is a failed try like any other + * (mailQueueDomainProblem()), as a lookup can fail for a moment * 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), @@ -28,6 +30,23 @@ const MAIL_QUEUE_CANCELLED = 4; // Pictures that were in an email as data, kept once each for the queue to attach inline (emailInlineImages()) - the web can't reach uploads/tmp const MAIL_QUEUE_IMAGE_DIR = 'uploads/tmp/mail_images'; +/* + * Why mail can't go to $recipient's domain - '' when it can: it has a mail server (MX), or, with + * none, an address of its own (A or AAAA), which takes its mail (RFC 5321). Asked before each try + * (cron/mail_queue.php), so a lookup that failed for a moment is asked again on the next try + * rather than ending the email. + */ +function mailQueueDomainProblem($recipient) { + static $asked = []; // each domain once a run - a queue of email to one client asks once + $recipient = (string) $recipient; + $domain = strtolower(trim(substr($recipient, strrpos($recipient, '@') + 1))); + if (!isset($asked[$domain])) { + $asked[$domain] = $domain === '' || checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A') || checkdnsrr($domain, 'AAAA') + ? '' : "No mail server takes email for $domain - it has no MX record, and no address either"; + } + return $asked[$domain]; +} + /* The tabs of the list - key => [label, icon, SQL for what is in it]. */ function mailQueueTabs() { $tries = MAIL_QUEUE_TRIES; diff --git a/functions/ticket.php b/functions/ticket.php index ae4a05099..ea8d86ee6 100644 --- a/functions/ticket.php +++ b/functions/ticket.php @@ -247,12 +247,15 @@ function ticketWatcherEmails($input) { return array_keys($emails); } -/** Add watchers a ticket doesn't have already - the ones added. */ +/** Add watchers a ticket doesn't have already - the ones added. Never one of ITFlow's own addresses (emailOwnAddresses()). */ function ticketAddWatchers($ticket_id, array $emails) { global $mysqli; $ticket_id = intval($ticket_id); $added = []; foreach ($emails as $email) { + if (emailOwnAddress($email)) { + continue; + } $email_sql = escapeSql($email); if (mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM ticket_watchers WHERE watcher_ticket_id = $ticket_id AND watcher_email = '$email_sql' LIMIT 1"))) { continue; @@ -712,6 +715,10 @@ function ticketMailSend($ticket_id, $subject, array $email, array $opts = []) { } } } + // Never to one of ITFlow's own addresses - a watcher that is the ticket mailbox would read it back in (emailOwnAddresses()) + $recipients = array_values(array_filter($recipients, function ($recipient) { + return !emailOwnAddress($recipient['email'] ?? ''); + })); return count(emailQueue($recipients, $subject, $email, ['from' => ticketEmailFrom(), 'attachments' => $opts['attachments'] ?? [], 'cal_str' => $opts['cal_str'] ?? '', 'ticket_reply_id' => intval($opts['ticket_reply_id'] ?? 0), 'ticket_id' => intval($ticket_id)])); } @@ -744,7 +751,7 @@ function ticketColleagueRecipients($ticket_id, $kind) { AND contact_company_ticket_emails IN ($wanted)"); while ($row = mysqli_fetch_assoc($sql)) { $email = trim((string) $row['contact_email']); - if (filter_var($email, FILTER_VALIDATE_EMAIL)) { + if (filter_var($email, FILTER_VALIDATE_EMAIL) && !emailOwnAddress($email)) { $recipients[$email] = ['name' => (string) $row['contact_name'], 'client_name' => (string) $ticket['client_name'], 'portal' => !empty($row['user_id'])]; } } @@ -1069,7 +1076,9 @@ function ticketMailNewTicketAddress($ticket_id, $how) { global $mysqli, $config_app_name; $address = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_ticket_new_ticket_notification_email FROM settings WHERE company_id = 1"))[0] ?? ''); $row = ticketRow($ticket_id); - if (!filter_var($address, FILTER_VALIDATE_EMAIL) || !$row) { + // Not to the mailbox ITFlow reads tickets from - it would come back in as an email, and that one raise another + // ticket (emailReadMailbox()). Admin > Ticket won't save it now; one saved before is skipped here + if (!filter_var($address, FILTER_VALIDATE_EMAIL) || !$row || emailReadMailbox($address)) { return 0; } $client = !empty($row['client_name']) ? $row['client_name'] : 'Guest'; diff --git a/functions/ticket_mail_try.php b/functions/ticket_mail_try.php index 1cf47489c..af0a39761 100644 --- a/functions/ticket_mail_try.php +++ b/functions/ticket_mail_try.php @@ -6,6 +6,7 @@ * this address with this subject - without doing any of it. It asks the same * questions in the same order, against the same records: * + * 0. ITFlow's own email come back (emailOwnAddresses()) is skipped; * 1. an automatic reply (out of office) is skipped; * 2. a ticket number in [brackets] in the subject - a reply on that ticket, * as whoever sent it is to the ticket (its contact or another of the @@ -42,6 +43,12 @@ function ticketMailTry($from_email, $subject, $auto_reply = false) { return $end('fa-power-off', 'secondary', 'Nothing - the mailbox isn\'t read', 'Email to ticket is off, so the parser leaves the mailbox alone.'); } + if (emailOwnAddress($from_email)) { + $steps[] = ['From one of ITFlow\'s own addresses?', 'Yes']; + return $end('fa-sync-alt', 'secondary', 'Skipped', 'It is ITFlow\'s own email come back - a copy to this mailbox, or a notice sent to it - so it is moved to the ITFlow folder without a ticket. Taken, it would reopen the ticket, or raise one that sends another.'); + } + $steps[] = ['From one of ITFlow\'s own addresses?', 'No']; + if ($auto_reply) { $steps[] = ['An automatic reply?', 'Yes']; return $end('fa-forward', 'secondary', 'Skipped', 'Out-of-office and other automatic replies are moved to the ITFlow folder without a ticket, so two systems can\'t answer each other forever. Agents get a notice.'); diff --git a/functions/ticket_reply_recipients.php b/functions/ticket_reply_recipients.php index 6f4b26533..84e73ffdf 100644 --- a/functions/ticket_reply_recipients.php +++ b/functions/ticket_reply_recipients.php @@ -64,9 +64,10 @@ function ticketReplyRecipients($ticket_id) { } $client_id = intval($ticket['ticket_client_id']); $list = []; + // Never one of ITFlow's own addresses - an email to it would be read back in (emailOwnAddresses()) $add = function ($email, $name, $group, $default, $look = '') use (&$list) { $email = strtolower(trim((string) $email)); - if ($email === '' || strlen($email) > 255 || !filter_var($email, FILTER_VALIDATE_EMAIL) || isset($list[$email])) { + if ($email === '' || strlen($email) > 255 || !filter_var($email, FILTER_VALIDATE_EMAIL) || isset($list[$email]) || emailOwnAddress($email)) { return; } $list[$email] = ['name' => trim((string) $name), 'group' => $group, 'default' => $default, 'send' => $default, 'look' => $look];