Mail review stage 5: a website form's email is answered at the visitor in its Reply-To, who becomes the ticket's CC under the client whose domain the site is on or none, and Reply-To at the sender's own domain is taken as the sender; someone who isn't a contact is answered without their subject or the ticket's link, and a forged ticket or one with no contact no longer emails the client's colleagues; a client's reply on a ticket nobody is assigned goes to the new-ticket address or the admins, and bounce and closed-ticket notices go to the assigned agent or the admins instead of every agent; an email with no From is from nobody rather than itflow-guest@example.com; a forged email no longer clears Email bounced; an address that is a contact at two clients goes to the one whose domain it is or where they last raised a ticket, with a note; an agent's emailed reply that went to the client is their public reply and the first response; 26.12.3 changelog

This commit is contained in:
johnnyq
2026-11-30 18:22:59 -05:00
parent 039c1b11da
commit e57faeeb3a
9 changed files with 422 additions and 126 deletions

View File

@@ -92,10 +92,14 @@ $read_mail_minutes = 60; // Mail someone opened before this got to it
* 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.
* added, its sender isn't answered, a ticket with no contact doesn't copy them in, and the
* client's colleagues who hear about new tickets aren't emailed its subject
* via the address it was sent from, when it is taken as from its Reply-To (ticketMailReplyTo()) -
* shown beside who it is from
* picked the sender is a contact at more than one client (ticketMailSenderContact()) - said in a note
* One from someone who isn't a contact ($contact_id 0) is answered without its own subject or a link to the
* ticket: anyone can send an email from someone else's address, and the answer carried their words to them.
* One with no sender address ($contact_email '') is answered by nobody. The new ticket's id.
*/
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;
@@ -110,6 +114,7 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
$automatic = (string) ($email['automatic'] ?? '');
$dropped = (array) ($email['dropped'] ?? []);
$forged = (string) ($email['forged'] ?? '');
$via = (string) ($email['via'] ?? '');
// Atomically increment and get the new ticket number
mysqli_query($mysqli, "
@@ -134,7 +139,9 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
// Convert newlines to <br>
$message = nl2br($message);
// Wrap final formatted message
$message = "<i>Email from: <b>" . escapeHtml($contact_name_raw) . "</b> &lt;" . escapeHtml($contact_email_raw) . "&gt; at $date:-</i> <br><br><div style='line-height:1.5;'>$message</div>";
$from_line = $contact_email_raw !== '' ? "<b>" . escapeHtml($contact_name_raw) . "</b> &lt;" . escapeHtml($contact_email_raw) . "&gt;" : '<b>No sender address</b>';
$from_line .= $via !== '' ? ' - sent by ' . escapeHtml($via) : '';
$message = "<i>Email from: $from_line at $date:-</i> <br><br><div style='line-height:1.5;'>$message</div>";
$ticket_prefix_esc = mysqli_real_escape_string($mysqli, $config_ticket_prefix);
$message_esc = mysqli_real_escape_string($mysqli, $message);
@@ -194,11 +201,15 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
ticketMailForgedNote($id, $forged, $contact_email_raw, $copied);
}
// From a contact at more than one client - which it went to, and the others
ticketMailSenderContactNote($id, $contact_email_raw, (array) ($email['picked'] ?? []));
// 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 - not when it was forged
if ($client_id == 0 && $forged === '' && !preg_match($bad_pattern, $contact_email_esc)) {
// The person whose email raised a ticket with no contact is its CC, so its emails reach them - a website form's visitor, at
// a client's domain or none - not when it was forged
if (!$contact_id && $forged === '' && $contact_email_raw !== '' && !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");
}
@@ -210,19 +221,21 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
// 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, 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)
// already. Someone who isn't a contact is told without their own subject or the ticket's link (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 === '' && $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');
if ($config_ticket_client_general_notifications == 1 && $contact_email_raw !== '' && !preg_match($bad_pattern, $contact_email) && $contact_wants && $automatic === '' && $forged === ''
&& ticketMailAnswersLeft($contact_email_raw)) {
ticketMailCreated($id, [], false, 'email', [['email' => $contact_email_raw, 'name' => $contact_name_raw]], !$contact_id);
}
// The new-ticket address (Admin > Ticket), when there is one
ticketMailNewTicketAddress($id, 'email');
// The client's primary and technical contacts who asked to hear about new tickets
ticketMailColleaguesNew($id);
// The client's primary and technical contacts who asked to hear about new tickets - not of a forged one, whose subject
// would go to them from your own mailbox, nor of one from someone who isn't a contact (a form on the client's website)
if ($forged === '' && $contact_id) {
ticketMailColleaguesNew($id);
}
triggerCustomAction('ticket_create', $id);
@@ -233,8 +246,8 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
* A mail server's report that an email couldn't be delivered (ticketMailIsBounce()) - never a ticket, never answered.
* The addresses it failed for are marked on their contacts ("Email bounced", functions/contact_bounce.php), the ticket
* the email was about gets a note - found by the Message-ID of the email that bounced, as ITFlow keeps each it sends
* about a ticket, else by the number in its subject - and agents get a notice. A report that an email is only delayed
* is set aside.
* about a ticket, else by the number in its subject - and the agent it is assigned to gets a notice, or the admins when
* nobody has it or it is about no ticket (ticketNotifyOwner()). A report that an email is only delayed is set aside.
*/
function addBounce($message, array $headers, $from_email, $subject) {
global $mysqli;
@@ -273,9 +286,8 @@ function addBounce($message, array $headers, $from_email, $subject) {
ticket_reply_time_worked = '00:00:00', ticket_reply_by = 0, ticket_reply_ticket_id = $ticket_id");
logTicketHistory($ticket_id, escapeSql("An email to $to couldn't be delivered - noted"));
}
appNotify("Ticket", escapeSql("Email parser: an email to $to bounced" . ($ticket ? ' - ' . ticketReference($ticket) . ' has a note' : '')
. ($why !== '' ? ". The mail server said: " . mb_strimwidth($why, 0, 300, '...') : '') . ". Subject: " . ($bounce['subject'] !== '' ? $bounce['subject'] : stripslashes($subject))),
$ticket ? "/agent/ticket.php?ticket_id=$ticket_id" . ($client_id ? "&client_id=$client_id" : '') : '', $client_id, $ticket_id);
ticketNotifyOwner($ticket, escapeSql("Email parser: an email to $to bounced" . ($ticket ? ' - ' . ticketReference($ticket) . ' has a note' : '')
. ($why !== '' ? ". The mail server said: " . mb_strimwidth($why, 0, 300, '...') : '') . ". Subject: " . ($bounce['subject'] !== '' ? $bounce['subject'] : stripslashes($subject))));
}
/*
@@ -304,12 +316,15 @@ function linkSimilarTicket($new_id, $similar_id) {
* reply passed on, as anyone can put a ticket number in a subject
* automatic auto-reply (an out-of-office) or automated (a system, a list) - never answered or passed on
* (functions/ticket_mail_auto.php)
* via the address it was sent from, when it is taken as from its Reply-To (ticketMailReplyTo())
*
* Who it is from decides what it is: the ticket's contact, another of the client's contacts, or one of its CCs
* (the person whose email raised a ticket with no client is one) - a client reply, which reopens the ticket; one
* of your agents - their own internal note; anyone else - an internal note with a warning, which reopens nothing
* and takes no attachments. An automatic reply is a note that reopens nothing, and an email to a closed ticket is
* kept on it as a note - it isn't reopened.
* (the person whose email raised a ticket with no contact is one) - a client reply, which reopens the ticket; one
* of your agents - their own internal note, or their reply to the client when it went to the client too (a reply
* to all on the client's email), which the client has: public, and the ticket's first response; anyone else, or
* no sender address at all - an internal note with a warning, which reopens nothing and takes no attachments. An
* automatic reply is a note that reopens nothing, and an email to a closed ticket is kept on it as a note - it
* isn't reopened.
*/
function addReply($from_email, $date, $subject, $ticket_id, $message, $attachments, $original_message_file = null, array $email = []) {
global $mysqli, $allowed_extensions;
@@ -317,6 +332,8 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
$automatic = (string) ($email['automatic'] ?? '');
$from_name = trim(stripslashes((string) ($email['from_name'] ?? '')));
$from_name = $from_name === 'Unknown' ? '' : $from_name;
$via = (string) ($email['via'] ?? '');
$no_sender = trim(stripslashes((string) $from_email)) === '';
// 1) What they wrote, without the email of ours they quote - above the reply line, or below it when
// that's where they wrote (emailReplyText(), functions/email.php)
@@ -331,7 +348,9 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
$message = nl2br($message);
// 3) Who it is from and when, above it - by name too, when the email gives one
$from_line = $from_name !== '' ? '<b>' . escapeHtml($from_name) . '</b> &lt;' . escapeHtml(stripslashes($from_email)) . '&gt;' : escapeHtml(stripslashes($from_email));
$from_line = $no_sender ? '<b>No sender address</b>'
: ($from_name !== '' ? '<b>' . escapeHtml($from_name) . '</b> &lt;' . escapeHtml(stripslashes($from_email)) . '&gt;' : escapeHtml(stripslashes($from_email)));
$from_line .= $via !== '' ? ' - sent by ' . escapeHtml($via) : '';
$message = "<i>Email from: $from_line at $date:-</i><br><br><div style='line-height:1.5;'>$message</div>";
$from_email_esc = mysqli_real_escape_string($mysqli, $from_email);
@@ -350,12 +369,14 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
$ticket_reference_esc = escapeSql(ticketReference($row));
$ticket_status = intval($row['ticket_status']);
$client_id = intval($row['ticket_client_id']);
$client_uri = $client_id ? "&client_id=$client_id" : '';
// Who wrote it - contact (the ticket's, or another of the client's), CC, agent or stranger
// Who wrote it - contact (the ticket's, or another of the client's), CC, agent or stranger. No sender address is a stranger's:
// matched, it was any contact or agent with no email address
$who = 'stranger';
$reply_by = 0;
if (trim((string) $row['contact_email']) !== '' && strcasecmp(trim((string) $row['contact_email']), stripslashes($from_email)) === 0) {
if ($no_sender) {
$who = 'stranger';
} elseif (trim((string) $row['contact_email']) !== '' && strcasecmp(trim((string) $row['contact_email']), stripslashes($from_email)) === 0) {
$who = 'contact';
$reply_by = intval($row['ticket_contact_id']);
} elseif ($client_id && ($contact_row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT contact_id FROM contacts WHERE contact_email = '$from_email_esc' AND contact_client_id = $client_id AND contact_archived_at IS NULL LIMIT 1")))) {
@@ -375,11 +396,16 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
}
$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));
$by_name = $who === 'contact' ? (string) getFieldById('contacts', $reply_by, 'contact_name') : ($from_name !== '' ? $from_name : ($no_sender ? 'Someone with no address' : stripslashes($from_email)));
// What it is on the ticket: a client reply from the client or a CC; a note from an agent, a stranger, an automatic
// reply, or anything on a closed ticket
$type = in_array($who, ['contact', 'cc'], true) && $automatic !== 'auto-reply' && !$closed ? 'Client' : 'Internal';
// An agent's email that went to the client too - they answered the client's email with reply to all, the ticket's
// address copied in. The client has it: it is the agent's reply, not a note only your team sees
$to_client = $who === 'agent' && !$closed && $automatic === ''
? ticketMailClientAddresses($ticket_id, array_diff((array) ($email['had'] ?? []), [strtolower(stripslashes($from_email))])) : [];
// What it is on the ticket: a client reply from the client or a CC; an agent's reply when it went to the client; a note
// from an agent, a stranger, an automatic reply, or anything on a closed ticket
$type = in_array($who, ['contact', 'cc'], true) && $automatic !== 'auto-reply' && !$closed ? 'Client' : ($to_client ? 'Public' : 'Internal');
$notes = [];
if ($automatic === 'auto-reply') {
$notes[] = 'An automatic reply - the ticket was not reopened';
@@ -390,7 +416,7 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
$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')
$notes[] = ($no_sender ? 'It has no sender address' : '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" : '');
}
if ($notes) {
@@ -459,18 +485,25 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
return true;
}
// A closed ticket isn't reopened. Agents are told it is on it, and the client or CC that nobody will act on it -
// not a stranger (a guessed number would tell them the ticket's subject), an agent, or anything automatic, whose
// answer would only be answered, and not more than a few times an hour (functions/ticket_mail_auto.php)
// A closed ticket isn't reopened. The agent it was assigned to is told it is on it - or the admins, when nobody had it
// (ticketNotifyOwner()) - and the client or CC that nobody will act on it: not a stranger (a guessed number would tell
// them the ticket's subject), an agent, or anything automatic, whose answer would only be answered, and not more than
// a few times an hour (functions/ticket_mail_auto.php)
if ($closed) {
appNotify("Ticket", "Email parser: $from_email replied to closed ticket $ticket_reference_esc - it is on the ticket as a note, and the ticket was not reopened", "/agent/ticket.php?ticket_id=$ticket_id$client_uri", $client_id, $ticket_id);
logTicketHistory($ticket_id, "$from_email_esc replied after it was closed - kept as a note");
$who_esc = $no_sender ? 'An email with no sender address' : $from_email_esc;
ticketNotifyOwner(ticketRow($ticket_id), "Email parser: $who_esc replied to closed ticket $ticket_reference_esc - it is on the ticket as a note, and the ticket was not reopened");
logTicketHistory($ticket_id, "$who_esc replied after it was closed - kept as a note");
if (in_array($who, ['contact', 'cc'], true) && $automatic === '' && ticketMailAnswersLeft(stripslashes($from_email))) {
ticketMailClosedReply($ticket_id, stripslashes($from_email), (string) ($email['message_id'] ?? ''));
}
return true;
}
if ($who === 'agent' && $to_client) {
addReplyAgentToClient($ticket_id, $reply_id, $reply_by, $to_client, $ticket_status, $row);
logAudit("Ticket", "Edit", "Email parser: Agent $from_email_esc replied to the client on ticket $ticket_reference_esc ($subject) by email", $client_id, $ticket_id);
return true;
}
if ($who === 'agent') {
logTicketHistory($ticket_id, "$from_email_esc (an agent) emailed - added as their internal note");
logAudit("Ticket", "Edit", "Email parser: Agent $from_email_esc added an internal note to ticket $ticket_reference_esc ($subject)", $client_id, $ticket_id);
@@ -498,8 +531,9 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
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);
$stranger_esc = $no_sender ? 'An email with no sender address' : "$from_email_esc (not a contact, CC or agent)";
logTicketHistory($ticket_id, "$stranger_esc emailed - kept as an internal note, the ticket was not reopened");
logAudit("Ticket", "Edit", "Email parser: $stranger_esc emailed ticket $ticket_reference_esc ($subject) - kept as an internal note", $client_id, $ticket_id);
return true;
}
@@ -530,6 +564,44 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
return true;
}
/*
* An agent's email to ticket $ticket_id that went to the client too - $to_client, the client's addresses it was to or
* copied to (ticketMailClientAddresses()) - saved as their public reply, $reply_id. Nothing is sent: the client has it.
* Kept with who it went to, as a reply sent from the ticket is; it is the ticket's first response; a New ticket is Open;
* and one nobody had is theirs, as a reply from the ticket page makes it (Admin > Settings > Tickets).
*/
function addReplyAgentToClient($ticket_id, $reply_id, $user_id, array $to_client, $ticket_status, array $row) {
global $mysqli;
$ticket_id = intval($ticket_id);
$user_id = intval($user_id);
if (ticketReplyEmailReady()) {
$to = array_map(function ($email) {
return ['email' => $email, 'name' => ''];
}, $to_client);
mysqli_query($mysqli, "UPDATE ticket_replies SET ticket_reply_emailed = 1, ticket_reply_emailed_to = '" . escapeSql(json_encode($to, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) . "',
ticket_reply_updated_at = ticket_reply_updated_at WHERE ticket_reply_id = " . intval($reply_id));
}
setTicketFirstResponse($ticket_id);
$agent_name = (string) getFieldById('users', $user_id, 'user_name');
logTicketHistory($ticket_id, escapeSql("$agent_name replied to " . implode(', ', $to_client) . " by email, the ticket's address copied in - added as their reply"));
// As if from the ticket page, by the agent - so the history and audit log name them, and they aren't told of their own doing
$session = [$GLOBALS['session_name'] ?? null, $GLOBALS['session_user_id'] ?? null];
$GLOBALS['session_name'] = escapeSql($agent_name);
$GLOBALS['session_user_id'] = $user_id;
if (!intval($row['ticket_assigned_to'] ?? 0) && ticketReplyAssigns()) {
ticketChangeAssignee($ticket_id, $user_id);
} elseif (intval($ticket_status) === TICKET_STATUS_NEW) {
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = " . TICKET_STATUS_OPEN . " WHERE ticket_id = $ticket_id AND ticket_status = " . TICKET_STATUS_NEW);
syncTicketSlaClock($ticket_id);
logTicketHistory($ticket_id, escapeSql("$agent_name set the status to " . getTicketStatusName(TICKET_STATUS_OPEN)));
}
[$GLOBALS['session_name'], $GLOBALS['session_user_id']] = $session;
triggerCustomAction('reply_reply_agent_public', $ticket_id);
}
/** ------------------------------------------------------------------
* OAuth helpers + provider guard
* ------------------------------------------------------------------ */
@@ -861,16 +933,15 @@ foreach ($messages as $message) {
try {
$email_processed = false;
// From
// From - with no address, '': a stranger's, which nobody is answered at. It was itflow-guest@example.com, made the
// ticket's CC and answered, each email failing in the mail queue
$from_addr = $message->from(); // ?Address
$from_email = escapeSql($from_addr?->email() ?: 'itflow-guest@example.com');
$from_raw = trim((string) ($from_addr?->email() ?? ''));
$from_raw = preg_match('/^[^@\s]+@[^@\s]+$/', $from_raw) ? $from_raw : '';
$from_email = escapeSql($from_raw);
$from_name = escapeSql($from_addr?->name() ?: 'Unknown');
$from_domain = explode("@", $from_email);
$from_domain = escapeSql(end($from_domain));
// Mail from an address that bounced before - it works again (functions/contact_bounce.php)
contactBounceClearEmail(stripslashes($from_email));
$from_domain = $from_raw !== '' ? strtolower(substr($from_raw, strrpos($from_raw, "@") + 1)) : '';
// Subject
// ticket_subject holds 500 characters - a longer one failed the insert and the email was tried again every run
@@ -904,6 +975,28 @@ foreach ($messages as $message) {
// 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);
// Mail from an address that bounced before - it works again (functions/contact_bounce.php). Not when it was forged: it
// was cleared before the check, so anyone could clear an address's mark by sending as it
if ($forged === '' && $from_raw !== '') {
contactBounceClearEmail($from_raw);
}
// Its Reply-To (ticketMailReplyTo()) - someone at the domain it is from that it was sent for, taken as from them; or a
// website form's visitor, From the site: the one answered and made the ticket's CC ($answer), never a contact by it
$via = '';
$answer = null;
$reply_addr = $message->replyTo();
if ($reply_to = ticketMailReplyTo($reply_addr?->email(), $reply_addr?->name(), $from_raw)) {
$via = $from_raw;
if ($reply_to['same_domain']) {
$from_raw = $reply_to['email'];
$from_email = escapeSql($from_raw);
$from_name = escapeSql($reply_to['name'] !== '' ? $reply_to['name'] : 'Unknown');
} else {
$answer = ['email' => escapeSql($reply_to['email']), 'name' => escapeSql($reply_to['name'] !== '' ? $reply_to['name'] : 'Unknown')];
}
}
// 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());
@@ -937,7 +1030,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, 'forged' => $forged];
'automatic' => $automatic, 'forged' => $forged, 'via' => $via];
// Date (string)
$dateObj = $message->date(); // ?CarbonInterface
@@ -1012,30 +1105,21 @@ foreach ($messages as $message) {
// 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
// (linkSimilarTicket()): two people with "Printer not working" in a week used to land on one ticket.
// 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;
}
$similar_ticket_id = 0;
if (!$email_processed && strlen(trim($subject)) > 10) {
$contact_id = 0;
$client_id = 0;
// First: check if sender is a registered contact
$contact_id = $contact_row && !$answer ? intval($contact_row['contact_id']) : 0;
$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);
$contact_sql = mysqli_query($mysqli, "SELECT * FROM contacts WHERE contact_email = '$from_email_esc' AND contact_archived_at IS NULL LIMIT 1");
$contact_row = mysqli_fetch_assoc($contact_sql);
if ($contact_row) {
$contact_id = intval($contact_row['contact_id']);
$client_id = intval($contact_row['contact_client_id']);
} else {
// Else: check if sender domain is registered
$from_domain_esc = mysqli_real_escape_string($mysqli, $from_domain);
$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");
$domain_row = mysqli_fetch_assoc($domain_sql);
if ($domain_row && $from_domain == $domain_row['domain_name']) {
$client_id = intval($domain_row['domain_client_id']);
}
}
// If we found either a contact or a domain, check recent tickets for a matching subject
if ($client_id) {
@@ -1054,8 +1138,8 @@ foreach ($messages as $message) {
if ($percent >= 95) {
$similar_id = intval($rowt['ticket_id']);
$theirs = ($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"));
$theirs = !$answer && (($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 {
@@ -1067,32 +1151,27 @@ foreach ($messages as $message) {
}
}
// 4. A known, registered contact?
if (!$email_processed) {
$from_email_esc = mysqli_real_escape_string($mysqli, $from_email);
$any_contact_sql = mysqli_query($mysqli, "SELECT * FROM contacts WHERE contact_email = '$from_email_esc' AND contact_archived_at IS NULL LIMIT 1");
$rowc = mysqli_fetch_assoc($any_contact_sql);
if ($rowc) {
$contact_name = escapeSql($rowc['contact_name']);
$contact_id = intval($rowc['contact_id']);
$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, $email);
// 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 && $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);
} 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]);
}
}
// 5. A known domain?
if (!$email_processed) {
$from_domain_esc = mysqli_real_escape_string($mysqli, $from_domain);
$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);
// 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']);
// 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 && $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);
} 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
$contact_name = mysqli_real_escape_string($mysqli, rtrim(mb_substr(stripslashes($from_name), 0, 200)));
@@ -1107,9 +1186,12 @@ foreach ($messages as $message) {
}
}
// 6. Unknown sender allowed? A ticket with no client - a mail server's bounce never gets here
// 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
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, $email);
$email_processed = $answer
? addTicket(0, $answer['name'], $answer['email'], 0, $date, $subject, $message_body, $attachments, $original_message_file, $email)
: 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