mirror of
https://github.com/itflow-org/itflow
synced 2026-12-04 10:09:01 +00:00
One decision for where an email goes: ticketMailDecide() in functions/ticket_mail_decide.php runs the thread, ticket number, forwarding agent, routing rule, contact and domain, out-of-office, similar subject, contact, routed client, domain and unknown sender steps once and returns the decision with its steps, the email parser carries it out and Try an Email shows it so the two can no longer disagree, a CC whose address has an apostrophe is found again on a same-subject reply, an out-of-office is checked after the ticket number in Try an Email as the mailbox does, and an out-of-office that isn't set aside is left in the inbox; 26.12.4 changelog
This commit is contained in:
@@ -1,39 +1,20 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Try an email (Admin > Settings > Tickets, Email to ticket): what the
|
||||
* Ticket Email Parser (cron/ticket_email_parser.php) would do with an email from
|
||||
* this address with this subject - without doing any of it. It asks the same
|
||||
* questions in the same order, against the same records:
|
||||
* ITFlow - Try an email (Admin > Settings > Tickets, Email to ticket): what the Ticket Email Parser
|
||||
* (cron/ticket_email_parser.php) would do with an email from this address with this subject - without doing any of it.
|
||||
* What happens to an email that gets past the first checks is decided by ticketMailDecide() (functions/ticket_mail_decide.php)
|
||||
* for both - the same steps, in the same order, against the same records - so this can't say one thing while the parser
|
||||
* does another. Asked here first, as the parser does before it, by what an email typed in can show:
|
||||
*
|
||||
* 0. ITFlow's own email come back (emailOwnAddresses()) is skipped - anything else from one of those addresses is left
|
||||
* - email to ticket off - nothing is read;
|
||||
* - ITFlow's own email come back (emailOwnAddresses()) is skipped - anything else from one of those addresses is left
|
||||
* in the inbox when unknown senders aren't let in;
|
||||
* - 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
|
||||
* typed in here has no such headers, so this says so and goes on;
|
||||
* 2. a ticket number in [brackets] in the subject, with the prefix that
|
||||
* ticket was given (ticketMailSubjectTicket()) - a reply on that ticket,
|
||||
* as whoever sent it is to the ticket (its contact or another of the
|
||||
* client's, an agent, a CC, or a stranger), or a closed one turned away;
|
||||
* - a client's email an agent forwarded is read as from the client it forwards (ticketMailForwarded()) - only
|
||||
* said here, where there is no email to read;
|
||||
* - an email routing rule (mailRouteFor(), functions/mail_route.php) sets the ticket up, and the client it is for
|
||||
* over the sender's contact or domain;
|
||||
* 3. a subject almost the same as an open ticket of the sender's client from
|
||||
* the last 7 days - a reply on that ticket from its contact or a CC, and
|
||||
* from anyone else at the client a new ticket linked to it as related;
|
||||
* 4. a contact's address - a new ticket for their client: at more than one client, the one whose
|
||||
* domain it is, else where they last raised a ticket (ticketMailSenderContact());
|
||||
* 5. an address at a client's domain - a new contact, and a ticket;
|
||||
* 6. anyone else - a ticket with no client when unknown senders are let in,
|
||||
* else left in the inbox, flagged.
|
||||
* - a mail server's bounce (ticketMailIsBounce() - here, by its address alone) is read as one, whatever the settings;
|
||||
* - an ignored sender (mailIgnoreFind(), functions/mail_ignore.php) is set aside.
|
||||
*
|
||||
* Kept in step with the parser by hand - change one, change the other. A Reply-To (ticketMailReplyTo())
|
||||
* can't be typed in here.
|
||||
* An email typed in has no headers - it answers no email (In-Reply-To, References), has no Reply-To and no sender checks -
|
||||
* and no text, so a forward in it can't be read: each is said where it would count.
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ -43,12 +24,10 @@
|
||||
* ticketMailAutomaticKind() reads).
|
||||
*/
|
||||
function ticketMailTry($from_email, $subject, $auto_reply = false) {
|
||||
global $mysqli, $config_ticket_email_parse, $config_ticket_email_parse_unknown_senders, $config_ticket_prefix;
|
||||
global $config_ticket_email_parse, $config_ticket_email_parse_unknown_senders;
|
||||
|
||||
$from_email = strtolower(trim((string) $from_email));
|
||||
$subject = mb_substr(trim((string) $subject) !== '' ? trim((string) $subject) : 'No Subject', 0, 500);
|
||||
$from_sql = escapeSql($from_email);
|
||||
$domain = substr(strrchr($from_email, '@') ?: '', 1);
|
||||
$steps = [];
|
||||
$end = function ($icon, $tone, $title, $text = '') use (&$steps) {
|
||||
return ['steps' => $steps, 'outcome' => [$icon, $tone, $title, $text]];
|
||||
@@ -85,139 +64,64 @@ function ticketMailTry($from_email, $subject, $auto_reply = false) {
|
||||
$steps[] = ['An ignored sender?', 'No'];
|
||||
}
|
||||
|
||||
if ($auto_reply) {
|
||||
$steps[] = ['An automatic reply?', 'Yes'];
|
||||
$answered = ticketMailSubjectTicket($subject);
|
||||
if ($answered) {
|
||||
return $end('fa-robot', 'secondary', 'An internal note on ' . $answered['reference'],
|
||||
'Out-of-office and other automatic replies go on the ticket they answer as a note, to read - it isn\'t reopened, nothing is passed on, and nobody is answered, so two systems can\'t answer each other forever.');
|
||||
// Where it goes from here - decided as the parser decides it
|
||||
$decision = ticketMailDecide(['from' => $from_email, 'from_name' => '', 'subject' => $subject, 'thread_ticket_id' => null,
|
||||
'automatic' => $auto_reply ? 'auto-reply' : '', 'forged' => '', 'answers_ours' => null]);
|
||||
return ['steps' => array_merge($steps, $decision['steps']), 'outcome' => ticketMailTryOutcome($decision, $from_email, $auto_reply)];
|
||||
}
|
||||
|
||||
/*
|
||||
* Where an email ends up, from ticketMailDecide() - [icon, tone, title, text] - for an email from $from_email.
|
||||
* $auto_reply: it says it is an out-of-office.
|
||||
*/
|
||||
function ticketMailTryOutcome(array $decision, $from_email, $auto_reply = false) {
|
||||
global $config_ticket_email_parse_unknown_senders;
|
||||
$route = $decision['route'];
|
||||
$route_text = $route ? ticketMailTryRoute($route, $decision['quiet']) : '';
|
||||
$linked = $decision['similar'] ? ' It is linked to ' . ticketReference($decision['similar']) . ' as related, as its subject is almost the same, in case it is the same thing.' : '';
|
||||
|
||||
if ($decision['action'] === 'reply') {
|
||||
if ($auto_reply) {
|
||||
return ['fa-robot', 'secondary', 'An internal note on ' . ticketReference(ticketRow($decision['ticket_id']) ?: ['ticket_prefix' => '', 'ticket_number' => $decision['ticket_id']]),
|
||||
'Out-of-office and other automatic replies go on the ticket they answer as a note, to read - it isn\'t reopened, nothing is passed on, and nobody is answered, so two systems can\'t answer each other forever.'];
|
||||
}
|
||||
// A stranger's, with unknown senders not let in: ITFlow's only when it answers an email ITFlow sent - by its headers
|
||||
$auto_known = $config_ticket_email_parse_unknown_senders || ticketMailSenderContact($from_email, $domain)['contact']
|
||||
|| ($domain !== '' && mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM domains WHERE domain_name = '" . escapeSql($domain) . "' AND domain_archived_at IS NULL LIMIT 1")));
|
||||
if (!$auto_known) {
|
||||
return $end('fa-inbox', 'warning', 'Left in the inbox - unless it answers an email ITFlow sent',
|
||||
'It answers no ticket, and is from someone who isn\'t a contact, at a domain no client has. One answering an email ITFlow sent - an invoice, say - is moved to the ITFlow folder; an out-of-office to an email someone sent from the mailbox by hand stays in the inbox, flagged.');
|
||||
return ticketMailTryReply($from_email, $decision['ticket_id']) ?? ['fa-question-circle', 'warning', 'A reply on a ticket that is gone', 'The ticket isn\'t there any more.'];
|
||||
}
|
||||
if ($decision['action'] === 'set_aside') {
|
||||
return ['fa-forward', 'secondary', 'Set aside', 'It answers no ticket, so it is moved to the ITFlow folder without one, and isn\'t answered.'];
|
||||
}
|
||||
if ($decision['action'] === 'leave') {
|
||||
if ($decision['answers_unknown']) {
|
||||
return ['fa-inbox', 'warning', 'Left in the inbox - unless it answers an email ITFlow sent',
|
||||
'It answers no ticket, and is from someone who isn\'t a contact, at a domain no client has. One answering an email ITFlow sent - an invoice, say - is moved to the ITFlow folder; an out-of-office to an email someone sent from the mailbox by hand stays in the inbox, flagged.'];
|
||||
}
|
||||
return $end('fa-forward', 'secondary', 'Set aside',
|
||||
'It answers no ticket, so it is moved to the ITFlow folder without one, and isn\'t answered.');
|
||||
}
|
||||
$steps[] = ['An automatic reply?', 'No'];
|
||||
|
||||
// A reply to one of a ticket's emails - by its headers, which an email typed in here hasn't got
|
||||
$steps[] = ['A reply to one of a ticket\'s emails?', 'Not known here - an email answering one is matched by its headers first, whatever its subject says'];
|
||||
|
||||
// 2. A ticket number in the subject, with the prefix that ticket was given
|
||||
$numbered = ticketMailSubjectTicket($subject);
|
||||
if ($numbered) {
|
||||
$steps[] = ['A ticket number in the subject?', 'Yes - ' . $numbered['reference']];
|
||||
return ['steps' => $steps, 'outcome' => ticketMailTryReply($from_email, $numbered['ticket_id'])];
|
||||
}
|
||||
$steps[] = ['A ticket number in the subject?', preg_match('/\[[^\[\]\s]{0,40}\d\]/', $subject)
|
||||
? 'No - there is no ticket with the number in brackets, so it carries on'
|
||||
: 'No - it would be [' . $config_ticket_prefix . '123] somewhere in it'];
|
||||
|
||||
// An agent's: a client's email they forward is read as from the client - said, as there is no email here to read
|
||||
if ($forward_agent = ticketMailForwardAgent($from_email)) {
|
||||
$steps[] = ['From one of your agents?', 'Yes - ' . $forward_agent['user_name'] . '. A client\'s email they forward to the mailbox is read as from the client: its sender, subject and text go the steps below,'
|
||||
. ' and what ' . $forward_agent['user_name'] . ' wrote above it is their internal note on the ticket. One from someone who isn\'t a client\'s is a ticket with no client even when tickets from unknown senders are off, and they aren\'t answered or copied in. Anything else from them goes on as from them'];
|
||||
return ['fa-inbox', 'warning', 'Left in the inbox',
|
||||
'ITFlow leaves it in the mailbox\'s inbox, flagged, and doesn\'t look at it again. Make the sender a contact, add their domain to a client, or turn on From unknown senders too, to have email like it taken - and clear the flag on one already there to have it taken as well.'];
|
||||
}
|
||||
|
||||
// An email routing rule (Admin > Email Routing)
|
||||
$route = mailRouteFor($from_email, $subject);
|
||||
if (mailRouteReady()) {
|
||||
$steps[] = ['An email routing rule?', $route ? 'Yes - ' . $route['name'] . ': ' . mailRouteSummary($route) . ($route['gone'] ? ' - but ' . implode(', ', $route['gone']) : '') : 'No'];
|
||||
}
|
||||
|
||||
// The sender: a contact - at the client whose domain it is, when they are one at more than one - or the client whose domain it is
|
||||
$picked = ticketMailSenderContact($from_email, $domain);
|
||||
$contact = $picked['contact'];
|
||||
$domain_row = $domain !== '' ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT domain_client_id, domain_name, client_name FROM domains
|
||||
LEFT JOIN clients ON client_id = domain_client_id WHERE domain_name = '" . escapeSql($domain) . "' AND domain_archived_at IS NULL LIMIT 1")) : null;
|
||||
if ($domain_row && $domain_row['domain_name'] !== $domain) {
|
||||
$domain_row = null;
|
||||
}
|
||||
|
||||
// Routed to a client - the rule's contact, else the sender as one of that client's contacts, else none
|
||||
$route_client = $route && $route['client'] ? $route['client'] : null;
|
||||
if ($route_client) {
|
||||
$picked = ['contact' => null, 'others' => [], 'why' => ''];
|
||||
$domain_row = null;
|
||||
$contact = $route['contact'] ? $route['contact'] + ['client_name' => $route_client['client_name']] : (mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_id, contact_name, contact_email, contact_client_id, client_name
|
||||
FROM contacts LEFT JOIN clients ON client_id = contact_client_id WHERE contact_client_id = " . intval($route_client['client_id']) . " AND contact_email = '$from_sql' AND contact_archived_at IS NULL LIMIT 1")) ?: null);
|
||||
}
|
||||
// Its sender answered only as the ticket's contact
|
||||
$route_quiet = $route && !(($contact && strtolower((string) $contact['contact_email']) === $from_email) || $domain_row);
|
||||
$route_text = $route ? ticketMailTryRoute($route, $route_quiet) : '';
|
||||
|
||||
// 3. Almost the same subject as an open ticket of theirs from the last week - a reply from its contact or a CC; from
|
||||
// anyone else at the client, a ticket of its own linked to it
|
||||
$client_id = $route_client ? intval($route_client['client_id']) : ($contact ? intval($contact['contact_client_id']) : ($domain_row ? intval($domain_row['domain_client_id']) : 0));
|
||||
$similar = null;
|
||||
if (strlen(escapeSql($subject)) > 10 && $client_id) {
|
||||
$sql = mysqli_query($mysqli, "SELECT ticket_id, ticket_prefix, ticket_number, ticket_subject, ticket_contact_id FROM tickets WHERE ticket_client_id = $client_id AND ticket_resolved_at IS NULL
|
||||
AND ticket_created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
similar_text(strtolower(escapeSql($subject)), strtolower($row['ticket_subject']), $percent);
|
||||
if ($percent >= 95) {
|
||||
$similar = $row;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$linked = '';
|
||||
if ($similar) {
|
||||
$theirs = !$route_quiet && (($contact && intval($contact['contact_id']) === intval($similar['ticket_contact_id']))
|
||||
|| mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM ticket_ccs WHERE ticket_cc_ticket_id = " . intval($similar['ticket_id']) . " AND ticket_cc_email = '$from_sql' LIMIT 1")));
|
||||
if ($theirs && ($reply = ticketMailTryReply($from_email, intval($similar['ticket_id'])))) {
|
||||
$steps[] = ['The same subject as an open ticket of theirs from the last week?', 'Yes - ' . ticketReference($similar)];
|
||||
return ['steps' => $steps, 'outcome' => $reply];
|
||||
}
|
||||
$steps[] = ['The same subject as an open ticket of theirs from the last week?', 'Yes - ' . ticketReference($similar) . ', but someone else\'s, so it carries on'];
|
||||
$linked = ' It is linked to ' . ticketReference($similar) . ' as related, as its subject is almost the same, in case it is the same thing.';
|
||||
} else {
|
||||
$steps[] = ['The same subject as an open ticket of theirs from the last week?', $client_id ? 'No' : 'No - they aren\'t a client\'s'];
|
||||
}
|
||||
|
||||
// Routed to a client: its ticket, whoever the sender is
|
||||
if ($route_client) {
|
||||
// A ticket - routed to a client, a contact's, a new contact's, or nobody's
|
||||
$contact = $decision['contact'];
|
||||
if ($route && $route['client']) {
|
||||
$contact_text = $contact ? (strtolower((string) $contact['contact_email']) === $from_email ? 'From ' . $contact['contact_name'] . ', its contact. ' : 'From ' . $from_email . ', with ' . $contact['contact_name'] . ' as its contact. ')
|
||||
: 'From ' . $from_email . ', with no contact - they aren\'t one of ' . $route_client['client_name'] . '\'s. ';
|
||||
return ['steps' => $steps, 'outcome' => ['fa-route', 'success', 'A new ticket for ' . $route_client['client_name'],
|
||||
$contact_text . 'The subject is its subject and the email its details. ' . $route_text . ($route_quiet ? '' : ' ' . ticketMailTryNoReply($from_email)) . $linked]];
|
||||
: 'From ' . $from_email . ', with no contact - they aren\'t one of ' . $decision['client_name'] . '\'s. ';
|
||||
return ['fa-route', 'success', 'A new ticket for ' . $decision['client_name'],
|
||||
$contact_text . 'The subject is its subject and the email its details. ' . $route_text . ($decision['quiet'] ? '' : ' ' . ticketMailTryNoReply($from_email)) . $linked];
|
||||
}
|
||||
|
||||
// 4. A contact
|
||||
if ($contact) {
|
||||
$others = $picked['others'] ? ' - and at ' . implode(', ', $picked['others']) . ' too, so it goes to ' . ($contact['client_name'] ?? 'no client') . ', ' . $picked['why'] : '';
|
||||
$steps[] = ['A contact\'s address?', 'Yes - ' . $contact['contact_name'] . ($contact['client_name'] !== null ? ', ' . $contact['client_name'] : '') . $others];
|
||||
return ['steps' => $steps, 'outcome' => ['fa-plus-circle', 'success', 'A new ticket for ' . ($contact['client_name'] ?? 'no client'),
|
||||
'From ' . $contact['contact_name'] . ', with the subject as its subject and the email as its details. ' . ($picked['others'] ? 'A note on it names the other clients they are a contact at. ' : '')
|
||||
. ($route_text !== '' ? $route_text . ' ' : '') . ticketMailTryNoReply($from_email) . $linked]];
|
||||
return ['fa-plus-circle', 'success', 'A new ticket for ' . ($decision['client_name'] ?? 'no client'),
|
||||
'From ' . $contact['contact_name'] . ', with the subject as its subject and the email as its details. ' . (!empty($decision['picked']['others']) ? 'A note on it names the other clients they are a contact at. ' : '')
|
||||
. ($route_text !== '' ? $route_text . ' ' : '') . ticketMailTryNoReply($from_email) . $linked];
|
||||
}
|
||||
$steps[] = ['A contact\'s address?', 'No'];
|
||||
|
||||
// 5. A client's domain
|
||||
if ($domain_row) {
|
||||
$steps[] = ['At a client\'s domain?', 'Yes - ' . $domain . ', ' . $domain_row['client_name']];
|
||||
return ['steps' => $steps, 'outcome' => ['fa-plus-circle', 'success', 'A new contact and ticket for ' . $domain_row['client_name'],
|
||||
'The sender is added as a contact of ' . $domain_row['client_name'] . ' with the name in their email, and the ticket is theirs. ' . ($route_text !== '' ? $route_text . ' ' : '') . ticketMailTryNoReply($from_email) . $linked]];
|
||||
if ($decision['new_contact']) {
|
||||
return ['fa-plus-circle', 'success', 'A new contact and ticket for ' . $decision['client_name'],
|
||||
'The sender is added as a contact of ' . $decision['client_name'] . ' with the name in their email, and the ticket is theirs. ' . ($route_text !== '' ? $route_text . ' ' : '') . ticketMailTryNoReply($from_email) . $linked];
|
||||
}
|
||||
$steps[] = ['At a client\'s domain?', $domain !== '' ? 'No - no client has ' . $domain : 'No'];
|
||||
|
||||
// 6. Anyone else - a routing rule takes theirs whatever the setting
|
||||
if ($route) {
|
||||
return ['steps' => $steps, 'outcome' => ['fa-route', 'success', 'A new ticket with no client',
|
||||
'The routing rule takes it even with tickets from unknown senders ' . ($config_ticket_email_parse_unknown_senders ? 'on' : 'off') . ' - open for an agent to give it a client. ' . $route_text]];
|
||||
if ($decision['taken'] === 'route') {
|
||||
return ['fa-route', 'success', 'A new ticket with no client',
|
||||
'The routing rule takes it even with tickets from unknown senders ' . ($config_ticket_email_parse_unknown_senders ? 'on' : 'off') . ' - open for an agent to give it a client. ' . $route_text];
|
||||
}
|
||||
if (!$config_ticket_email_parse_unknown_senders) {
|
||||
$steps[] = ['Tickets from unknown senders?', 'Off'];
|
||||
return ['steps' => $steps, 'outcome' => ['fa-inbox', 'warning', 'Left in the inbox',
|
||||
'ITFlow leaves it in the mailbox\'s inbox, flagged, and doesn\'t look at it again. Make the sender a contact, add their domain to a client, or turn on From unknown senders too, to have email like it taken - and clear the flag on one already there to have it taken as well.']];
|
||||
}
|
||||
$steps[] = ['Tickets from unknown senders?', 'On'];
|
||||
return ['steps' => $steps, 'outcome' => ['fa-plus-circle', 'success', 'A new ticket with no client',
|
||||
'Open for an agent to give it a client. The sender is added as a CC, so replies reach them. ' . ticketMailTryNoReply($from_email, true)]];
|
||||
return ['fa-plus-circle', 'success', 'A new ticket with no client',
|
||||
'Open for an agent to give it a client. The sender is added as a CC, so replies reach them. ' . ticketMailTryNoReply($from_email, true)];
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user