mirror of
https://github.com/itflow-org/itflow
synced 2026-12-03 09:39:02 +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:
@@ -1288,176 +1288,89 @@ foreach ($messages as $message) {
|
||||
}
|
||||
}
|
||||
|
||||
// 1. A reply in a ticket's conversation - it answers one of the emails on the ticket, by its In-Reply-To or
|
||||
// References - whatever its subject says (functions/ticket_mail_thread.php)
|
||||
$thread_ticket_id = !in_array($asked, ['reply', 'ticket'], true) ? ticketMailHeaderTicket($in_reply_to, $references) : 0;
|
||||
if ($thread_ticket_id) {
|
||||
$email['threaded'] = true;
|
||||
$email_processed = addReply($from_email, $date, $subject, $thread_ticket_id, $message_body, $attachments, $original_message_file, $email);
|
||||
}
|
||||
|
||||
// 2. Reply to existing ticket with the number in subject - with the prefix that ticket was given
|
||||
if (!$email_processed && !$asked_only && ($subject_ticket = ticketMailSubjectTicket(stripslashes($subject)))) {
|
||||
$email_processed = addReply($from_email, $date, $subject, $subject_ticket['ticket_id'], $message_body, $attachments, $original_message_file, $email);
|
||||
}
|
||||
|
||||
// A client's email one of your agents forwarded to the mailbox - the client's, not the agent's: from here on it is from
|
||||
// whoever the forwarded part says, so it goes to the client whose email it is, and what the agent wrote above it is
|
||||
// their note on the ticket (functions/ticket_mail_forward.php). It was a ticket for your own company, or none.
|
||||
// Not a forged one - anyone can write an agent's address in a From line - nor a reply to a ticket (above): an agent's
|
||||
// email with a ticket's number is their note on it, as always. A colleague's email forwarded is the agent's, as before
|
||||
// Where it goes - a reply on a ticket, a ticket, set aside or left - decided by ticketMailDecide()
|
||||
// (functions/ticket_mail_decide.php), the same steps Try an Email shows, and done here. An agent's email that forwards a
|
||||
// client's is read from the forwarded part (functions/ticket_mail_forward.php) and decided again, as the client's: it goes
|
||||
// to the client whose email it is, and what the agent wrote above it is their note on the ticket. Not a forged one - anyone
|
||||
// can write an agent's address in a From line - nor a reply to a ticket: an agent's email with a ticket's number is their
|
||||
// note on it, as always. A colleague's email forwarded is the agent's, as before
|
||||
$decision = null;
|
||||
$forward = null;
|
||||
if (!$email_processed && !$asked_only && $forged === '' && $automatic === '' && !$answer
|
||||
&& ($forward_agent = ticketMailForwardAgent($from_raw))) {
|
||||
$forward = ticketMailForwarded(!empty($message_body_html) ? $message_body : '', (string) $message_body_text, stripslashes($subject), $attachments);
|
||||
if ($forward && ticketMailForwardFromClient($forward['email'])) {
|
||||
logApp("Cron-Email-Parser", "info", escapeSql("Email parser took an email " . $forward_agent['user_name'] . " forwarded (" . stripslashes($subject) . ") as from " . $forward['email']));
|
||||
$from_raw = $forward['email'];
|
||||
$from_email = escapeSql($from_raw);
|
||||
$from_name = escapeSql($forward['name'] !== '' ? $forward['name'] : 'Unknown');
|
||||
$from_domain = strtolower(substr($from_raw, strrpos($from_raw, '@') + 1));
|
||||
$subject = escapeSql(mb_substr($forward['subject'], 0, 500));
|
||||
if ($forward['html'] !== '') {
|
||||
$message_body = $forward['html'];
|
||||
}
|
||||
$via = '';
|
||||
$email['via'] = '';
|
||||
$email['from_name'] = $from_name;
|
||||
$email['forward'] = ['agent' => $forward_agent, 'note' => $forward['note']];
|
||||
} else {
|
||||
$forward = null;
|
||||
}
|
||||
}
|
||||
|
||||
// An email routing rule (Admin > Email Routing, functions/mail_route.php) - the most exact one for its sender and subject
|
||||
// sets up the ticket it raises, and the client it is for over the sender's own contact or domain. Not an out-of-office,
|
||||
// which is only ever a note on what it answers; a reply to a ticket was matched above
|
||||
$route = null;
|
||||
if (!$email_processed && !$asked_only && $automatic !== 'auto-reply') {
|
||||
$route = mailRouteFor($from_raw, stripslashes($subject));
|
||||
if ($route && $route['gone']) {
|
||||
logApp("Cron-Email-Parser", "warning", escapeSql("The email routing rule for " . $route['name'] . " names what isn't there any more - " . implode(', ', $route['gone']) . " - fix it in Admin > Email Routing"));
|
||||
}
|
||||
}
|
||||
|
||||
// The sender's contact - at the client whose domain it is, when they are one at more than one (ticketMailSenderContact()) -
|
||||
// or the client whose domain it is. A website form's ($answer) is never a contact: its client only
|
||||
$picked = ticketMailSenderContact($from_raw, $from_domain);
|
||||
$contact_row = $picked['contact'];
|
||||
$domain_row = null;
|
||||
if (!$contact_row && $from_domain !== '') {
|
||||
$domain_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT domain_client_id, domain_name FROM domains WHERE domain_name = '" . escapeSql($from_domain) . "' AND domain_archived_at IS NULL LIMIT 1"));
|
||||
$domain_row = $domain_row && strcasecmp($from_domain, $domain_row['domain_name']) === 0 ? $domain_row : null;
|
||||
}
|
||||
|
||||
// Routed to a client: the rule's, over the sender's own contact or domain. Its contact is the rule's, else the sender
|
||||
// when they are one of that client's contacts, else nobody. A website form's is from its visitor, as ever
|
||||
$route_client_id = $route && $route['client'] ? intval($route['client']['client_id']) : 0;
|
||||
if ($route_client_id) {
|
||||
$picked = ['contact' => null, 'others' => [], 'why' => ''];
|
||||
$domain_row = null;
|
||||
$contact_row = null;
|
||||
if (!$answer) {
|
||||
$contact_row = $route['contact'] ?: (mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_id, contact_name, contact_email, contact_client_id FROM contacts
|
||||
WHERE contact_client_id = $route_client_id AND contact_email = '" . escapeSql($from_raw) . "' AND contact_archived_at IS NULL LIMIT 1")) ?: null);
|
||||
}
|
||||
}
|
||||
// A routed email's sender is answered and copied in only as the ticket's contact - who they are, or who they will be
|
||||
// as a new contact at a client's domain: a system's address isn't sent "we have your email" and every reply after it
|
||||
$route_email = [];
|
||||
if ($route) {
|
||||
$route_sender_is_contact = $answer || ($contact_row && strcasecmp((string) $contact_row['contact_email'], $from_raw) === 0) || ($domain_row && $forged === '');
|
||||
$route_email = ['route' => $route + ['quiet' => !$route_sender_is_contact]];
|
||||
if ($contact_row && strcasecmp((string) $contact_row['contact_email'], $from_raw) !== 0) {
|
||||
$route_email['sender'] = ['name' => stripslashes($from_name), 'email' => $from_raw];
|
||||
}
|
||||
}
|
||||
|
||||
// An automatic reply - an out-of-office - that answers no ticket is set aside: it isn't a ticket, and isn't answered.
|
||||
// With unknown senders not let in, a stranger's is ITFlow's only when it answers an email ITFlow sent: an
|
||||
// out-of-office to an email someone sent from the mailbox by hand goes on, and is left in the inbox as a stranger's is
|
||||
if (!$email_processed && !$asked_only && $automatic === 'auto-reply') {
|
||||
if (!$asked_only) {
|
||||
// An out-of-office that answers an email ITFlow sent is ITFlow's to take, whoever it is from
|
||||
$answers_ours = false;
|
||||
foreach (ticketMailIdsIn($in_reply_to . ' ' . $references) as $answered_id) {
|
||||
$answers_ours = $answers_ours || emailMessageIdOurs($answered_id);
|
||||
if ($automatic === 'auto-reply') {
|
||||
foreach (ticketMailIdsIn($in_reply_to . ' ' . $references) as $answered_id) {
|
||||
$answers_ours = $answers_ours || emailMessageIdOurs($answered_id);
|
||||
}
|
||||
}
|
||||
if ($config_ticket_email_parse_unknown_senders || $contact_row || ($domain_row && $forged === '') || $answers_ours || $message instanceof MailUnprocessedMessage) {
|
||||
logApp("Cron-Email-Parser", "info", "Email parser set aside an automatic reply from $from_email ($subject) - it answers no ticket");
|
||||
$email_processed = true;
|
||||
}
|
||||
}
|
||||
$facts = ['from' => $from_raw, 'from_name' => stripslashes($from_name), 'subject' => stripslashes($subject),
|
||||
'visitor' => $answer ? ['email' => stripslashes($answer['email']), 'name' => stripslashes($answer['name'])] : null,
|
||||
'thread_ticket_id' => ticketMailHeaderTicket($in_reply_to, $references), 'automatic' => $automatic, 'forged' => $forged,
|
||||
'answers_ours' => $answers_ours, 'unprocessed' => $message instanceof MailUnprocessedMessage];
|
||||
$decision = ticketMailDecide($facts);
|
||||
|
||||
// 3. Almost the same subject as an open ticket of the sender's client from the last week - a reply on it when they are
|
||||
// its contact or a CC on it. From anyone else at the client it is a ticket of its own, linked to that one as related
|
||||
// (linkSimilarTicket()): two people with "Printer not working" in a week used to land on one ticket.
|
||||
$similar_ticket_id = 0;
|
||||
if (!$email_processed && !$asked_only && strlen(trim($subject)) > 10) {
|
||||
$contact_id = $contact_row && !$answer ? intval($contact_row['contact_id']) : 0;
|
||||
$client_id = $route_client_id ?: ($contact_row ? intval($contact_row['contact_client_id']) : intval($domain_row['domain_client_id'] ?? 0));
|
||||
$from_email_esc = mysqli_real_escape_string($mysqli, $from_email);
|
||||
|
||||
// If we found either a contact or a domain, check recent tickets for a matching subject
|
||||
if ($client_id) {
|
||||
$recent_tickets_sql = mysqli_query($mysqli,
|
||||
"SELECT ticket_id, 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 ($rowt = mysqli_fetch_assoc($recent_tickets_sql)) {
|
||||
$existing_subject = $rowt['ticket_subject'];
|
||||
|
||||
// Calculate similarity percentage
|
||||
similar_text(strtolower($subject), strtolower($existing_subject), $percent);
|
||||
|
||||
if ($percent >= 95) {
|
||||
$similar_id = intval($rowt['ticket_id']);
|
||||
// Forwarded by an agent, it is a ticket of its own however alike - their note goes with a ticket. So is a
|
||||
// routed one whose sender isn't the ticket's contact: a system's alerts aren't the client's replies
|
||||
$theirs = !$answer && !$forward && !($route_email['route']['quiet'] ?? false) && (($contact_id && $contact_id === intval($rowt['ticket_contact_id']))
|
||||
|| mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM ticket_ccs WHERE ticket_cc_ticket_id = $similar_id AND ticket_cc_email = '$from_email_esc' LIMIT 1")));
|
||||
if ($theirs) {
|
||||
$email_processed = addReply($from_email, $date, $subject, $similar_id, $message_body, $attachments, $original_message_file, $email);
|
||||
} else {
|
||||
$similar_ticket_id = $similar_id;
|
||||
}
|
||||
break;
|
||||
if ($decision['agent']) {
|
||||
$forward_agent = $decision['agent'];
|
||||
$forward = ticketMailForwarded(!empty($message_body_html) ? $message_body : '', (string) $message_body_text, stripslashes($subject), $attachments);
|
||||
if ($forward && ticketMailForwardFromClient($forward['email'])) {
|
||||
logApp("Cron-Email-Parser", "info", escapeSql("Email parser took an email " . $forward_agent['user_name'] . " forwarded (" . stripslashes($subject) . ") as from " . $forward['email']));
|
||||
$from_raw = $forward['email'];
|
||||
$from_email = escapeSql($from_raw);
|
||||
$from_name = escapeSql($forward['name'] !== '' ? $forward['name'] : 'Unknown');
|
||||
$from_domain = strtolower(substr($from_raw, strrpos($from_raw, '@') + 1));
|
||||
$subject = escapeSql(mb_substr($forward['subject'], 0, 500));
|
||||
if ($forward['html'] !== '') {
|
||||
$message_body = $forward['html'];
|
||||
}
|
||||
$via = '';
|
||||
$email['via'] = '';
|
||||
$email['from_name'] = $from_name;
|
||||
$email['forward'] = ['agent' => $forward_agent, 'note' => $forward['note']];
|
||||
$decision = ticketMailDecide(['from' => $from_raw, 'from_name' => stripslashes($from_name), 'subject' => stripslashes($subject), 'forward' => $forward_agent] + $facts);
|
||||
} else {
|
||||
$forward = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. A known, registered contact? A website form sent as one - an address made a contact before - is for that
|
||||
// contact's client, from its visitor
|
||||
if (!$email_processed && !$asked_only && $contact_row) {
|
||||
$client_id = intval($contact_row['contact_client_id']);
|
||||
if ($answer) {
|
||||
$email_processed = addTicket(0, $answer['name'], $answer['email'], $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email + $route_email);
|
||||
} else {
|
||||
$contact_name = escapeSql($contact_row['contact_name']);
|
||||
$contact_id = intval($contact_row['contact_id']);
|
||||
$contact_email = escapeSql($contact_row['contact_email']);
|
||||
$email_processed = addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email + ['picked' => $picked] + $route_email);
|
||||
$route = $decision['route'] ?? null;
|
||||
if ($route && $route['gone']) {
|
||||
logApp("Cron-Email-Parser", "warning", escapeSql("The email routing rule for " . $route['name'] . " names what isn't there any more - " . implode(', ', $route['gone']) . " - fix it in Admin > Email Routing"));
|
||||
}
|
||||
|
||||
// 1-3. A reply on a ticket - in its conversation, with its number in the subject, or with almost the same subject as an
|
||||
// open ticket of theirs from the last week
|
||||
if ($decision && $decision['action'] === 'reply') {
|
||||
$email['threaded'] = $decision['by'] === 'thread';
|
||||
$email_processed = addReply($from_email, $date, $subject, $decision['ticket_id'], $message_body, $attachments, $original_message_file, $email);
|
||||
}
|
||||
|
||||
// An out-of-office that answers no ticket - set aside: it isn't a ticket, and isn't answered
|
||||
if ($decision && $decision['action'] === 'set_aside') {
|
||||
logApp("Cron-Email-Parser", "info", "Email parser set aside an automatic reply from $from_email ($subject) - it answers no ticket");
|
||||
$email_processed = true;
|
||||
}
|
||||
|
||||
// 4-6. A ticket - a contact's, a routed client's, a new contact's at a client's domain, or nobody's - from its sender, or a
|
||||
// website form's visitor. Set up by its routing rule, saying on it which client the sender is a contact at as well
|
||||
if ($decision && $decision['action'] === 'ticket') {
|
||||
$client_id = intval($decision['client_id']);
|
||||
$set_up = [];
|
||||
if ($route) {
|
||||
$set_up['route'] = $route + ['quiet' => $decision['quiet']];
|
||||
}
|
||||
if ($decision['sender']) {
|
||||
$set_up['sender'] = $decision['sender'];
|
||||
}
|
||||
if ($decision['picked']) {
|
||||
$set_up['picked'] = $decision['picked'];
|
||||
}
|
||||
}
|
||||
|
||||
// Routed to a client with no contact for it - from its sender, or a website form's visitor
|
||||
if (!$email_processed && !$asked_only && $route_client_id) {
|
||||
$email_processed = $answer
|
||||
? addTicket(0, $answer['name'], $answer['email'], $route_client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email + $route_email)
|
||||
: addTicket(0, $from_name, $from_email, $route_client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email + $route_email);
|
||||
}
|
||||
|
||||
// 5. A known domain? Not a forged one: anyone could make themselves a client's contact by saying they are from its domain.
|
||||
// A website form's is the client's ticket, from its visitor - the site isn't made a contact
|
||||
if (!$email_processed && !$asked_only && $domain_row && mb_strlen($from_raw) <= 200 && $forged === '') {
|
||||
$client_id = intval($domain_row['domain_client_id']);
|
||||
if ($answer) {
|
||||
$email_processed = addTicket(0, $answer['name'], $answer['email'], $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email + $route_email);
|
||||
} else {
|
||||
// Create a new contact - its name cut to the 200 characters the column holds, and escaped once: $from_name and
|
||||
// $from_email come SQL-escaped, and escaped again, an O'Brien was saved as O\'Brien
|
||||
$email_processed = addTicket(0, $answer['name'], $answer['email'], $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email + $set_up);
|
||||
} elseif ($decision['new_contact']) {
|
||||
// A new contact at the client whose domain it is - its name cut to the 200 characters the column holds, and escaped
|
||||
// once: $from_name and $from_email come SQL-escaped, and escaped again, an O'Brien was saved as O\'Brien
|
||||
$contact_name = mysqli_real_escape_string($mysqli, rtrim(mb_substr(stripslashes($from_name), 0, 200)));
|
||||
$contact_email = $from_email;
|
||||
$contact_notes = $forward ? escapeSql('Added from an email ' . $email['forward']['agent']['user_name'] . ' forwarded.') : 'Added automatically via email parsing.';
|
||||
@@ -1467,28 +1380,25 @@ foreach ($messages as $message) {
|
||||
logAudit("Contact", "Create", "Email parser: created contact $contact_name", $client_id, $contact_id);
|
||||
triggerCustomAction('contact_create', $contact_id);
|
||||
|
||||
$email_processed = addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email + $route_email);
|
||||
$email_processed = addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email + $set_up);
|
||||
} elseif ($decision['contact']) {
|
||||
$contact = $decision['contact'];
|
||||
$email_processed = addTicket(intval($contact['contact_id']), escapeSql($contact['contact_name']), escapeSql($contact['contact_email']), $client_id, $date, $subject, $message_body,
|
||||
$attachments, $original_message_file, $email + $set_up);
|
||||
} else {
|
||||
$email_processed = addTicket(0, $from_name, $from_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email + $set_up);
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Unknown sender allowed? A ticket with no client - a mail server's bounce never gets here. A website form's from its
|
||||
// visitor; one with no sender address from nobody, answered by nobody. One an agent forwarded is always taken: they
|
||||
// sent it in to be a ticket, and give it its client. So is one a routing rule takes - it was set up to
|
||||
if (!$email_processed && !$asked_only && ($config_ticket_email_parse_unknown_senders || $forward || $route)) {
|
||||
$email_processed = $answer
|
||||
? addTicket(0, $answer['name'], $answer['email'], 0, $date, $subject, $message_body, $attachments, $original_message_file, $email + $route_email)
|
||||
: addTicket(0, $from_name, $from_email, 0, $date, $subject, $message_body, $attachments, $original_message_file, $email + $route_email);
|
||||
}
|
||||
// Almost the subject of another of the client's tickets, from someone else - linked as related
|
||||
if ($decision['similar'] && is_int($email_processed) && $email_processed > 0) {
|
||||
linkSimilarTicket($email_processed, intval($decision['similar']['ticket_id']), $decision['link_why']);
|
||||
}
|
||||
|
||||
// A new ticket whose subject is almost that of another's at the client, from someone else - linked as related
|
||||
if ($similar_ticket_id && is_int($email_processed) && $email_processed > 0) {
|
||||
linkSimilarTicket($email_processed, $similar_ticket_id, $forward ? 'forward' : ($route_email['route']['quiet'] ?? false ? 'route' : ''));
|
||||
}
|
||||
|
||||
// A routing rule raised a ticket - counted on its rule, with when
|
||||
if ($route && is_int($email_processed) && $email_processed > 0) {
|
||||
mailRouteHit($route['rule']['routing_rule_id']);
|
||||
logApp("Cron-Email-Parser", "info", escapeSql("Email parser routed an email from $from_raw (" . stripslashes($subject) . ") by the rule for " . $route['name'] . " - " . mailRouteSummary($route)));
|
||||
// A routing rule raised it - counted on its rule, with when
|
||||
if ($route && is_int($email_processed) && $email_processed > 0) {
|
||||
mailRouteHit($route['rule']['routing_rule_id']);
|
||||
logApp("Cron-Email-Parser", "info", escapeSql("Email parser routed an email from $from_raw (" . stripslashes($subject) . ") by the rule for " . $route['name'] . " - " . mailRouteSummary($route)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1524,14 +1434,12 @@ foreach ($messages as $message) {
|
||||
$why = $asked_failed;
|
||||
} elseif ($asked_only) {
|
||||
$why = 'It couldn\'t be ' . ($asked === 'reply' ? 'added to the ticket' : 'made a ticket') . ' - try again, or pick something else';
|
||||
} elseif ($config_ticket_email_parse_unknown_senders || $contact_row) {
|
||||
$why = 'It couldn\'t be made a ticket';
|
||||
} elseif ($domain_row && $forged !== '') {
|
||||
$why = "Its sender checks failed - $forged - so it isn't taken as from the client whose domain it is from";
|
||||
$not_ours = true;
|
||||
} elseif ($decision && $decision['action'] === 'leave') {
|
||||
// Nobody's - a stranger's, or not who it says (ticketMailDecide())
|
||||
$why = $decision['why'];
|
||||
$not_ours = $decision['not_ours'];
|
||||
} else {
|
||||
$why = 'From someone who isn\'t a contact, at a domain no client has';
|
||||
$not_ours = true;
|
||||
$why = 'It couldn\'t be made a ticket';
|
||||
}
|
||||
if ($not_ours && !($message instanceof MailUnprocessedMessage)) {
|
||||
$message->markFlagged();
|
||||
|
||||
Reference in New Issue
Block a user