Mail review stage 3: email-to-ticket reads the receiving server's Authentication-Results and treats an email failing DMARC for a domain that asks for reject or quarantine, or SPF with nothing else passing, as from a stranger - a reply is an internal note that reopens nothing, a new email still makes a ticket with a note but copies nobody in and gets no answer, and it never makes a contact; someone who isn't a contact is answered without their own subject; 26.12.3 changelog

This commit is contained in:
johnnyq
2026-11-30 16:50:51 -05:00
parent 899f2259fe
commit 17d2611b60
5 changed files with 135 additions and 23 deletions

View File

@@ -84,13 +84,20 @@ $read_mail_minutes = 60; // Mail someone opened before this got to it
* Ticket / Reply helpers (unchanged)
* ------------------------------------------------------------------ */
/*
* A new ticket from an email. $copied - the people it was also to or copied to, made its CCs
* (ticketMailCopiedCcs()); $message_id its Message-ID, kept with the ticket so a reply to it -
* or to all on it, before the ticket's number was ever sent - finds it (functions/ticket_mail_thread.php);
* $automatic 'automated' for one sent by a system or a list, which isn't answered (functions/ticket_mail_auto.php).
* A new ticket from an email. $email, about the email (as addReply()'s):
* copied the people it was also to or copied to, made its CCs (ticketMailCopiedCcs())
* message_id its Message-ID, kept with the ticket so a reply to it - or to all on it, before the
* ticket's number was ever sent - finds it (functions/ticket_mail_thread.php)
* automatic 'automated' for one sent by a system or a list, which isn't answered (functions/ticket_mail_auto.php)
* dropped its files too big to take, named on the ticket with any of a kind it doesn't take
* forged why the mail server that took it found it forged (ticketMailSenderCheck()), '' when it didn't:
* still a ticket, so nothing is lost, but with a note saying so - nobody it copied in is
* added, its sender isn't answered, and a ticket with no client doesn't copy them in
* One from someone who isn't a contact - a ticket with no client - is answered without its own subject:
* anyone can send an email from someone else's address, and the answer carried their words to them.
* The new ticket's id.
*/
function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message, $attachments, $original_message_file, $copied, $message_id = '', $automatic = '', array $dropped = []) {
function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message, $attachments, $original_message_file, array $email = []) {
global $mysqli, $config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_ticket_client_general_notifications, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $config_ticket_default_billable, $allowed_extensions;
$bad_pattern = "/do[\W_]*not[\W_]*reply|no[\W_]*reply/i"; // Email addresses to ignore
@@ -98,6 +105,11 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
// more for itself. Escaped again, an O'Brien was shown and saved as O\'Brien
$contact_name_raw = stripslashes((string) $contact_name);
$contact_email_raw = stripslashes((string) $contact_email);
$copied = (array) ($email['copied'] ?? []);
$message_id = (string) ($email['message_id'] ?? '');
$automatic = (string) ($email['automatic'] ?? '');
$dropped = (array) ($email['dropped'] ?? []);
$forged = (string) ($email['forged'] ?? '');
// Atomically increment and get the new ticket number
mysqli_query($mysqli, "
@@ -177,23 +189,33 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
}
}
// Found forged by the mail server that took it - said first, before anything else on the ticket
if ($forged !== '') {
ticketMailForgedNote($id, $forged, $contact_email_raw, $copied);
}
// The files that weren't added, said on the ticket - not only in the audit log
ticketMailFilesLeftOut($id, $dropped);
// The person whose email raised a ticket with no client is its CC, so its emails reach them
if ($client_id == 0 && !preg_match($bad_pattern, $contact_email_esc)) {
// The person whose email raised a ticket with no client is its CC, so its emails reach them - not when it was forged
if ($client_id == 0 && $forged === '' && !preg_match($bad_pattern, $contact_email_esc)) {
mysqli_query($mysqli, "INSERT INTO ticket_ccs SET ticket_cc_email = '$contact_email_esc', ticket_cc_ticket_id = $id");
}
// The people it was also to or copied to are its CCs - not your own addresses or agents (ticketMailCopiedCcs())
ticketMailCopiedCcs($id, $copied, stripslashes($contact_email));
// The people it was also to or copied to are its CCs - not your own addresses or agents (ticketMailCopiedCcs()), and none
// from a forged email, whose sender could copy themselves in on every reply
if ($forged === '') {
ticketMailCopiedCcs($id, $copied, $contact_email_raw);
}
// The sender is told it is a ticket now - unless they asked for replies only (Your account in the client portal), it was
// sent by a system or a list, whose answer could be answered back, or they have had a few answers this hour already
// sent by a system or a list, whose answer could be answered back, it was forged, or they have had a few answers this hour
// already. Someone who isn't a contact is told without their own subject (above)
$contact_wants = !$contact_id || ticketContactWantsEmail(getFieldById('contacts', intval($contact_id), 'contact_ticket_emails'), 'created');
if ($config_ticket_client_general_notifications == 1 && !preg_match($bad_pattern, $contact_email) && $contact_wants && $automatic === ''
&& ticketMailAnswersLeft(stripslashes($contact_email)) && ($new = ticketRow($id))) {
ticketMailCreated($id, [], false, 'email', [['email' => stripslashes($contact_email), 'name' => stripslashes($contact_name)]]);
if ($config_ticket_client_general_notifications == 1 && !preg_match($bad_pattern, $contact_email) && $contact_wants && $automatic === '' && $forged === ''
&& ticketMailAnswersLeft($contact_email_raw) && ($new = ticketRow($id))) {
ticketMailCreated($id, [], false, 'email', [['email' => $contact_email_raw, 'name' => $contact_name_raw]],
$client_id ? null : '[' . ticketReference($new) . '] We have your email');
}
// The new-ticket address (Admin > Ticket), when there is one
@@ -345,6 +367,12 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
} elseif (mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM ticket_ccs WHERE ticket_cc_ticket_id = $ticket_id AND ticket_cc_email = '$from_email_esc' LIMIT 1"))) {
$who = 'cc';
}
// Found forged by the mail server that took it (ticketMailSenderCheck()) - whoever it says it is from, it is a stranger's
$forged = (string) ($email['forged'] ?? '');
if ($forged !== '') {
$who = 'stranger';
$reply_by = 0;
}
$stranger = $who === 'stranger';
$closed = $ticket_status === TICKET_STATUS_CLOSED;
$by_name = $who === 'contact' ? (string) getFieldById('contacts', $reply_by, 'contact_name') : ($from_name !== '' ? $from_name : stripslashes($from_email));
@@ -358,7 +386,10 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
} elseif ($closed) {
$notes[] = 'A reply to the ticket after it was closed - it was not reopened';
}
if ($stranger) {
if ($forged !== '') {
$notes[] = "Its sender checks failed: $forged - so it may not be from " . stripslashes($from_email) . ($closed || $automatic === 'auto-reply' ? '' : '. The ticket was not reopened')
. ', nobody it copied in was added' . ($attachments ? ", and the email's " . count($attachments) . " attachment(s) were not added - it is in the mailbox's ITFlow folder" : '');
} elseif ($stranger) {
$notes[] = 'From someone who is not a contact, CC or agent' . ($closed || $automatic === 'auto-reply' ? '' : ' - the ticket was not reopened')
. ($attachments ? ", and the email's " . count($attachments) . " attachment(s) were not added - it is in the mailbox's ITFlow folder" : '');
}
@@ -461,6 +492,11 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
}
ticketMailAssigneeReplied($ticket_id, 'email', $mail_reply, $by_name);
if ($forged !== '') {
logTicketHistory($ticket_id, "An email that says it is from $from_email_esc failed its sender checks - kept as an internal note, the ticket was not reopened");
logAudit("Ticket", "Edit", "Email parser: an email that says it is from $from_email_esc failed its sender checks (" . escapeSql($forged) . ") - kept as an internal note on ticket $ticket_reference_esc ($subject)", $client_id, $ticket_id);
return true;
}
if ($stranger) {
logTicketHistory($ticket_id, "$from_email_esc (not a contact, CC or agent) emailed - kept as an internal note, the ticket was not reopened");
logAudit("Ticket", "Edit", "Email parser: $from_email_esc (not a contact, CC or agent) emailed ticket $ticket_reference_esc ($subject) - kept as an internal note", $client_id, $ticket_id);
@@ -864,6 +900,10 @@ foreach ($messages as $message) {
}
$automatic = ticketMailAutomaticKind($headers, stripslashes($subject));
// Whether the mail server that took it found it forged - by SPF, DKIM and DMARC (ticketMailSenderCheck()). A From line can say
// anyone: one that fails is a stranger's on a ticket, and makes no contact, adds no CCs and gets no answer
$forged = ticketMailSenderCheck($headers);
// The same email a second time - sent to two of your addresses that both land in this mailbox, say - is on its
// ticket already, kept by its Message-ID (functions/ticket_mail_thread.php). Taken again, it was a second ticket
$email_message_id = ticketMailIdClean((string) $message->messageId());
@@ -897,7 +937,7 @@ foreach ($messages as $message) {
$in_reply_to = (string) ($message->header('In-Reply-To')?->getRawValue() ?? '');
$references = (string) ($message->header('References')?->getRawValue() ?? '');
$email = ['message_id' => $email_message_id, 'from_name' => $from_name, 'copied' => $copied, 'had' => array_values(array_unique(array_filter($had))), 'threaded' => false,
'automatic' => $automatic];
'automatic' => $automatic, 'forged' => $forged];
// Date (string)
$dateObj = $message->date(); // ?CarbonInterface
@@ -1039,7 +1079,7 @@ foreach ($messages as $message) {
$contact_email = escapeSql($rowc['contact_email']);
$client_id = intval($rowc['contact_client_id']);
$email_processed = addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $copied, $email_message_id, $automatic, $dropped);
$email_processed = addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email);
}
}
@@ -1049,7 +1089,8 @@ foreach ($messages as $message) {
$domain_sql = mysqli_query($mysqli, "SELECT domain_client_id, domain_name FROM domains WHERE domain_name = '$from_domain_esc' AND domain_archived_at IS NULL LIMIT 1");
$rowd = mysqli_fetch_assoc($domain_sql);
if ($rowd && $from_domain == $rowd['domain_name'] && mb_strlen(stripslashes($from_email)) <= 200) {
// Not a forged one: anyone could make themselves a client's contact by saying they are from its domain
if ($rowd && $from_domain == $rowd['domain_name'] && mb_strlen(stripslashes($from_email)) <= 200 && $forged === '') {
$client_id = intval($rowd['domain_client_id']);
// Create a new contact - its name cut to the 200 characters the column holds, and escaped once: $from_name and
@@ -1062,13 +1103,13 @@ 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, $copied, $email_message_id, $automatic, $dropped);
$email_processed = addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file, $email);
}
}
// 6. Unknown sender allowed? A ticket with no client - a mail server's bounce never gets here
if (!$email_processed && $config_ticket_email_parse_unknown_senders) {
$email_processed = addTicket(0, $from_name, $from_email, 0, $date, $subject, $message_body, $attachments, $original_message_file, $copied, $email_message_id, $automatic, $dropped);
$email_processed = addTicket(0, $from_name, $from_email, 0, $date, $subject, $message_body, $attachments, $original_message_file, $email);
}
// A new ticket whose subject is almost that of another's at the client, from someone else - linked as related