diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fe226eaa..68522cb4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This file documents all notable changes made to ITFlow. ### Upgrading to 26.12.3 -Update from Maintenance > Update as usual - this release has eleven database changes: 4.2.2 adds a table for passkeys and a setting to turn them on or off, 4.2.3 lets a document keep the Markdown it was written in, 4.2.4 lets a ticket keep its pictures as files, 4.2.5 adds identity providers, 4.2.6 keeps each ticket's email conversation together, 4.2.7 lets a reply from a CC say who sent it, 4.2.8 renames the watchers table for CCs, 4.2.9 lets the mail queue go at a pace your mail host takes, 4.3.0 lets contacts unsubscribe from bulk email, 4.3.1 encrypts the mail passwords and tokens, and 4.3.2 keeps the email the ticket mailbox couldn't take, for your team to sort. The client portal's Microsoft 365 single sign-on becomes the first identity provider, for client contacts, and keeps its redirect address, so nothing changes in Microsoft Entra. Nothing else is required: passkeys are on, and no one has one until they add it. Pictures already inside tickets move to files in the background afterwards - the new Ticket Pictures job in Maintenance > Cron does a batch a minute, so an install with years of emailed screenshots may take a while, and tickets work as usual meanwhile. A passkey is made for your ITFlow's address (the one in config.php) and only works there, over HTTPS - if you move ITFlow to a new address, everyone adds theirs again. Watchers are called CCs now. Emails about a ticket go to its contact with the CCs copied in, rather than one each - Admin > Settings > Tickets has the switch, under Emails about a ticket. On nginx, where ITFlow's .htaccess files aren't read, add `location ^~ /uploads/mail_unprocessed/ { deny all; }` beside the other private folders - it holds copies of the email kept for Unprocessed Email. +Update from Maintenance > Update as usual - this release has twelve database changes: 4.2.2 adds a table for passkeys and a setting to turn them on or off, 4.2.3 lets a document keep the Markdown it was written in, 4.2.4 lets a ticket keep its pictures as files, 4.2.5 adds identity providers, 4.2.6 keeps each ticket's email conversation together, 4.2.7 lets a reply from a CC say who sent it, 4.2.8 renames the watchers table for CCs, 4.2.9 lets the mail queue go at a pace your mail host takes, 4.3.0 lets contacts unsubscribe from bulk email, 4.3.1 encrypts the mail passwords and tokens, 4.3.2 keeps the email the ticket mailbox couldn't take, for your team to sort, and 4.3.3 adds Undo send and Reply-To for ticket emails. The client portal's Microsoft 365 single sign-on becomes the first identity provider, for client contacts, and keeps its redirect address, so nothing changes in Microsoft Entra. Nothing else is required: passkeys are on, and no one has one until they add it. Pictures already inside tickets move to files in the background afterwards - the new Ticket Pictures job in Maintenance > Cron does a batch a minute, so an install with years of emailed screenshots may take a while, and tickets work as usual meanwhile. A passkey is made for your ITFlow's address (the one in config.php) and only works there, over HTTPS - if you move ITFlow to a new address, everyone adds theirs again. Watchers are called CCs now. Emails about a ticket go to its contact with the CCs copied in, rather than one each - Admin > Settings > Tickets has the switch, under Emails about a ticket. On nginx, where ITFlow's .htaccess files aren't read, add `location ^~ /uploads/mail_unprocessed/ { deny all; }` beside the other private folders - it holds copies of the email kept for Unprocessed Email. Emailed and texted replies now wait 10 seconds on the ticket page, with Undo, before they go - Admin > Settings > Tickets sets how long, or turns it off. Ticket emails sent from another address than the mailbox ITFlow reads now ask for replies at that mailbox (Reply-To) - turn that off in the same place if the From address is an alias of that mailbox and you'd rather clients only saw it. ### Security @@ -81,6 +81,8 @@ Update from Maintenance > Update as usual - this release has eleven database cha - An agent who answers the client's email from their own mailbox - reply to all, with the support address copied in - has it on the ticket as their reply, not an internal note: the client has it, so it shows as emailed to them and counts as the ticket's first response, and the ticket is theirs when nobody had it, as a reply from the ticket does. Nothing more is sent. An email only to the support address is still their internal note. - Unprocessed Email: email the ticket mailbox couldn't make a ticket from - someone who isn't a contact, at a domain no client has, while unknown senders aren't let in, or one that failed - is kept in Tickets > Unprocessed Email (in the menu beside New Ticket) and moved to the mailbox's ITFlow folder, where it used to stay in the inbox flagged and unread for someone to find. The tickets list says how many are waiting. Each shows who it is from, its subject, a line of what it says and why it wasn't taken. - From Unprocessed Email an email can be opened (as text - nothing in it loads), made a ticket at the client you pick - for one of their contacts, or with the sender made a contact there first - added to a ticket, with the sender made a CC on it if you like, tried again (after making them a contact, say), dismissed, or downloaded as a .eml file. Try Again and Dismiss also work on several at once. The ticket or reply is made within a minute, as any email is, attachments and all, and the email shows which ticket it went on. Sorted and dismissed email is kept 30 days, and email nobody sorts 90. +- Undo send: an emailed or texted reply waits a few seconds under the reply box - "Emailing it in 10 seconds" - with Undo and Send now. Undo leaves it in the reply box, as it was, to change or send later: nothing about it has happened yet - no email, no status change, no time added. Escape undoes it too, and leaving the page while it waits asks first. Admin > Settings > Tickets, Emails about a ticket, sets how long - 5, 10 (to start), 20 or 30 seconds - or turns it off. A public or internal note goes at once, as before. +- Replies to ticket emails come back: an email about a ticket sent from another address than the mailbox ITFlow reads - a no-reply address, or one that isn't delivered there - asks for replies at that mailbox (Reply-To), where a client's reply used to go to the From address and be lost if nobody read it. Admin > Settings > Tickets says which addresses it uses, and turns it off for a From address that is an alias of the mailbox. ### Bug Fixes @@ -163,6 +165,7 @@ Update from Maintenance > Update as usual - this release has eleven database cha - Mail review stage 6: database 4.3.0 adds contacts.contact_unsubscribed_at and settings.config_mail_unsubscribe_secret. functions/contact_unsubscribe.php signs each contact's link with an HMAC of the contact id and address (contactUnsubscribeToken(), the key made on first use), and contactUnsubscribeSet() takes every contact with the address out or back in; contactUnsubscribedAddresses() is checked at send time, so a contact added since at another client is out too. guest/unsubscribe.php asks on GET, acts on its form's POST, and takes RFC 8058's List-Unsubscribe=One-Click POST with no page. emailBulkRecipients() (functions/email.php) builds the recipients from contact rows - the bounced, the unsubscribed, no address and the same address left out and counted - each with an unsubscribe link (emailHtml()'s new unsubscribe field) and its own List-Unsubscribe and List-Unsubscribe-Post headers (emailQueue() recipients' headers, let through by MAIL_QUEUE_HEADERS); emailBulkPost() now takes contact rows and converts Send at with timezoneFromView(). sendQueueEmail() sets the envelope sender per email - setFrom(..., false) with Sender reset, as PHPMailer's setFrom() kept the first email's From as Sender for the whole kept-alive connection - and for email_bulk passes mailQueueReturnPath(), the read mailbox when ticket parsing is on, the provider isn't Graph sending, the SMTP username is empty or that mailbox, and the domains align; a MAIL FROM refusal sends it again from the From address and sets mailQueueReturnPathRefused() for the run. mailQueueBouncesTo() tells the Email window. - Mail review stage 7a: database 4.3.1 widens config_smtp_password, config_imap_password and config_mail_oauth_client_secret to varchar(500) and encrypts what is saved. functions/mail_secret.php seals MAIL_SECRET_COLUMNS (the two passwords, the OAuth secret, refresh and access tokens, and config_mail_graph_token) with secretSeal($plain, 'mail') as RMM and identity provider secrets are - but keeps a value as typed when there is no key, as mail would otherwise stop. mailSecretOpen() reads an unsealed value as it is, so nothing breaks before it is sealed: load_global_settings.php, cron/mail_queue.php, cron/nightly_tasks.php and mailGraphToken() open them; settingsWrite() seals secret-kind mail columns; every token write (the parser's two refreshes, persistMailOauthTokens(), the Microsoft callback, the Test OAuth refresh, mailGraphToken()) goes through mailSecretSql(). mailSecretsSealAll() runs in the update and nightly; mailSecretProblem() is the Admin > Settings > Mail warning. Remote pictures: ticketHtmlForAgents($purifier, $html) purifies and then ticketRemoteImagesHold() moves an http(s) or // img src to data-remote-src (a blank GIF and .itflow-remote-image in its place) and a style with a remote url() to data-remote-style - in agent/ticket.php, the quick look and ticketWorkOrderHtml(); ticketRemoteImagesNote() and js/app.js show the note and put them back. login.php drops the SMTP host, port, encryption, username and password it loaded but never used. - Mail review stage 7b: database 4.3.2 adds email_unprocessed. functions/mail_unprocessed.php keeps an email the parser couldn't take - mailUnprocessedKeep($raw, $meta, $reason) writes its MIME to uploads/mail_unprocessed (denied to the web, as uploads/backups) and a row, one per Message-ID - and mailUnprocessedDecide() records an agent's ticket, reply, retry or dismiss (agent/email_unprocessed.php, agent/post/email_unprocessed.php). cron/ticket_email_parser.php appends mailUnprocessedAsked() to the run as MailUnprocessedMessage (functions/mail_unprocessed_message.php), a FileMessage whose move() is mailUnprocessedDone() with the ticket it went on and whose flags do nothing: a ticket or reply ask skips steps 1 to 6 for addTicket() at the chosen contact or client (or a new contact, as the domain match makes one) or addReply() on the chosen ticket (the sender added through ticketAddCcs() first, if asked); a retry goes the usual way. mailUnprocessedLeave() takes the unprocessed and exception branches - kept, then marked seen and moved to the ITFlow folder; flagged and unread as before when it can't be kept; an ask that fails waits again with the reason. mailUnprocessedCleanup() runs nightly. +- Mail review stage 7c: database 4.3.3 adds settings.config_ticket_reply_undo_seconds (0, 5, 10, 20 or 30 - TICKET_REPLY_UNDO_CHOICES) and config_ticket_reply_to_read, read by ticketReplySendSettings() (functions/ticket_reply_send.php). Undo send is the ticket page alone: #ticketReplyForm's data-undo-seconds, and agent/js/ticket_reply_extras.js holds an emailed or texted reply's submit in a window listener - after the form's own checks and the canned response check on the document - with the reply card inert and the [data-reply-hold] bar, then requestSubmit()s it with holdOk, cannedSendAnyway and noTimeOk set; agent/post/ticket.php is unchanged. emailQueue() puts Reply-To (ticketMailReplyToRead(): email to ticket on, the setting on, the IMAP username an address and not From) in a thread email's headers; MAIL_QUEUE_HEADERS lets one bare address through and sendQueueEmail() gives it to addReplyTo(), so Graph's large-email draft carries it too. ## [26.12.2] diff --git a/admin/database_updates/4.3.3.php b/admin/database_updates/4.3.3.php new file mode 100644 index 000000000..39180764f --- /dev/null +++ b/admin/database_updates/4.3.3.php @@ -0,0 +1,28 @@ + Settings > Tickets: + * settings.config_ticket_reply_undo_seconds - how long an emailed or texted reply waits on the ticket page, with + * Undo, before it goes: 0 (at once), 5, 10, 20 or 30 seconds - 10 to start + * settings.config_ticket_reply_to_read - a ticket's email from another address than the mailbox ITFlow reads asks + * for replies at that mailbox (Reply-To) - on to start + * + * Re-runnable: each column is added only where it isn't there. + */ + +defined('FROM_DB_UPDATER') || die("Direct file access is not allowed"); + + $itflow_reply_send_columns = [ + ['config_ticket_reply_undo_seconds', "tinyint(3) NOT NULL DEFAULT 10 AFTER `config_ticket_email_relay`"], + ['config_ticket_reply_to_read', "tinyint(1) NOT NULL DEFAULT 1 AFTER `config_ticket_reply_undo_seconds`"], + ]; + foreach ($itflow_reply_send_columns as [$itflow_column, $itflow_definition]) { + $itflow_column_exists = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'settings' AND COLUMN_NAME = '$itflow_column' LIMIT 1"); + if (!$itflow_column_exists || mysqli_num_rows($itflow_column_exists) === 0) { + mysqli_query($mysqli, "ALTER TABLE `settings` ADD `$itflow_column` $itflow_definition"); + } + } diff --git a/admin/post/settings_ticket.php b/admin/post/settings_ticket.php index c04bfb97a..cb4a43394 100644 --- a/admin/post/settings_ticket.php +++ b/admin/post/settings_ticket.php @@ -2,7 +2,7 @@ /* * Admin > Settings > Tickets (admin/settings_ticket.php): - * edit_ticket_settings saves the numbering, email to ticket, the emails about a ticket, text to ticket, who hears of a + * edit_ticket_settings saves the numbering, email to ticket, the emails about a ticket (with undo send and Reply-To), text to ticket, who hears of a * new ticket and whether AI triages it, whether the first agent to reply to an * unassigned ticket is assigned it, what a quick ticket starts with, the timer * and billing, the messages to clients, closing and the tickets page. A next @@ -24,6 +24,7 @@ if (isset($_POST['edit_ticket_settings'])) { 'config_ticket_default_view', 'config_ticket_ordering', 'config_ticket_moving_columns'], aiTriageReady() ? ['config_ticket_ai_triage'] : [], version_compare((string) CURRENT_DATABASE_VERSION, '4.0.9', '>=') ? ['config_ticket_reply_assigns'] : [], ticketMailThreadReady() ? ['config_ticket_email_copy', 'config_ticket_email_relay'] : [], + ticketReplySendReady() ? ['config_ticket_reply_undo_seconds', 'config_ticket_reply_to_read'] : [], settingsColumnExists($mysqli, 'config_ticket_sms_parse') ? ['config_ticket_sms_parse', 'config_ticket_sms_unknown_senders', 'config_ticket_sms_acknowledge'] : [], ticketStatusReplyReady() ? ['config_ticket_assigned_message'] : [], ticketNoResponseReady() ? ['config_ticket_no_response_message'] : [], quickTicketReady() ? ['config_quick_ticket_on', 'config_quick_ticket_assign', 'config_quick_ticket_status', 'config_quick_ticket_priority', 'config_quick_ticket_category', @@ -54,6 +55,14 @@ if (isset($_POST['edit_ticket_settings'])) { $after[$switch] = empty($_POST[$switch]) ? 0 : 1; } } + // Undo send and Reply-To - once the database has them (functions/ticket_reply_send.php) + if (array_key_exists('config_ticket_reply_to_read', $before)) { + $after['config_ticket_reply_to_read'] = empty($_POST['config_ticket_reply_to_read']) ? 0 : 1; + $after['config_ticket_reply_undo_seconds'] = intval($in['config_ticket_reply_undo_seconds']); + if (!isset(TICKET_REPLY_UNDO_CHOICES[$after['config_ticket_reply_undo_seconds']])) { + $errors['config_ticket_reply_undo_seconds'] = 'Pick how long from the list.'; + } + } // Text to ticket - once the database has it if (array_key_exists('config_ticket_sms_parse', $before)) { foreach (['config_ticket_sms_parse', 'config_ticket_sms_unknown_senders', 'config_ticket_sms_acknowledge'] as $switch) { diff --git a/admin/settings_ticket.php b/admin/settings_ticket.php index 8870a0c0a..28d009238 100644 --- a/admin/settings_ticket.php +++ b/admin/settings_ticket.php @@ -47,6 +47,21 @@ echo settingsPageStart('ticket'); [' . escapeHtml($config_ticket_prefix) . '123] Subject, the same from the first to the last, and carries the headers a mail program keeps a conversation together by - so they stay together in the client\'s inbox, and a reply finds its ticket even when its subject was changed.') ?> 'An email about a ticket goes to its contact with the CCs, and the colleagues who asked for every update, copied in - everyone sees who else has it, and a reply to all reaches them. Off, each gets an email of their own.']) ?> 'When the client replies - by email, text or in the portal - the people an agent\'s reply would go to get it too, with its files, unless it was already to them. By email, only a reply to one of the ticket\'s emails: anyone can put a ticket number in a subject.']) ?> + ' . escapeHtml($ticket_from) . ', the mailbox ITFlow reads.'; + } else { + $reply_to_help = 'Ticket emails are from ' . escapeHtml($ticket_from ?: 'the system mailbox') . '. On, they ask for replies at ' . escapeHtml($reply_send['reads']) + . ', the mailbox ITFlow reads (Reply-To), so a reply isn\'t lost on an address nobody reads. Turn it off if ' . escapeHtml($ticket_from ?: 'that address') . ' already comes to that mailbox and you\'d rather clients only saw it.'; + } ?> + 'fa-undo-alt', + 'help' => 'An emailed or texted reply waits this long on the ticket page, with Undo, before it goes - Undo leaves it in the reply box to change. Nothing about it happens until then.']) ?> + $reply_to_help]) ?> + diff --git a/agent/js/ticket_reply_extras.js b/agent/js/ticket_reply_extras.js index b0951efdd..d8df83774 100644 --- a/agent/js/ticket_reply_extras.js +++ b/agent/js/ticket_reply_extras.js @@ -9,6 +9,10 @@ * - +15m, +30m and +1h add to Time worked. The timer stops at the new total, as it does when the * time is typed. * - A reply with something in it and no time, on a billable ticket, asks first. + * - Undo send: an emailed or texted reply waits the form's data-undo-seconds (Admin > Settings > Tickets, + * functions/ticket_reply_send.php) under the reply box, locked, with Undo and Send now. Nothing is posted until then, + * so Undo leaves everything as it was, the reply still in the box and kept as a draft. Last of the checks: it holds + * only a reply nothing else held back, and lets it go with every check already answered. */ (function () { 'use strict'; @@ -353,4 +357,114 @@ } else { form.addEventListener('submit', onSubmit); } + + // ---- Undo send + + var holdSeconds = parseInt(form.dataset.undoSeconds || '0', 10) || 0; + var hold = form.querySelector('[data-reply-hold]'); + var card = form.querySelector('.card'); + var holding = null; + + function replyType() { + var picked = form.querySelector('input[name="public_reply_type"]:checked'); + return picked ? picked.value : ''; + } + + function stopHold() { + if (!holding) { + return null; + } + var submitter = holding.submitter; + clearInterval(holding.timer); + holding = null; + hold.hidden = true; + if (card) { + card.inert = false; + card.classList.remove('opacity-50'); + } + return submitter; + } + + // Go - every check before this one was answered on the way in, so none asks again + function release() { + var submitter = stopHold(); + if (submitter === null) { + return; + } + form.dataset.holdOk = '1'; + form.dataset.cannedSendAnyway = '1'; + form.dataset.noTimeOk = '1'; + if (typeof form.requestSubmit === 'function') { + form.requestSubmit(submitter); + } else { + form.submit(); + } + } + + function undo() { + if (stopHold() === null) { + return; + } + setStatus('Not sent - it is still here'); + var ed = editor(); + if (ed) { + ed.focus(); + } + } + + if (holdSeconds > 0 && hold) { + // On the window, so it hears the submit after the form's checks and the canned response check (on the document) + window.addEventListener('submit', function (e) { + if (e.target !== form) { + return; + } + // Already waiting - a second Send waits with it + if (holding) { + e.preventDefault(); + return; + } + var let_go = form.dataset.holdOk === '1'; + delete form.dataset.holdOk; + var type = replyType(); + if (let_go || e.defaultPrevented || (type !== '2' && type !== '3') || !hasWords()) { + return; + } + e.preventDefault(); + var left = holdSeconds; + var count = hold.querySelector('[data-reply-hold-count]'); + hold.querySelector('[data-reply-hold-what]').textContent = type === '3' ? 'Texting it' : 'Emailing it'; + count.textContent = left; + hold.hidden = false; + if (card) { + card.inert = true; + card.classList.add('opacity-50'); + } + holding = { + submitter: e.submitter || form.querySelector('#ticket_add_reply'), + timer: setInterval(function () { + left -= 1; + count.textContent = Math.max(0, left); + if (left <= 0) { + release(); + } + }, 1000) + }; + hold.querySelector('[data-reply-hold-undo]').focus(); + }); + hold.querySelector('[data-reply-hold-undo]').addEventListener('click', undo); + hold.querySelector('[data-reply-hold-now]').addEventListener('click', release); + document.addEventListener('keydown', function (e) { + if (holding && e.key === 'Escape') { + e.preventDefault(); + undo(); + } + }); + // Leaving now would lose the send - the draft is kept, but say so first + window.addEventListener('beforeunload', function (e) { + if (holding) { + e.preventDefault(); + e.returnValue = ''; + } + }); + } })(); diff --git a/agent/ticket.php b/agent/ticket.php index 9a0484b75..446e504a0 100644 --- a/agent/ticket.php +++ b/agent/ticket.php @@ -861,7 +861,8 @@ if (isset($_GET['ticket_id'])) { ?>
+ data-billable="" data-drafts="" + data-undo-seconds=""> @@ -1061,6 +1062,14 @@ if (isset($_GET['ticket_id'])) { + + +
diff --git a/cron/mail_queue.php b/cron/mail_queue.php index 53ce0d4de..be74d4819 100644 --- a/cron/mail_queue.php +++ b/cron/mail_queue.php @@ -361,6 +361,11 @@ function sendQueueEmail(array $mailer, string $from_email, string $from_name, st foreach ($headers as $name => $value) { if ($name === 'Message-ID') { $mail->MessageID = $value; + } elseif ($name === 'Reply-To') { + // PHPMailer's own, so there is one Reply-To - and Graph's large-email draft carries it too (mailGraphSendLarge()) + if (filter_var($value, FILTER_VALIDATE_EMAIL)) { + $mail->addReplyTo($value); + } } else { $mail->addCustomHeader($name, $value); } diff --git a/db.sql b/db.sql index 053eb027f..e215fb5f5 100644 --- a/db.sql +++ b/db.sql @@ -4155,6 +4155,8 @@ CREATE TABLE `settings` ( `config_ticket_client_general_notifications` tinyint(1) NOT NULL DEFAULT 1, `config_ticket_email_copy` tinyint(1) NOT NULL DEFAULT 1, `config_ticket_email_relay` tinyint(1) NOT NULL DEFAULT 1, + `config_ticket_reply_undo_seconds` tinyint(3) NOT NULL DEFAULT 10, + `config_ticket_reply_to_read` tinyint(1) NOT NULL DEFAULT 1, `config_ticket_autoclose_hours` int(5) NOT NULL DEFAULT 72, `config_ticket_new_ticket_notification_email` varchar(200) DEFAULT NULL, `config_ticket_ai_triage` tinyint(1) NOT NULL DEFAULT 0, diff --git a/functions.php b/functions.php index 19e7be201..1b7d1e6d1 100644 --- a/functions.php +++ b/functions.php @@ -149,6 +149,7 @@ require_once __DIR__ . '/functions/settings.php'; require_once __DIR__ . '/functions/mail_queue.php'; require_once __DIR__ . '/functions/mail_secret.php'; require_once __DIR__ . '/functions/mail_unprocessed.php'; +require_once __DIR__ . '/functions/ticket_reply_send.php'; require_once __DIR__ . '/functions/mail_graph.php'; require_once __DIR__ . '/functions/purchase_orders.php'; require_once __DIR__ . '/functions/kb.php'; diff --git a/functions/email.php b/functions/email.php index ef7b09f48..51767255d 100644 --- a/functions/email.php +++ b/functions/email.php @@ -554,7 +554,9 @@ function emailInlineImages($html, array $ticket = []) { * update, which gives the queue somewhere to keep them, one each * thread the ticket whose conversation it is in - it gets a Message-ID of its own and * In-Reply-To and References naming the emails before it (ticketMailHeaders()), - * and answering, the Message-ID of the email it answers, when it answers one + * and answering, the Message-ID of the email it answers, when it answers one. From + * another address than the mailbox ITFlow reads, it asks for replies there + * (Reply-To, ticketMailReplyToRead()) * auto replied (an automatic answer to an email) or generated (sent by ITFlow by * 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 @@ -589,6 +591,8 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = []) // Where the ticket's conversation stands as it is written - each email answers that $thread_id = intval($opts['thread'] ?? 0); $thread = $thread_id && ticketMailThreadReady() ? ticketMailThread($thread_id, (string) ($opts['answering'] ?? '')) : null; + // Replies to the mailbox ITFlow reads, when it is sent from another address (functions/ticket_reply_send.php) + $reply_to = $thread_id ? ticketMailReplyToRead((string) $from['email']) : ''; $auto_headers = !empty($opts['auto']) ? ticketMailAutoHeaders((string) $opts['auto']) : []; $sent = []; @@ -601,7 +605,8 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = []) 'name' => array_key_exists('greet', $recipient) ? $recipient['greet'] : $name, ], $footnote !== null ? ['footnote' => $footnote] : [], !empty($recipient['unsubscribe']) && !$together ? ['unsubscribe' => (string) $recipient['unsubscribe']] : [])); // 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'] ?? []) : []); + $headers = ($thread !== null ? ticketMailHeaders($thread_id, (string) $from['email'], $thread) : []) + $auto_headers + (!$together ? (array) ($recipient['headers'] ?? []) : []) + + ($reply_to !== '' ? ['Reply-To' => $reply_to] : []); $went = []; foreach (array_merge([$recipient], $cc) as $person) { $went[strtolower($person['email'])] = $person['name'] !== '' ? $person['name'] . ' (' . $person['email'] . ')' : $person['email']; diff --git a/functions/mail_queue.php b/functions/mail_queue.php index cc7a654d1..1d95d411e 100644 --- a/functions/mail_queue.php +++ b/functions/mail_queue.php @@ -526,6 +526,8 @@ const MAIL_QUEUE_HEADERS = [ // Bulk email's link to leave, which mail programs show as their own Unsubscribe - one click (RFC 8058, functions/contact_unsubscribe.php) 'List-Unsubscribe' => '/^\s]+>$/', 'List-Unsubscribe-Post' => '/^List-Unsubscribe=One-Click$/', + // A ticket's email from another address than the mailbox ITFlow reads asks for replies there - one address (functions/ticket_reply_send.php) + 'Reply-To' => '/^[^<>\s@,;:"()\[\]\\\\]+@[^<>\s@,;:"()\[\]\\\\]+$/', ]; /* diff --git a/functions/settings.php b/functions/settings.php index e127bea81..930bd9402 100644 --- a/functions/settings.php +++ b/functions/settings.php @@ -268,6 +268,10 @@ function settingsFields() { 'words' => 'cc ccs copy copied watchers one email each separate recipients reply all'] + $on_off, 'config_ticket_email_relay' => ['page' => 'ticket', 'section' => 'Emails about a ticket', 'label' => 'Pass a client\'s reply on to the CCs', 'words' => 'relay forward client reply cc ccs watchers copied notify pass on'] + $on_off, + 'config_ticket_reply_undo_seconds' => ['page' => 'ticket', 'section' => 'Emails about a ticket', 'label' => 'Undo send', 'kind' => 'select', 'options' => TICKET_REPLY_UNDO_CHOICES, + 'words' => 'undo send delay hold back cancel recall unsend reply email text seconds'], + 'config_ticket_reply_to_read' => ['page' => 'ticket', 'section' => 'Emails about a ticket', 'label' => 'Replies come to the mailbox ITFlow reads', + 'words' => 'reply-to reply to replies mailbox read alias from address lost'] + $on_off, 'config_ticket_sms_parse' => ['page' => 'ticket', 'section' => 'Text to ticket', 'label' => 'Turn texts into tickets', 'words' => 'text sms message mobile phone skyetel to ticket'] + $on_off, 'config_ticket_sms_unknown_senders' => ['page' => 'ticket', 'section' => 'Text to ticket', 'label' => 'From unknown numbers too', 'words' => 'text sms unknown numbers anyone'] + $on_off, 'config_ticket_sms_acknowledge' => ['page' => 'ticket', 'section' => 'Text to ticket', 'label' => 'Text back the ticket number', 'words' => 'text sms acknowledge reply confirm number'] + $on_off, diff --git a/functions/ticket_reply_send.php b/functions/ticket_reply_send.php new file mode 100644 index 000000000..0bd4cacf1 --- /dev/null +++ b/functions/ticket_reply_send.php @@ -0,0 +1,62 @@ + Settings > Tickets, Emails about a ticket (4.3.3): + * + * - Undo send: an emailed or texted reply waits config_ticket_reply_undo_seconds on the ticket page, with Undo, before + * it is posted (agent/js/ticket_reply_extras.js) - Undo leaves it in the reply box, kept as a draft. Nothing reaches + * the server until then, so there is nothing to take back: not the email, the status, the time or who it's assigned. + * - Reply-To: an email in a ticket's conversation from an address other than the mailbox ITFlow reads asks for replies + * at that mailbox (Reply-To), so a reply isn't lost on an address nobody reads - config_ticket_reply_to_read, on. + */ + +const TICKET_REPLY_UNDO_CHOICES = [0 => 'Off - send at once', 5 => '5 seconds', 10 => '10 seconds', 20 => '20 seconds', 30 => '30 seconds']; +const TICKET_REPLY_UNDO_DEFAULT = 10; + +/** Whether the 4.3.3 update has run - the settings are there. */ +function ticketReplySendReady() { + 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.3', '>='); + } + return $ready; +} + +/** ['undo' => seconds, 'reply_to_read' => bool, 'parse' => bool, 'reads' => the mailbox ITFlow reads, lower case, or ''] - as saved. */ +function ticketReplySendSettings() { + global $mysqli; + static $settings = null; + if ($settings === null) { + $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT config_ticket_email_parse, config_imap_provider, config_imap_username" + . (ticketReplySendReady() ? ', config_ticket_reply_undo_seconds, config_ticket_reply_to_read' : '') . " FROM settings WHERE company_id = 1")) ?: []; + $reads = strtolower(trim((string) ($row['config_imap_username'] ?? ''))); + $settings = [ + 'undo' => intval($row['config_ticket_reply_undo_seconds'] ?? 0), + 'reply_to_read' => intval($row['config_ticket_reply_to_read'] ?? 0) === 1, + 'parse' => intval($row['config_ticket_email_parse'] ?? 0) === 1 && trim((string) ($row['config_imap_provider'] ?? '')) !== '', + 'reads' => filter_var($reads, FILTER_VALIDATE_EMAIL) ? $reads : '', + ]; + } + return $settings; +} + +/** How long an emailed or texted reply waits on the ticket page, with Undo - 0 when it goes at once. */ +function ticketReplyUndoSeconds() { + $seconds = ticketReplySendSettings()['undo']; + return isset(TICKET_REPLY_UNDO_CHOICES[$seconds]) ? $seconds : 0; +} + +/* + * The address a ticket's email from $from_email asks replies to go to - the mailbox ITFlow reads, when email to ticket + * is on, the setting is, and $from_email is another address - or '' for none, a reply going to From as always. + */ +function ticketMailReplyToRead($from_email) { + $settings = ticketReplySendSettings(); + if (!$settings['parse'] || !$settings['reply_to_read'] || $settings['reads'] === '') { + return ''; + } + return $settings['reads'] !== strtolower(trim((string) $from_email)) ? $settings['reads'] : ''; +}