mirror of
https://github.com/itflow-org/itflow
synced 2026-12-03 09:39:02 +00:00
221 lines
16 KiB
PHP
221 lines
16 KiB
PHP
<?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.
|
|
* 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:
|
|
*
|
|
* - 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;
|
|
* - an ignored sender (mailIgnoreFind(), functions/mail_ignore.php) is set aside.
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
/*
|
|
* The steps it went through, each [question, answer], and where it ended up:
|
|
* ['steps', 'outcome' => [icon, tone, title, text]]. $auto_reply is the email
|
|
* saying it is an automatic reply (Auto-Submitted: auto-replied, and the others
|
|
* ticketMailAutomaticKind() reads).
|
|
*/
|
|
function ticketMailTry($from_email, $subject, $auto_reply = false) {
|
|
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);
|
|
$steps = [];
|
|
$end = function ($icon, $tone, $title, $text = '') use (&$steps) {
|
|
return ['steps' => $steps, 'outcome' => [$icon, $tone, $title, $text]];
|
|
};
|
|
|
|
if (!$config_ticket_email_parse) {
|
|
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', 'Never a ticket', 'ITFlow\'s own email come back - a copy to this mailbox, or a notice sent to it - is moved to the ITFlow folder: taken, it would reopen the ticket, or raise one that sends another. '
|
|
. ($config_ticket_email_parse_unknown_senders ? 'So is anything else from the address.' : 'Anything else from the address - a scanner emailing the mailbox as itself, a note to self - is left in the inbox, flagged.'));
|
|
}
|
|
$steps[] = ['From one of ITFlow\'s own addresses?', 'No'];
|
|
|
|
// A mail server's bounce - here by its address, as an email typed in has no headers or report in it
|
|
if (ticketMailBounceSender($from_email)) {
|
|
$steps[] = ['A mail server saying an email couldn\'t be delivered?', 'Yes - from ' . $from_email];
|
|
return $end('fa-envelope-open-text', 'info', 'Read as a bounce',
|
|
'Never a ticket, and never answered - whatever the settings. The contacts with the address it failed for are marked Email bounced, the ticket the email was about gets a note, and the agent it is assigned to gets a notice - the admins, when nobody has it. A notice that an email is only delayed is set aside.'
|
|
. ($config_ticket_email_parse_unknown_senders ? '' : ' One that isn\'t about a ticket, an email ITFlow sent or a contact - for an email someone sent from the mailbox by hand - is left in the inbox, flagged.'));
|
|
}
|
|
$steps[] = ['A mail server saying an email couldn\'t be delivered?', 'No'];
|
|
|
|
// An ignored sender (Admin > Ignored Senders, functions/mail_ignore.php)
|
|
if (mailIgnoreReady()) {
|
|
$ignored = mailIgnoreFind($from_email);
|
|
if ($ignored) {
|
|
$steps[] = ['An ignored sender?', 'Yes - ' . $ignored['ignored_sender_match']];
|
|
return $end('fa-eye-slash', 'secondary', 'Set aside - an ignored sender',
|
|
'No ticket, no reply on one and no answer - it is moved to the ITFlow folder. Admin > Ignored Senders has the rule.');
|
|
}
|
|
$steps[] = ['An ignored sender?', 'No'];
|
|
}
|
|
|
|
// 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.'];
|
|
}
|
|
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 ['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.'];
|
|
}
|
|
|
|
// 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 ' . $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];
|
|
}
|
|
if ($contact) {
|
|
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];
|
|
}
|
|
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];
|
|
}
|
|
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];
|
|
}
|
|
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)];
|
|
}
|
|
|
|
/*
|
|
* What a routing rule does to the ticket, as sentences - how it is set up (its own, else its template's), and with $quiet
|
|
* that nobody at the client is emailed. Raw text.
|
|
*/
|
|
function ticketMailTryRoute(array $route, $quiet) {
|
|
global $config_ticket_default_billable;
|
|
[$priority, $category_id, $assigned_to] = mailRouteTicketFields($route, $config_ticket_default_billable);
|
|
$agent = $assigned_to ? (string) (getFieldById('users', $assigned_to, 'user_name') ?? '') : '';
|
|
$category = $category_id ? (string) (getFieldById('categories', $category_id, 'category_name') ?? '') : '';
|
|
$text = 'The routing rule makes it ' . $priority . ' priority' . ($category !== '' ? ", in $category" : '') . ($agent !== '' ? ", and assigns it to $agent, who is told - so it starts Open" : '') . '.';
|
|
if ($route['template']) {
|
|
$text .= ' Template ' . $route['template']['ticket_template_name'] . ' adds its tasks, tags and due time.';
|
|
}
|
|
if ($quiet) {
|
|
$text .= ' The sender isn\'t its contact, so nobody at the client is emailed that it is a ticket, and the sender isn\'t copied in on its replies.';
|
|
}
|
|
return $text;
|
|
}
|
|
|
|
/*
|
|
* What a reply by email to ticket $ticket_id from $from_email would do (addReply()
|
|
* in the parser) - [icon, tone, title, text] - or null when there is no such ticket.
|
|
*/
|
|
function ticketMailTryReply($from_email, $ticket_id) {
|
|
global $mysqli;
|
|
$from_sql = escapeSql($from_email);
|
|
$ticket = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT ticket_id, ticket_prefix, ticket_number, ticket_subject, ticket_status, ticket_client_id, contact_email, contact_name, client_name
|
|
FROM tickets LEFT JOIN contacts ON contact_id = ticket_contact_id LEFT JOIN clients ON client_id = ticket_client_id
|
|
WHERE ticket_id = " . intval($ticket_id) . " LIMIT 1"));
|
|
if (!$ticket) {
|
|
return null;
|
|
}
|
|
$reference = ticketReference($ticket);
|
|
$on = $reference . ' - ' . $ticket['ticket_subject'] . ($ticket['client_name'] !== null ? ' (' . $ticket['client_name'] . ')' : '');
|
|
$client_id = intval($ticket['ticket_client_id']);
|
|
$known = (strtolower((string) $ticket['contact_email']) === $from_email && $from_email !== '')
|
|
|| ($client_id && mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM contacts WHERE contact_email = '$from_sql' AND contact_client_id = $client_id AND contact_archived_at IS NULL LIMIT 1")))
|
|
|| mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM ticket_ccs WHERE ticket_cc_ticket_id = " . intval($ticket['ticket_id']) . " AND ticket_cc_email = '$from_sql' LIMIT 1"));
|
|
if (intval($ticket['ticket_status']) === TICKET_STATUS_CLOSED) {
|
|
return ['fa-ban', 'warning', "A note on $reference - it is closed",
|
|
'Closed tickets aren\'t reopened by email: it is kept on the ticket as an internal note, and the agent it was assigned to gets a notice - the admins, when nobody had it'
|
|
. ($known ? '. The sender is told it is closed, and how to raise a new ticket.' : '. The sender isn\'t a contact or CC, so nobody is told - a guessed number would tell them its subject.')];
|
|
}
|
|
$reopens = ticketMailTryReopens($ticket['ticket_status']);
|
|
if (strtolower((string) $ticket['contact_email']) === $from_email && $from_email !== '') {
|
|
return ['fa-reply', 'success', "A reply on $on", 'From its contact, ' . $ticket['contact_name'] . ', as the client\'s reply. ' . $reopens];
|
|
}
|
|
$other = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_name FROM contacts WHERE contact_email = '$from_sql' AND contact_client_id = $client_id LIMIT 1"));
|
|
if ($other) {
|
|
return ['fa-reply', 'success', "A reply on $on", 'From ' . $other['contact_name'] . ', another of the client\'s contacts, as the client\'s reply. ' . $reopens];
|
|
}
|
|
$agent = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT user_name FROM users WHERE user_email = '$from_sql' AND user_type = 1 AND user_status = 1 AND user_archived_at IS NULL LIMIT 1"));
|
|
if ($agent) {
|
|
return ['fa-sticky-note', 'info', "An internal note on $on", 'From ' . $agent['user_name'] . ', one of your agents - as their own note. Nothing is reopened. When the email went to the client too - a reply to all on their email - it is their reply to the client instead, and the ticket\'s first response.'];
|
|
}
|
|
$cc = mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM ticket_ccs WHERE ticket_cc_ticket_id = " . intval($ticket['ticket_id']) . " AND ticket_cc_email = '$from_sql' LIMIT 1"));
|
|
if ($cc) {
|
|
return ['fa-reply', 'success', "A reply on $on", 'From one of its CCs, as the client\'s reply. ' . $reopens];
|
|
}
|
|
return ['fa-user-secret', 'warning', "An internal note on $on, with a warning",
|
|
'From someone who isn\'t a contact, a CC or an agent - anyone can put a ticket number in a subject - so it is kept as a note marked as from a stranger, the ticket isn\'t reopened, and its attachments aren\'t added.'];
|
|
}
|
|
|
|
/** What a client's reply by email does to the ticket's status, as a sentence. */
|
|
function ticketMailTryReopens($status) {
|
|
global $mysqli;
|
|
$status = intval($status);
|
|
$after = intval(ticketStatusAfterClientReply($status));
|
|
if ($after === $status) {
|
|
return 'It stays ' . ticketMailTryStatusName($status) . '.';
|
|
}
|
|
return 'It goes from ' . ticketMailTryStatusName($status) . ' to ' . ticketMailTryStatusName($after) . ($status === TICKET_STATUS_RESOLVED ? ', and its resolution clock runs again' : '') . '.';
|
|
}
|
|
|
|
function ticketMailTryStatusName($status_id) {
|
|
global $mysqli;
|
|
$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT ticket_status_name FROM ticket_statuses WHERE ticket_status_id = " . intval($status_id)));
|
|
return $row ? $row[0] : 'status ' . intval($status_id);
|
|
}
|
|
|
|
/*
|
|
* Whether the sender is emailed the new ticket - not a no-reply address, and only while that email is on. $stranger:
|
|
* not a contact, so without their own subject or the ticket's link.
|
|
*/
|
|
function ticketMailTryNoReply($from_email, $stranger = false) {
|
|
global $config_ticket_client_general_notifications;
|
|
if (preg_match("/do[\W_]*not[\W_]*reply|no[\W_]*reply/i", $from_email)) {
|
|
return 'It\'s a no-reply address, so nobody is emailed back.';
|
|
}
|
|
if (intval($config_ticket_client_general_notifications) !== 1) {
|
|
return 'Emails to clients about new tickets are off, so they aren\'t emailed.';
|
|
}
|
|
return $stranger ? 'They\'re emailed its number - not its subject or a link to it, as anyone can send an email as someone else.' : 'They\'re emailed its number.';
|
|
}
|