diff --git a/CHANGELOG.md b/CHANGELOG.md index 16aca3724..70aeaa0e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This file documents all notable changes made to ITFlow. ### Upgrading to 26.12.4 -Update from Maintenance > Update as usual. This release has one database change, 4.3.8, which lets ITFlow remind clients before their autopay card expires: a setting for it, and on each saved card when it was last checked with Stripe, which reminders went out and the key in their link. +Update from Maintenance > Update as usual. This release has two database changes: 4.3.8, which lets ITFlow remind clients before their autopay card expires - a setting for it, and on each saved card when it was last checked with Stripe, which reminders went out and the key in their link - and 4.3.9, which adds the table that keeps email routing rules. ### New Features & Updates @@ -14,6 +14,7 @@ Update from Maintenance > Update as usual. This release has one database change, - Cards on autopay that are about to expire: a month before the month a card expires, and again in that month, the client's primary and billing contacts get an email with an Add your new card button that needs no sign-in. The new card takes the old one's place on every recurring invoice it paid, with who changed it and what they agreed to in each one's history. Your team gets a Card Expiring notification each time, and one when a client adds their new card. A card that already ran out in the last 60 days gets one email saying so. Turn the emails off with Email clients before their autopay card expires in Settings > Invoices > Paid online; a client left out of reminders isn't emailed either, and the team is still told. - Saved cards are checked with Stripe each night when they expire soon or have lately, so a card the bank replaced shows its new expiry and nobody is asked about it. Admin > Payment Providers > Saved Cards says when each card was checked, and when the team was told it is running out. - Autopay that doesn't go through - a declined or expired card, or a card that's gone - sends your team an Autopay Failed notification with Stripe's reason. Before, it was only in the invoice's history. +- Email routing rules, in Admin > Email Routing: pick an address or a domain - alerts@vendor.example, or vendor.example - and, if you like, words its subject has, and say where its new tickets go: a client and a contact, a category, a priority, an agent and a ticket template. A vendor's monitoring alerts about one client go to that client at High priority and land with the technician who looks after them, with the template's tasks already on the ticket. A rule decides the client over the sender's own contact or domain, and takes email even with tickets from unknown senders off. What it leaves as it is comes from its template. When more than one rule fits, the most exact wins - an address before a domain, a subdomain before its domain, and a rule with subject words before one without. Replies to a ticket still go on that ticket, Ignored Senders are still set aside first, and out-of-office replies are never routed. The sender is only answered and copied in when they are the ticket's contact, so a system's address isn't sent "we have your email" or every reply, and nobody at the client is emailed about an alert they didn't send. The agent a rule assigns is told, as when a ticket is assigned by hand. Each rule shows how many tickets it has routed and when the last was, and warns when it takes another client's contacts or domain, when Ignored Senders sets its email aside first, or when its client, contact, category, agent or template has been archived. Try an Email in Settings > Tickets shows which rule an email would meet and what it would do, and Email to ticket links to the page. ### Bug Fixes @@ -31,6 +32,7 @@ Update from Maintenance > Update as usual. This release has one database change, - Saved cards expiring (DB 4.3.8): new `functions/card_expiry.php`. `cardExpiryNightly($now = null)`, called from `cron/nightly_tasks.php` before autopay, reads back from Stripe (`cardRefresh()`, PaymentMethods retrieve) up to `CARD_EXPIRY_CHECK_LIMIT` (200) cards a night whose expiry is from 6 months ago to next month and that weren't checked in 20 hours, keeping Stripe's brand, last four and expiry in `saved_payment_description` and stamping `saved_payment_checked_at`. It then finds cards on autopay for active recurring invoices (`cardAutopayCards()`) and, for an expiry within 30 days of its month starting (stage 1) or in or past its month (stage 2), emails the billing recipients (`billingMailCardExpiring()`), notifies the team (`Card Expiring`) and logs each recurring invoice's history, recording `saved_payment_reminded_expiry`, `saved_payment_reminded` and a random `saved_payment_update_key` that both stages' links share. `settings.config_invoice_card_expiry_reminders` (default 1) and `client_reminders` gate the email only. `guest/guest_card_update.php?card=&key=` reuses `js/guest_autopay_stripe.js` (its form's `data-checkout-url` now picks the endpoint): `guest_ajax.php?guest_card_update_checkout` starts a setup-mode Checkout Session on the client's Stripe customer (`cardUpdateCheckoutStart()`), and `guest_post.php?guest_card_update_done` runs `cardUpdateFinish()`, which checks the session's mode, card and customer first, saves the new card, repoints every `recurring_payments` row of the client's that used the old card, clears the key, and sends `billingMailCardUpdated()`. `billingCardParts()` gains `expires` (yyyymm), `billingStripeChargeSaved()` reads an expired-looking card back from Stripe before refusing it, and `autopayFailedNotify()` in `functions/billing_pay.php` sends the `Autopay Failed` notification from the nightly autopay's failure branches. - Texts: `smsInboxBulk($action, $ids, $user_id)` in `functions/sms.php` runs an action from `SMS_INBOX_BULK` (dismiss, spam, clear, not_spam, delete => the status it takes texts from and the permission it needs) on the ticked inbound texts, or with `$ids` null on the whole tab, inside `clientScopeSql()`. Spam and not spam block or unblock the numbers and move every text from them, as the single actions do. Clearing a code sets it to dismissed with its body, code and media wiped, so the text log's search can't find its words. `agent/post/texts.php` takes `bulk_texts` (with `sms_message_ids[]`) and `bulk_texts_all`, plus `sms_clear_code` for one code. `agent/texts.php` uses the standard `js/bulk_actions.js` select-all and Bulk Action button. - Forwarded client emails: new `functions/ticket_mail_forward.php`. In `cron/ticket_email_parser.php`, after the thread and ticket-number steps and before `ticketMailSenderContact()`, an email from an active agent (`ticketMailForwardAgent()`) that isn't forged, automatic or answered at its Reply-To goes to `ticketMailForwarded($html, $text, $subject, $attachments)`. It looks for the first header block - a From line with an address and a Sent, Date, To or Subject line beside it, with English and common European labels - below a forward line (Gmail's, Thunderbird's, Apple Mail's), or anywhere when the subject is a forward (`TICKET_MAIL_FORWARD_ONLY`); Outlook's `-----Original Message-----` and underscore line only count with a forward subject, as its replies have them too. The HTML is split at `gmail_quote`, `moz-forward-container`, `divRplyFwdMsg`/`appendonsend`, Outlook's bordered header div, `blockquote type="cite"` or the forward line, as long as the part from there holds the sender, else the text part is split; a single `message/rfc822` attachment is parsed with MailMimeParser, its cid pictures inlined. When the forwarded sender isn't one of your addresses, your domain or an agent (`ticketMailForwardFromClient()`), the parser takes its sender, subject (`ticketMailForwardSubject()`) and body and sets `$email['forward'] = ['agent', 'note']`. A forward is never a similar-subject reply (it is linked as related, `linkSimilarTicket(..., true)`), a contact it creates says who forwarded it, and step 6 runs for it whatever `config_ticket_email_parse_unknown_senders` says. `addTicket()` adds "forwarded by" to the from line and audit entry, calls `ticketMailForwardNote()` (an Internal reply by the agent, when there is a note, and a history line), and doesn't CC or answer a forwarded sender with no contact. Parser attachments now carry `mime`. `ticketMailTry()` adds a "From one of your agents?" step. +- Email routing rules (DB 4.3.9, `email_routing_rules`): new `functions/mail_route.php`. A rule is `routing_rule_match` (an address or a domain, cleaned by `mailIgnoreClean()` and matched by `mailIgnoreMatches()`, so a domain takes its subdomains), optional `routing_rule_subject` (case-insensitive, whitespace-collapsed substring), and what it sets: `routing_rule_client_id`, `routing_rule_contact_id`, `routing_rule_category_id`, `routing_rule_priority`, `routing_rule_assigned_to`, `routing_rule_ticket_template_id` - 0 or '' for none - with a note, a hit count and `routing_rule_matched_at`. `mailRouteFind()` takes the most exact match (`mailRouteExactness()`: address, then more domain labels, then longer subject words, then the older rule); `mailRouteFor()` adds `mailRouteTargets()`, which re-checks each target (client and contact not archived and the contact the client's, category, active agent, template not archived) and lists what's `gone`. In `cron/ticket_email_parser.php` the rule is found after the thread, ticket-number and forward steps and never for an out-of-office; with a client it replaces `$picked`/`$domain_row`, its contact is the rule's, else the sender as that client's contact, and a client with no contact gets its own `addTicket()` call before the domain step. `$email['route']` carries the rule, with `quiet` when the sender isn't the ticket's contact (or a website form's visitor, or a new contact at a client's domain), and `$email['sender']` when the ticket's contact isn't the sender. `addTicket()` now sets priority, category, agent and billable from `mailRouteTicketFields()` (the rule's, else the template's), Open when assigned, logs the rule in the ticket history, runs `ticketTemplateFinish(..., true, true)` for the template, notifies and emails the assignee (`ticketMailAssigned()`), and when `quiet` skips the sender CC, the created email and `ticketMailColleaguesNew()`; the from line, audit, forged note and copied-CC exclusion use the sender. A quiet email is never a similar-subject reply (`linkSimilarTicket($new, $similar, 'route')`, whose third argument is now the reason - 'forward' or 'route'), step 6 runs for a routed email whatever `config_ticket_email_parse_unknown_senders` says, and the hit is counted only when a ticket was made. Admin page `admin/email_routing_rules.php` (standard list, search across sender, words, client, contact, category, agent, template and note), modal `admin/modals/email_routing_rule/email_routing_rule.php` (its contact list reloads from `agent/ajax.php?get_client_contacts`), handlers `admin/post/email_routing_rule.php` (`add_routing_rule`, `edit_routing_rule`, `delete_routing_rule`; `mailRouteReach()` for the other-clients warning). `ticketMailTry()` adds an "An email routing rule?" step and `ticketMailTryRoute()` for the outcome. ## [26.12.3] diff --git a/admin/database_updates/4.3.9.php b/admin/database_updates/4.3.9.php new file mode 100644 index 000000000..b3078a116 --- /dev/null +++ b/admin/database_updates/4.3.9.php @@ -0,0 +1,36 @@ + Email Routing): a client and a contact, a category, a priority, an + * agent and a ticket template - 0 or '' for each it leaves as it is - with why, who added it, and how many emails + * it routed and when the last was + * + * Re-runnable: the table is made only where it isn't there. + */ + +defined('FROM_DB_UPDATER') || die("Direct file access is not allowed"); + + mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `email_routing_rules` ( + `routing_rule_id` int(11) NOT NULL AUTO_INCREMENT, + `routing_rule_match` varchar(255) NOT NULL, + `routing_rule_subject` varchar(255) NOT NULL DEFAULT '', + `routing_rule_client_id` int(11) NOT NULL DEFAULT 0, + `routing_rule_contact_id` int(11) NOT NULL DEFAULT 0, + `routing_rule_category_id` int(11) NOT NULL DEFAULT 0, + `routing_rule_priority` varchar(20) NOT NULL DEFAULT '', + `routing_rule_assigned_to` int(11) NOT NULL DEFAULT 0, + `routing_rule_ticket_template_id` int(11) NOT NULL DEFAULT 0, + `routing_rule_note` varchar(255) NOT NULL DEFAULT '', + `routing_rule_matches` int(11) NOT NULL DEFAULT 0, + `routing_rule_matched_at` datetime DEFAULT NULL, + `routing_rule_created_by` int(11) NOT NULL DEFAULT 0, + `routing_rule_created_at` datetime NOT NULL DEFAULT current_timestamp(), + `routing_rule_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(), + PRIMARY KEY (`routing_rule_id`), + KEY `routing_rule_match` (`routing_rule_match`), + KEY `routing_rule_client_id` (`routing_rule_client_id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci"); diff --git a/admin/email_routing_rules.php b/admin/email_routing_rules.php new file mode 100644 index 000000000..40f487608 --- /dev/null +++ b/admin/email_routing_rules.php @@ -0,0 +1,203 @@ + Email Routing - email routing rules (functions/mail_route.php): an address or a domain, with words its subject + * has, whose new tickets by email go to a client and a contact, with a category, a priority, an agent and a ticket + * template. Each with why, who added it, and how many emails it routed and the last. New and edit in + * modals/email_routing_rule/email_routing_rule.php; the handlers are admin/post/email_routing_rule.php. + */ + +// Default Column Sortby Filter +$sort = "routing_rule_match"; +$order = "ASC"; + +require_once "includes/inc_all_admin.php"; + +if (!mailRouteReady()) { ?> +
Email routing needs the database update - Maintenance > Update.
+ "routing_rule_match $order, routing_rule_subject ASC", + 'client_name' => "client_name IS NULL, client_name $order, routing_rule_match ASC", + 'routing_rule_matches' => "routing_rule_matches $order, routing_rule_match ASC", + 'routing_rule_matched_at' => "routing_rule_matched_at IS NULL, routing_rule_matched_at $order, routing_rule_match ASC", + 'routing_rule_created_at' => "routing_rule_created_at $order, routing_rule_match ASC", +]; +if (!isset($sortable_columns[$sort])) { + $sort = 'routing_rule_match'; +} + +$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS email_routing_rules.*, client_name, client_archived_at, contact_name, contact_archived_at, contact_client_id, + category_name, category_archived_at, agent.user_name AS agent_name, agent.user_status AS agent_status, agent.user_archived_at AS agent_archived_at, + ticket_template_name, ticket_template_archived_at, creator.user_name AS creator_name + FROM email_routing_rules + LEFT JOIN clients ON client_id = routing_rule_client_id + LEFT JOIN contacts ON contact_id = routing_rule_contact_id + LEFT JOIN categories ON category_id = routing_rule_category_id + LEFT JOIN users agent ON agent.user_id = routing_rule_assigned_to + LEFT JOIN ticket_templates ON ticket_template_id = routing_rule_ticket_template_id + LEFT JOIN users creator ON creator.user_id = routing_rule_created_by + WHERE (routing_rule_match LIKE '%$q%' OR routing_rule_subject LIKE '%$q%' OR routing_rule_note LIKE '%$q%' OR client_name LIKE '%$q%' OR contact_name LIKE '%$q%' + OR category_name LIKE '%$q%' OR agent.user_name LIKE '%$q%' OR ticket_template_name LIKE '%$q%') + ORDER BY {$sortable_columns[$sort]} + LIMIT $record_from, $record_to"); +$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); + +?> + +
+
+

Email Routing

+
+ +
+
+ +
+

+ A new ticket by email from these addresses and domains goes where the rule says - to its client, over the sender's own contact or domain, with its + category, priority, agent and template. Replies to a ticket still go on it, and Ignored Senders are set aside first. + The sender is answered and copied in only when they are the ticket's contact. When more than one rule takes an email, the most exact routes it: an address + before a domain, and a rule with subject words before one without. Try an Email shows what one would do. +

+
+
+
+
+ + +
+
+
+
+
+ +
+
+ + "> + + + + + + + + + + + + ' . escapeHtml($row['category_name']) . ''; + } else { + $gone[] = 'Category archived or deleted'; + } + } + if ((string) $row['routing_rule_priority'] !== '') { + $sets[] = '' . escapeHtml($row['routing_rule_priority']) . ''; + } + if (intval($row['routing_rule_assigned_to'])) { + if ($row['agent_name'] !== null && intval($row['agent_status']) === 1 && $row['agent_archived_at'] === null) { + $sets[] = '' . escapeHtml($row['agent_name']) . ''; + } else { + $gone[] = 'Agent disabled or archived'; + } + } + if (intval($row['routing_rule_ticket_template_id'])) { + if ($row['ticket_template_name'] !== null && $row['ticket_template_archived_at'] === null) { + $sets[] = '' . escapeHtml($row['ticket_template_name']) . ''; + } else { + $gone[] = 'Template archived or deleted'; + } + } + ?> + + + + + + + + + + + +
Sets
+ > +
+ , + with “” in the subject +
+ +
+ +
+ + +
+ + Whoever it's from + +
+
+
-' ?>
+ +
- skipped until it's fixed
+ +
ticket + +
+
+ +
+
+
+ + 'routing rules', + 'icon' => 'fa-route', + 'text' => 'Send the new tickets from an address or a domain to a client, with a category, priority, agent or template - a vendor\'s alerts about one client, a monitoring system, a form.', + 'actions' => [['label' => 'New Routing Rule', 'modal' => 'modals/email_routing_rule/email_routing_rule.php', 'size' => 'lg']], + ]; + require_once "../includes/filter_footer.php"; + ?> +
+ + + + +