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

@@ -251,6 +251,95 @@ function ticketMailCopiedCcs($ticket_id, array $addresses, $sender) {
return ticketAddCcs($ticket_id, array_slice($emails, 0, 20));
}
/*
* The contact an email from $email is from, when it is one - ['contact' => row (contact_id, contact_name, contact_email,
* contact_client_id, client_name) or null, 'others' => the other clients it is a contact at, [client_id => name],
* 'why' => how the one was picked]. An address can be a contact at more than one client - someone who works for two,
* a shared mailbox - and its email went to whichever the database gave first. Now: the client whose domain it is
* ($domain, Domains), else the one they last raised a ticket at, else the first they were added to.
*/
function ticketMailSenderContact($email, $domain = '') {
global $mysqli;
$email = trim((string) $email);
$none = ['contact' => null, 'others' => [], 'why' => ''];
if ($email === '') {
return $none;
}
$email_sql = escapeSql($email);
$domain_sql = escapeSql(strtolower(trim((string) $domain)));
$rows = [];
$sql = mysqli_query($mysqli, "SELECT contact_id, contact_name, contact_email, contact_client_id, client_name,
(SELECT 1 FROM domains WHERE domain_client_id = contact_client_id AND domain_name = '$domain_sql' AND domain_archived_at IS NULL LIMIT 1) AS at_domain,
(SELECT MAX(ticket_created_at) FROM tickets WHERE ticket_contact_id = contact_id) AS last_ticket
FROM contacts LEFT JOIN clients ON client_id = contact_client_id
WHERE contact_email = '$email_sql' AND contact_archived_at IS NULL
ORDER BY at_domain DESC, last_ticket DESC, contact_id");
while ($row = mysqli_fetch_assoc($sql)) {
$rows[] = $row;
}
if (!$rows) {
return $none;
}
$contact = $rows[0];
$others = [];
foreach (array_slice($rows, 1) as $row) {
if (intval($row['contact_client_id']) !== intval($contact['contact_client_id'])) {
$others[intval($row['contact_client_id'])] = (string) ($row['client_name'] ?? 'a client');
}
}
$why = !empty($contact['at_domain']) ? 'whose domain it is' : (!empty($contact['last_ticket']) ? 'where they last raised a ticket' : 'where they were added first');
return ['contact' => $contact, 'others' => $others, 'why' => $others ? $why : ''];
}
/*
* Say on a ticket raised by an email from $email - a contact at more than one client ($picked, ticketMailSenderContact()) -
* which client it went to and why, and the others, so an agent moves it when it is for one of those. An internal note.
*/
function ticketMailSenderContactNote($ticket_id, $email, array $picked) {
global $mysqli;
$ticket_id = intval($ticket_id);
if (!$ticket_id || empty($picked['others']) || empty($picked['contact'])) {
return 0;
}
$client = (string) ($picked['contact']['client_name'] ?? 'a client');
$others = array_values($picked['others']);
$list = count($others) === 1 ? $others[0] : implode(', ', array_slice($others, 0, -1)) . ' and ' . end($others);
$note = '<b>' . escapeHtml($email) . ' is a contact at ' . escapeHtml($list) . ' too</b> - this ticket went to ' . escapeHtml($client)
. ', ' . escapeHtml($picked['why']) . '.<br><i>If it is for ' . escapeHtml(count($others) === 1 ? $others[0] : 'one of the others') . ', change its client.</i>';
mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = '" . mysqli_real_escape_string($mysqli, $note) . "', ticket_reply_type = 'Internal',
ticket_reply_time_worked = '00:00:00', ticket_reply_by = 0, ticket_reply_ticket_id = $ticket_id");
$note_id = intval(mysqli_insert_id($mysqli));
logTicketHistory($ticket_id, escapeSql("$email is a contact at more than one client - it went to $client, " . $picked['why'] . ' - noted'));
return $note_id;
}
/*
* Which of $addresses (an email's To and Cc) are the client's on ticket $ticket_id - its contact, one of its CCs, or
* another of the client's contacts - for an agent's email that went to them too. Lower-case, as given.
*/
function ticketMailClientAddresses($ticket_id, array $addresses) {
global $mysqli;
$ticket_id = intval($ticket_id);
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT ticket_client_id, contact_email FROM tickets LEFT JOIN contacts ON contact_id = ticket_contact_id WHERE ticket_id = $ticket_id"));
if (!$row) {
return [];
}
$client_id = intval($row['ticket_client_id']);
$found = [];
foreach (array_unique(array_map('strtolower', array_map('trim', array_map('strval', $addresses)))) as $email) {
if ($email === '' || emailOwnAddress($email)) {
continue;
}
$email_sql = escapeSql($email);
if (strtolower(trim((string) $row['contact_email'])) === $email
|| mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM ticket_ccs WHERE ticket_cc_ticket_id = $ticket_id AND ticket_cc_email = '$email_sql' LIMIT 1"))
|| ($client_id && mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM contacts WHERE contact_email = '$email_sql' AND contact_client_id = $client_id AND contact_archived_at IS NULL LIMIT 1")))) {
$found[] = $email;
}
}
return $found;
}
/** Why an emailed file with the extension $extension isn't added to a ticket - it isn't one of TICKET_ATTACHMENT_EXTENSIONS. */
function ticketMailFileTypeWhy($extension) {
$extension = strtolower(trim((string) $extension));