From 17d2611b6049f304d55fc6a2fc06df5aaa58ee20 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Mon, 30 Nov 2026 16:50:51 -0500 Subject: [PATCH] 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 --- CHANGELOG.md | 3 ++ cron/ticket_email_parser.php | 79 ++++++++++++++++++++++++-------- functions/ticket.php | 11 +++-- functions/ticket_mail_auto.php | 44 ++++++++++++++++++ functions/ticket_mail_thread.php | 21 +++++++++ 5 files changed, 135 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8821ff02f..211ec0311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,8 @@ Update from Maintenance > Update as usual - this release has eight database chan - A reply to a closed ticket is kept on it as an internal note instead of left in the mailbox, and the notice says so. The ticket isn't reopened. The client or CC is told it is closed, as before - but not someone with no part in the ticket, who learned nothing from a number they guessed. - Mail queue: email goes out at a pace your mail host takes - 30 a minute to start, set under Pace in Admin > Settings > Mail, Sending - over one connection, ticket emails first and email from the contacts or clients list last. A host's limit doesn't lose email any more: the rest wait their turn. A failed email is tried again over about eight hours, sooner at first. An address the mail server turns away for good isn't tried again, and its contact shows Email bounced. A mail server ITFlow can't reach or sign in to costs no email a try - it waits, and goes once it can. Admins hear about the queue once an hour at most, where every agent got a notice for each failed email. - Every email has a plain text part beside its HTML, and names the machine sending it by ITFlow's own address rather than the server's own name ("vm", say) - both counted against an email with spam filters. +- Email-to-ticket checks who an email is really from, by what your mail server found as it arrived (SPF, DKIM and DMARC). An email that failed - DMARC for a domain that asks for forged email to be rejected or quarantined, or SPF with nothing else passing - is treated as from a stranger, whoever it says it's from: a reply is an internal note saying which check failed and reopens nothing, adds nobody and goes to nobody; a new email still makes a ticket, with a note, but copies nobody in and gets no answer; and it never makes a contact. An email that says it's from one of your agents isn't taken as their note. A domain that asks nothing of forged email (DMARC p=none) is taken as before. +- Email-to-ticket: someone who isn't a contact is answered "[TCK-123] We have your email", without their own subject. Anyone can send an email that says it's from someone else, and the answer carried their words to that person from your mailbox. ### Bug Fixes @@ -137,6 +139,7 @@ Update from Maintenance > Update as usual - this release has eight database chan - API ticket CCs: api/v1/ticket_ccs/ has read.php (ticket_id or ticket_cc_id), create.php (ticket_cc_email - one address, or several as a list or separated by commas - and ticket_cc_notify) and delete.php (ticket_cc_id, or ticket_id and ticket_cc_email), each finding the ticket through ticket_cc_find.php, and is mapped to module_support in enforce_api_rbac.php. Its delete.php needs Modify, as the ticket page does, through the new $resource_operation_level, which sets one resource's file apart from $operation_level. A closed ticket's CCs aren't changed. tickets/create.php takes ticket_ccs and answers with those added, and tickets/read.php attaches each ticket's ticket_ccs (ticketCcsApiAttach(), when $api_ticket_ccs is set for read_output.php). ticketCcEmailsValid() is ticketCcEmails() naming the first entry that isn't an email address, for the API to refuse. - Mail review stage 1: emailQueue() cuts the subject and the recipient's and sender's names to the queue's 255 characters (emailFit(), functions/email.php) and queues each email on its own; addToMailQueue() (functions/app.php) catches an insert the database turns away, logs it to App Logs (Mail) and returns how many went in, so emailQueue() answers with only those queued and a ticket reply's Emailed state is right - the email preview's send too. cron/ticket_email_parser.php takes TICKET_ATTACHMENT_EXTENSIONS (functions/files.php) in place of its own list; the list gains heic, heif, tif, tiff, eml, xlsm, ppt, rtf and log, and ticketAttachmentAccept() gives every file field's accept from it (the reply box, New Ticket, the portal's two, canned responses). Files not added - a kind it doesn't take (ticketMailFileTypeWhy()) or over $max_attachment_bytes - go in an internal note (ticketMailFilesLeftOut(), functions/ticket_mail_thread.php) through addTicket()'s new $dropped and addReply()'s email dropped. addReply() makes the copied-in CCs (ticketMailCopiedCcs()) only when the email is threaded. addTicket() takes its SQL-escaped name and address back to raw and escapes each once for HTML and SQL; the domain-match contact is inserted as escaped once, its name cut to 200 characters. - Mail review stage 2: cron/mail_queue.php sends what is due - waiting, and failed ones whose wait is over - in one query, at most mailQueuePerMinute() a run (settings.config_mail_send_per_minute, database 4.2.9), ORDER BY email_bulk (4.2.9, emailQueue()'s bulk, set by emailBulkSend()), over one PHPMailer kept open (SMTPKeepAlive, mailQueueMailer()). MailQueueSmtp keeps each refused recipient's code: everyone refused for good (5xx) - email_attempts 99 and contactBounceMark(); some refused - sent, and they're named in email_error; refused for now (4xx) - tried again. A connection or sign-in failure (MailQueueUnreachable) puts the email back as it was and ends the run with cronJobStop(..., 1); two failures in a row end it for the minute. MAIL_QUEUE_RETRY_MINUTES gives way to MAIL_QUEUE_RETRY_AFTER - 5, 10, 20, 40, 60, 120 and 240 minutes (mailQueueRetryMinutes(), mailQueueRetryDueSql()) - and MAIL_QUEUE_TRIES is 8. Each failed email's appNotify() to every agent gives way to one mailQueueNotifyAdmins() for the run, an hour apart at most. PHPMailer's Hostname is mailQueueHostname(), its AltBody mailQueueTextPart(). sendQueueEmail() takes the run's mailer and answers with who the mail server took and turned away. +- Mail review stage 3: ticketMailSenderCheck() (functions/ticket_mail_auto.php) reads the topmost Authentication-Results - the receiving server's - and answers why it failed, or '': dmarc=fail with p= or policy= reject or quarantine, or Microsoft 365's action reject, oreject or quarantine; or spf=fail with no dkim=pass and no dmarc result; never with arc=pass or compauth=pass. Only a failure is acted on, so a pass a sender writes in gains nothing. cron/ticket_email_parser.php keeps it as the email's forged: addReply() makes a forged email a stranger's, with its own note, history and audit line; addTicket() takes $email (copied, message_id, automatic, dropped, forged) in place of its last four arguments, writes ticketMailForgedNote() (functions/ticket_mail_thread.php) and leaves out the copied-in CCs, the client-less ticket's CC and the answer for a forged one; the domain match makes no contact from one. ticketMailCreated() takes a subject, as ticketMailSend()'s subject; a client-less emailed ticket is answered "[REF] We have your email". ## [26.12.2] diff --git a/cron/ticket_email_parser.php b/cron/ticket_email_parser.php index 766e10627..d5e0135b0 100644 --- a/cron/ticket_email_parser.php +++ b/cron/ticket_email_parser.php @@ -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 diff --git a/functions/ticket.php b/functions/ticket.php index 067d4f393..463c3f5a8 100644 --- a/functions/ticket.php +++ b/functions/ticket.php @@ -737,6 +737,7 @@ function ticketEmailPreview(array $email) { * (emailQueue()), for ticketReplyEmailed() * answering the Message-ID of the email it answers, when it answers one * auto replied or generated - one of ITFlow's own automatic emails (emailQueue()'s auto) + * subject in place of the ticket's own (ticketEmailSubject()) - with its number, so a reply finds it */ function ticketMailSend($ticket_id, array $email, array $opts = []) { global $mysqli; @@ -786,7 +787,7 @@ function ticketMailSend($ticket_id, array $email, array $opts = []) { return (($b['group'] ?? '') === 'contact') <=> (($a['group'] ?? '') === 'contact'); }); $to_contact = ($recipients[0]['group'] ?? '') === 'contact'; - return count(emailQueue($recipients, ticketEmailSubject($row), $email, ['from' => ticketEmailFrom(), 'attachments' => $opts['attachments'] ?? [], 'cal_str' => $opts['cal_str'] ?? '', + return count(emailQueue($recipients, (string) ($opts['subject'] ?? ticketEmailSubject($row)), $email, ['from' => ticketEmailFrom(), 'attachments' => $opts['attachments'] ?? [], 'cal_str' => $opts['cal_str'] ?? '', 'ticket_reply_id' => intval($opts['ticket_reply_id'] ?? 0), 'ticket_id' => $ticket_id, 'together' => ticketMailSettings()['copy'], 'together_footnote' => $to_contact @@ -876,14 +877,16 @@ function ticketMailColleaguesNew($ticket_id) { * unless $to_ccs is false; or to $to (ticketMailSend()'s) in their * place. $how agent, email or scheduled (ticketEmailContent()). */ -function ticketMailCreated($ticket_id, $attachments = [], $to_ccs = true, $how = 'agent', ?array $to = null) { +function ticketMailCreated($ticket_id, $attachments = [], $to_ccs = true, $how = 'agent', ?array $to = null, ?string $subject = null) { $row = ticketRow($ticket_id); if (!$row) { return 0; } - // Emailed in, it is an automatic answer to that email - which says so, so the other side doesn't answer it (functions/ticket_mail_auto.php) + // Emailed in, it is an automatic answer to that email - which says so, so the other side doesn't answer it (functions/ticket_mail_auto.php). + // $subject in place of the ticket's - an emailed ticket from someone who isn't a contact is answered without their own words return ticketMailSend($row['ticket_id'], ticketEmailContent($row, 'created', ['how' => $how]), - ['attachments' => $attachments, 'to_ccs' => $to_ccs, 'kind' => 'created'] + ($to !== null ? ['to' => $to] : []) + ($how === 'email' ? ['auto' => 'replied'] : [])); + ['attachments' => $attachments, 'to_ccs' => $to_ccs, 'kind' => 'created'] + ($to !== null ? ['to' => $to] : []) + ($how === 'email' ? ['auto' => 'replied'] : []) + + ($subject !== null ? ['subject' => $subject] : [])); } /* diff --git a/functions/ticket_mail_auto.php b/functions/ticket_mail_auto.php index a59d6e2b7..0a2338f71 100644 --- a/functions/ticket_mail_auto.php +++ b/functions/ticket_mail_auto.php @@ -44,6 +44,50 @@ function ticketMailHeaderMap($message) { return $map; } +/* + * Whether the mail server that took an email found it forged, by its Authentication-Results header - + * the topmost, which that server adds last: a sender's own is below it, and ITFlow only acts on a + * failure, so one saying "pass" gains them nothing. Failed is: + * dmarc=fail, from a domain that asks for its forged email to be rejected or quarantined (p=reject or + * quarantine, or Microsoft 365's action reject, oreject or quarantine) - one with p=none hasn't + * asked, and many such domains' own email fails too + * spf=fail (not softfail), with no DKIM signature passing and no DMARC result + * and never when the server vouched for it anyway - arc=pass, or Microsoft 365's compauth=pass. + * '' when it didn't fail - or no server checked - else why, in words ("DMARC failed for example.com, + * which asks for forged email to be rejected"). + */ +function ticketMailSenderCheck(array $headers) { + $results = strtolower(preg_replace('/\s+/', ' ', (string) ($headers['authentication-results'] ?? ''))); + if ($results === '') { + return ''; + } + $verdicts = []; + $dmarc_part = ''; + foreach (explode(';', $results) as $part) { + if (preg_match('/^\s*(dkim|spf|dmarc|arc|compauth)\s*=\s*([a-z]+)/', $part, $m)) { + $verdicts[$m[1]][] = $m[2]; + if ($m[1] === 'dmarc') { + $dmarc_part = $part; + } + } + } + $said = function ($method, $result) use ($verdicts) { + return in_array($result, $verdicts[$method] ?? [], true); + }; + if ($said('arc', 'pass') || $said('compauth', 'pass')) { + return ''; + } + $domain = preg_match('/header\.from=([a-z0-9.-]+)/', $results, $m) ? $m[1] : ''; + if ($said('dmarc', 'fail') && preg_match('/\b(p|policy)=(reject|quarantine)\b|\baction=(oreject|reject|quarantine|pct\.reject|pct\.quarantine)\b/', $dmarc_part, $policy)) { + $asks = ($policy[2] ?? '') !== '' ? $policy[2] : str_replace(['pct.', 'oreject'], ['', 'reject'], $policy[3]); + return 'DMARC failed' . ($domain !== '' ? " for $domain" : '') . ", which asks for forged email to be " . ($asks === 'quarantine' ? 'quarantined' : 'rejected'); + } + if ($said('spf', 'fail') && !$said('dkim', 'pass') && !isset($verdicts['dmarc'])) { + return 'SPF failed - it came from a server ' . ($domain !== '' ? $domain : 'its domain') . ' says doesn\'t send its email - and no DKIM signature passed'; + } + return ''; +} + /* * Whether an email is a mail server's report that an email couldn't be delivered: a delivery * status report (multipart/report; report-type=delivery-status), an X-Failed-Recipients header diff --git a/functions/ticket_mail_thread.php b/functions/ticket_mail_thread.php index 82a15252a..015680027 100644 --- a/functions/ticket_mail_thread.php +++ b/functions/ticket_mail_thread.php @@ -283,6 +283,27 @@ function ticketMailFilesLeftOut($ticket_id, array $dropped) { return $note_id; } +/* + * Say on a ticket made from an email that the email failed its sender checks - $why (ticketMailSenderCheck()), + * $from the address it says it is from, $copied the people it copied in, who weren't added. An internal note, + * as the ticket is made all the same, so nothing is lost. The note's id. + */ +function ticketMailForgedNote($ticket_id, $why, $from, array $copied = []) { + global $mysqli; + $ticket_id = intval($ticket_id); + if (!$ticket_id) { + return 0; + } + $note = 'This email failed its sender checks - ' . escapeHtml($why) . '. It may not be from ' . escapeHtml($from) . '.
' + . "It was made a ticket so nothing is lost, but its sender wasn't answered" . ($copied ? ' and the people it copied in (' . escapeHtml(implode(', ', $copied)) . ') weren\'t added' : '') + . '. Check with the client before acting on it.'; + 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("The email it came from failed its sender checks - it may not be from $from")); + return $note_id; +} + /* * A client's reply - by email, text or in the portal - passed on to the people an agent's reply goes to * (the ticket's To line, ticketReplyRecipients()) who didn't have it: not whoever wrote it, nor