Mail review stage 1: people copied in on an email become a ticket's CCs only from the email that raises it or a reply to one of its emails, not a reply matched by the number in its subject alone; a subject too long for the mail queue is cut to fit and an email the queue turns away is logged rather than stopping the page or the mailbox; emailed files use the same list as attached ones, which gains heic, heif, tif, tiff, eml, xlsm, ppt, rtf and log, with an internal note naming any file left out; names with an apostrophe escaped once; 26.12.3 changelog

This commit is contained in:
johnnyq
2026-11-30 16:49:01 -05:00
parent c98ea0e686
commit a9e5bc66db
12 changed files with 149 additions and 45 deletions

View File

@@ -79,6 +79,10 @@ Update from Maintenance > Update as usual - this release has seven database chan
- After the ticket prefix was changed (Admin > Settings > Tickets), a reply to an older ticket - its number with the old prefix - became a new ticket. It finds its ticket now, and so does a number written with zeros in front.
- A reply to an email of a ticket that was merged into another goes on the ticket it was merged into, rather than being turned away as a reply to a closed ticket.
- Email from an address with "mta", "daemon" or "bounce" anywhere in it - jmtaylor@, say - was taken for a mail server: it raised no ticket from an unknown sender, and a reply to a closed ticket wasn't answered. Only a mail server's own address, or a bounce report, counts now.
- Ticket emails: an email matched to a ticket only by the number in its subject no longer makes the people on its To and Cc lines the ticket's CCs. Anyone can put a ticket's number in a subject, and a From line from one of the client's contacts is easily made up - that was enough to copy an outsider in on every reply after it. People copied in are still added from the email that raises a ticket, and from a reply to one of the ticket's emails.
- Ticket emails: a ticket with a subject over about 245 characters - an emailed-in monitoring alert, say - had none of its emails sent. An agent's reply was saved and the page then showed an error; an emailed-in one was made a ticket, but nobody was told and the email was left flagged in the mailbox. A subject too long for an email is cut to fit now, and an email that can't be queued is logged (App Logs, Mail) without stopping the page or the mailbox - the reply shows it wasn't emailed.
- Files with an email: they're taken by the same list as files you attach yourself, so a client's .pptx, .msg, .odt, .mov or .mp4 is added to the ticket rather than left out - and iPhone photos (.heic), scans (.tif), forwarded emails (.eml), .xlsm, .ppt, .rtf and .log files can now be attached anywhere tickets take files. A file that still isn't added - another kind, or over 15 MB - is named in an internal note on the ticket, where it used to be in the audit log alone.
- Email-to-ticket: a name with an apostrophe got a stray backslash - a contact made from an email at a client's domain was saved as O\'Brien, and the "Email from" line of an emailed ticket showed it the same way. A new contact's name is cut to fit, too, where a very long one left the email flagged in the mailbox.
### Developer Updates
@@ -128,6 +132,7 @@ Update from Maintenance > Update as usual - this release has seven database chan
- Watchers are CCs in every page, email, ticket history line, log and message, and in the code. Database 4.2.8 renames ticket_watchers to ticket_ccs and its watcher_id, watcher_name, watcher_email and watcher_ticket_id to ticket_cc_id, ticket_cc_name, ticket_cc_email and ticket_cc_ticket_id, with its index. ticketWatcherEmails(), ticketAddWatchers(), ticketMailWatchersAdded() and ticketMailCopiedWatchers() are ticketCcEmails(), ticketAddCcs(), ticketMailCcsAdded() and ticketMailCopiedCcs(); ticketMailSend()'s to_watchers and ticketMailCreated()'s $to_watchers are to_ccs; the watcher group, email kind and sender are cc; the posts add_ticket_watcher (watcher_email, watcher_notify), delete_ticket_watcher and New Ticket's watchers[] are add_ticket_cc (cc_email, cc_notify), delete_ticket_cc and ccs[]; agent/modals/ticket/ticket_add_watcher.php is ticket_add_cc.php; the email preview ticket_watcher is ticket_cc.
- The ticket email parser waits while a database update is pending, rather than flag email it can't take - a flagged email is left in the mailbox for good.
- 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.
## [26.12.2]

View File

@@ -430,7 +430,7 @@ ob_start();
<div class="mb-3">
<label class="form-label"><i class="fas fa-fw fa-paperclip me-1"></i>Attachments</label>
<input type="file" class="form-control" name="attachments[]" multiple accept=".jpg, .jpeg, .gif, .png, .webp, .pdf, .txt, .md, .doc, .docx, .odt, .csv, .xls, .xlsx, .ods, .pptx, .odp, .zip, .tar, .gz, .xml, .msg, .json, .wav, .mp3, .ogg, .mov, .mp4, .av1, .ovpn">
<input type="file" class="form-control" name="attachments[]" multiple accept="<?= escapeHtml(ticketAttachmentAccept()) ?>">
</div>
</div>

View File

@@ -984,7 +984,7 @@ if (isset($_GET['ticket_id'])) {
<div class="mb-3">
<label class="form-label small text-secondary mb-1" for="ticket_reply_attachments"><i class="fas fa-fw fa-paperclip me-1"></i>Attach files</label>
<input type="file" class="form-control form-control-sm" id="ticket_reply_attachments" name="attachments[]" multiple accept=".jpg, .jpeg, .gif, .png, .webp, .pdf, .txt, .md, .doc, .docx, .odt, .csv, .xls, .xlsx, .ods, .pptx, .odp, .zip, .tar, .gz, .xml, .msg, .json, .wav, .mp3, .ogg, .mov, .mp4, .av1, .ovpn">
<input type="file" class="form-control form-control-sm" id="ticket_reply_attachments" name="attachments[]" multiple accept="<?= escapeHtml(ticketAttachmentAccept()) ?>">
</div>
<!-- The time, and when it was worked - filled from the timer until typed (agent/js/ticket_work_times.js) -->

View File

@@ -219,7 +219,7 @@ $reply_by = portalTicketReplyBy($ticket_id);
<div class="invalid-feedback d-block" data-editor-required-note hidden>Write a reply first.</div>
</div>
<div class="itflow-portal-drop mb-3" data-portal-drop>
<input type="file" name="file[]" multiple id="replyFiles" accept=".jpg, .jpeg, .gif, .png, .webp, .pdf, .txt, .md, .doc, .docx, .odt, .csv, .xls, .xlsx, .ods, .pptx, .odp, .zip, .tar, .gz, .xml, .msg, .json, .wav, .mp3, .ogg, .mov, .mp4, .av1, .ovpn">
<input type="file" name="file[]" multiple id="replyFiles" accept="<?= escapeHtml(ticketAttachmentAccept()) ?>">
<i class="fas fa-paperclip me-2"></i>Drop files here, or <label for="replyFiles">choose files</label> - screenshots, documents, error logs
<div class="itflow-portal-files" data-portal-files hidden></div>
</div>

View File

@@ -160,7 +160,7 @@ $reply_times = portalReplyTimes();
<div class="mb-4">
<label class="form-label">Files</label>
<div class="itflow-portal-drop" data-portal-drop>
<input type="file" name="file[]" multiple id="ticketFiles" accept=".jpg, .jpeg, .gif, .png, .webp, .pdf, .txt, .md, .doc, .docx, .odt, .csv, .xls, .xlsx, .ods, .pptx, .odp, .zip, .tar, .gz, .xml, .msg, .json, .wav, .mp3, .ogg, .mov, .mp4, .av1, .ovpn">
<input type="file" name="file[]" multiple id="ticketFiles" accept="<?= escapeHtml(ticketAttachmentAccept()) ?>">
<i class="fas fa-paperclip me-2"></i>Drop files here, or <label for="ticketFiles">choose files</label>
<div class="itflow-portal-files" data-portal-files hidden></div>
</div>
@@ -255,7 +255,7 @@ $reply_times = portalReplyTimes();
<div class="mb-4">
<label class="form-label">Screenshots or files</label>
<div class="itflow-portal-drop" data-portal-drop>
<input type="file" name="file[]" multiple id="ticketFiles" accept=".jpg, .jpeg, .gif, .png, .webp, .pdf, .txt, .md, .doc, .docx, .odt, .csv, .xls, .xlsx, .ods, .pptx, .odp, .zip, .tar, .gz, .xml, .msg, .json, .wav, .mp3, .ogg, .mov, .mp4, .av1, .ovpn">
<input type="file" name="file[]" multiple id="ticketFiles" accept="<?= escapeHtml(ticketAttachmentAccept()) ?>">
<i class="fas fa-paperclip me-2"></i>Drop files here, or <label for="ticketFiles">choose files</label>
<div class="itflow-portal-files" data-portal-files hidden></div>
</div>

View File

@@ -70,12 +70,13 @@ if (version_compare(LATEST_DATABASE_VERSION, CURRENT_DATABASE_VERSION, '>')) {
// recover from a killed run and could only end itself with exit() - fatal to a dispatched
// job. flock covers the same ground and the kernel drops it however the process ends.
// Allowed attachment extensions
$allowed_extensions = array('jpg', 'jpeg', 'gif', 'png', 'webp', 'pdf', 'txt', 'md', 'doc', 'docx', 'csv', 'xls', 'xlsx', 'xlsm', 'zip', 'tar', 'gz');
// The files an email may add to a ticket - the kinds an agent may attach (TICKET_ATTACHMENT_EXTENSIONS, functions/files.php).
// This kept a list of its own, narrower than that one, and a client's .pptx, .msg or video was left out with only an audit line
$allowed_extensions = TICKET_ATTACHMENT_EXTENSIONS;
// Processing limits
$max_emails_per_run = 50; // Cap per cron run to bound memory usage (cron catches up on the next run)
$max_attachment_bytes = 15728640; // 15 MB - larger attachments are skipped & logged
$max_attachment_bytes = 15728640; // 15 MB - larger attachments are skipped, logged and named on the ticket (ticketMailFilesLeftOut())
$max_inline_embed_bytes = 2097152; // 2 MB - larger inline images are saved as regular attachments instead of shown in the ticket's text (kept as its files - functions/ticket_pictures.php)
$read_mail_minutes = 60; // Mail someone opened before this got to it is taken when it came in this long ago at most
@@ -89,10 +90,15 @@ $read_mail_minutes = 60; // Mail someone opened before this got to it
* $automatic 'automated' for one sent by a system or a list, which isn't answered (functions/ticket_mail_auto.php).
* 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 = '') {
function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message, $attachments, $original_message_file, $copied, $message_id = '', $automatic = '', array $dropped = []) {
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
// Who it is from, as written - $contact_name and $contact_email come SQL-escaped, and each use escapes them once
// 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);
// Atomically increment and get the new ticket number
mysqli_query($mysqli, "
UPDATE settings
@@ -116,11 +122,11 @@ 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>$contact_name</b> &lt;$contact_email&gt; at $date:-</i> <br><br><div style='line-height:1.5;'>$message</div>";
$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>";
$ticket_prefix_esc = mysqli_real_escape_string($mysqli, $config_ticket_prefix);
$message_esc = mysqli_real_escape_string($mysqli, $message);
$contact_email_esc = mysqli_real_escape_string($mysqli, $contact_email);
$contact_email_esc = mysqli_real_escape_string($mysqli, $contact_email_raw);
$client_id = intval($client_id);
$url_key = randomString(32);
@@ -167,9 +173,13 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
} else {
$ticket_attachment_name_esc = mysqli_real_escape_string($mysqli, $att_name);
logAudit("Ticket", "Edit", "Email parser: Blocked attachment $ticket_attachment_name_esc from Client contact $contact_email_esc for ticket $ticket_prefix_esc$ticket_number", $client_id, $id);
$dropped[] = ['name' => $att_name, 'why' => ticketMailFileTypeWhy($att_extension)];
}
}
// 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)) {
mysqli_query($mysqli, "INSERT INTO ticket_ccs SET ticket_cc_email = '$contact_email_esc', ticket_cc_ticket_id = $id");
@@ -266,7 +276,7 @@ function linkSimilarTicket($new_id, $similar_id) {
* An email on ticket $ticket_id. $email, about the email:
* message_id its Message-ID, kept with the ticket (functions/ticket_mail_thread.php)
* from_name the name in its From line
* copied the people it was also to or copied to, made CCs when it is from the client or a CC
* copied the people it was also to or copied to, made CCs when it is from the client or a CC and threaded
* had everyone it was from, to and copied to - a client's reply goes on to the rest (ticketMailRelayClientReply())
* threaded it answers one of the ticket's emails (its In-Reply-To or References) - only then is a client's
* reply passed on, as anyone can put a ticket number in a subject
@@ -299,7 +309,7 @@ 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;' . $from_email . '&gt;' : $from_email;
$from_line = $from_name !== '' ? '<b>' . escapeHtml($from_name) . '</b> &lt;' . escapeHtml(stripslashes($from_email)) . '&gt;' : escapeHtml(stripslashes($from_email));
$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);
@@ -395,6 +405,7 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
} else {
$ticket_attachment_name_esc = mysqli_real_escape_string($mysqli, $att_name);
logAudit("Ticket", "Edit", "Email parser: Blocked attachment $ticket_attachment_name_esc from $from_email_esc for ticket $ticket_reference_esc", $client_id, $ticket_id);
$email['dropped'][] = ['name' => $att_name, 'why' => ticketMailFileTypeWhy($att_extension)];
}
}
@@ -406,6 +417,11 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
mysqli_query($mysqli, "INSERT INTO ticket_attachments SET ticket_attachment_name = 'Original-parsed-email.eml', ticket_attachment_reference_name = '$original_message_file_esc', ticket_attachment_reply_id = $reply_id, ticket_attachment_ticket_id = $ticket_id");
}
// The files that weren't added, said on the ticket - a stranger's and an automatic reply's are all left out, as their note says
if ($take_files) {
ticketMailFilesLeftOut($ticket_id, (array) ($email['dropped'] ?? []));
}
// An automatic reply - an out-of-office - is there to read, and that is all
if ($automatic === 'auto-reply') {
logTicketHistory($ticket_id, "$from_email_esc sent an automatic reply - kept as a note");
@@ -431,8 +447,10 @@ function addReply($from_email, $date, $subject, $ticket_id, $message, $attachmen
}
// The people the client or a CC also wrote to, or copied in, follow the ticket from now on - as
// they would have in a conversation by email. Not a stranger's: anyone can put a number in a subject
if (!$stranger && ($added_ccs = ticketMailCopiedCcs($ticket_id, (array) ($email['copied'] ?? []), stripslashes($from_email)))) {
// they would have in a conversation by email. Only from an email that answers one of the ticket's own
// (threaded): anyone can put a number in a subject, and a From line from one of the client's contacts
// is easily made up - that alone copied an outsider in on every reply after it. Never a stranger's
if (!$stranger && !empty($email['threaded']) && ($added_ccs = ticketMailCopiedCcs($ticket_id, (array) ($email['copied'] ?? []), stripslashes($from_email)))) {
logTicketHistory($ticket_id, escapeSql(implode(', ', $added_ccs)) . " added as " . (count($added_ccs) === 1 ? 'a CC' : 'CCs') . " - copied in on an email from $from_email_esc");
}
@@ -898,8 +916,9 @@ foreach ($messages as $message) {
$message_body = nl2br(htmlspecialchars($message->body()));
}
// Handle attachments (inline vs regular)
// Handle attachments (inline vs regular) - the ones too big to take named on the ticket ($dropped)
$attachments = [];
$dropped = [];
foreach ($message->attachments() as $att) {
$dispo = strtolower((string)$att->contentDisposition());
$cid = $att->contentId(); // Content-ID (without <>)
@@ -911,6 +930,7 @@ foreach ($messages as $message) {
// Skip oversized attachments entirely
if ($size > $max_attachment_bytes) {
logApp("Cron-Email-Parser", "warning", "Email parser skipped oversized attachment " . escapeSql($name) . " (" . round($size / 1048576, 1) . " MB) from $from_email ($subject)");
$dropped[] = ['name' => $name, 'why' => round($size / 1048576, 1) . ' MB - over the ' . round($max_attachment_bytes / 1048576) . ' MB an emailed file may be'];
continue;
}
@@ -929,6 +949,8 @@ foreach ($messages as $message) {
}
}
$email['dropped'] = $dropped;
// 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 = ticketMailHeaderTicket($in_reply_to, $references);
@@ -1017,7 +1039,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);
$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);
}
}
@@ -1027,25 +1049,26 @@ 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']) {
if ($rowd && $from_domain == $rowd['domain_name'] && mb_strlen(stripslashes($from_email)) <= 200) {
$client_id = intval($rowd['domain_client_id']);
// Create a new contact
$contact_name = $from_name;
// 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)));
$contact_email = $from_email;
mysqli_query($mysqli, "INSERT INTO contacts SET contact_name = '".mysqli_real_escape_string($mysqli, $contact_name)."', contact_email = '".mysqli_real_escape_string($mysqli, $contact_email)."', contact_notes = 'Added automatically via email parsing.', contact_client_id = $client_id");
mysqli_query($mysqli, "INSERT INTO contacts SET contact_name = '$contact_name', contact_email = '$contact_email', contact_notes = 'Added automatically via email parsing.', contact_client_id = $client_id");
$contact_id = mysqli_insert_id($mysqli);
logAudit("Contact", "Create", "Email parser: created contact " . mysqli_real_escape_string($mysqli, $contact_name), $client_id, $contact_id);
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);
$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);
}
}
// 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);
$email_processed = addTicket(0, $from_name, $from_email, 0, $date, $subject, $message_body, $attachments, $original_message_file, $copied, $email_message_id, $automatic, $dropped);
}
// A new ticket whose subject is almost that of another's at the client, from someone else - linked as related

View File

@@ -803,10 +803,16 @@ function updateChangelogHtml(string $markdown): string
return $html;
}
/*
* Put emails in the queue (email_queue) - $data, each value SQL-escaped already. How many went in: one that
* can't - too long for a column under strict SQL mode, the database gone for a moment - is logged (App Logs,
* Mail) and left out, rather than ending the page or the job queuing it after the work it is about was saved.
*/
function addToMailQueue($data) {
global $mysqli;
$queued = 0;
foreach ($data as $email) {
$from = (string) $email['from'];
$from_name = (string) $email['from_name'];
@@ -861,10 +867,26 @@ function addToMailQueue($data) {
. (!empty($email['headers']) ? ", email_headers = '" . mysqli_real_escape_string($mysqli, json_encode((array) $email['headers'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) . "'" : '');
}
mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$recipient', email_recipient_name = '$recipient_name', email_from = '$from', email_from_name = '$from_name', email_subject = '$subject', email_content = '$body', email_queued_at = $queued_at, email_cal_str = '$cal_str', email_attachments = '$attachments'$ticket_reply_sql$sensitive_sql$thread_sql");
$error = '';
try {
$inserted = mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$recipient', email_recipient_name = '$recipient_name', email_from = '$from', email_from_name = '$from_name', email_subject = '$subject', email_content = '$body', email_queued_at = $queued_at, email_cal_str = '$cal_str', email_attachments = '$attachments'$ticket_reply_sql$sensitive_sql$thread_sql");
$error = $inserted ? '' : mysqli_error($mysqli);
} catch (\mysqli_sql_exception $e) {
$inserted = false;
$error = $e->getMessage();
}
if ($inserted) {
$queued++;
continue;
}
try {
logApp("Mail", "error", "An email to " . stripslashes($recipient) . " couldn't be queued, so it wasn't sent - " . ($error !== '' ? $error : 'the database turned it away') . ". Subject: " . stripslashes($subject));
} catch (\Throwable $e) {
error_log("ITFlow: an email to " . stripslashes($recipient) . " couldn't be queued - $error");
}
}
return true;
return $queued;
}
function getTicketCalendarUid($ticket_id) {

View File

@@ -553,7 +553,8 @@ function emailInlineImages($html, array $ticket = []) {
* and answering, the Message-ID of the email it answers, when it answers one
* auto replied (an automatic answer to an email) or generated (sent by ITFlow by
* itself) - headers saying so, so it isn't answered (ticketMailAutoHeaders())
* Who it was queued to, "Name (email)" - the copied in too. Nothing is queued without mail set up.
* Who it was queued to, "Name (email)" - the copied in too: only those that went in the queue
* (addToMailQueue()). Nothing is queued without mail set up. The subject is cut to fit (emailFit()).
*/
function emailQueue(array $recipients, $subject, array $email, array $opts = []) {
global $mysqli;
@@ -561,6 +562,8 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = [])
return [];
}
$from = $opts['from'] ?? emailFrom();
// To fit the queue's columns (emailFit())
$subject = emailFit($subject);
$queued_at = !empty($opts['queued_at']) && strtotime((string) $opts['queued_at']) ? date('Y-m-d H:i:s', strtotime((string) $opts['queued_at'])) : '';
$attachments = array_values(array_filter((array) ($opts['attachments'] ?? [])));
// The ticket whose pictures its message may carry (functions/ticket_pictures.php)
@@ -582,7 +585,6 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = [])
$thread = $thread_id && ticketMailThreadReady() ? ticketMailThread($thread_id, (string) ($opts['answering'] ?? '')) : null;
$auto_headers = !empty($opts['auto']) ? ticketMailAutoHeaders((string) $opts['auto']) : [];
$data = [];
$sent = [];
foreach ($together ? [$people[0]] : $people as $recipient) {
$address = $recipient['email'];
@@ -592,8 +594,9 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = [])
$html = emailHtml(array_merge($email, [
'name' => array_key_exists('greet', $recipient) ? $recipient['greet'] : $name,
], $footnote !== null ? ['footnote' => $footnote] : []));
$went = [];
foreach (array_merge([$recipient], $cc) as $person) {
$sent[strtolower($person['email'])] = $person['name'] !== '' ? $person['name'] . ' (' . $person['email'] . ')' : $person['email'];
$went[strtolower($person['email'])] = $person['name'] !== '' ? $person['name'] . ' (' . $person['email'] . ')' : $person['email'];
}
$cc_list = array_map(function ($person) {
return ['email' => $person['email'], 'name' => $person['name']];
@@ -601,15 +604,16 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = [])
if (emailCapturing()) {
$GLOBALS['itflow_email_capture'][] = ['to' => $address, 'to_name' => $name, 'cc' => $cc_list, 'from' => (string) $from['email'], 'from_name' => (string) $from['name'],
'subject' => (string) $subject, 'html' => $html, 'attachments' => $attachments, 'cal_str' => (string) ($opts['cal_str'] ?? '')];
$sent += $went;
continue;
}
// Its pictures attached inline, not carried as data
[$html, $images] = emailInlineImages($html, $pictures);
$data[] = [
$row = [
'from' => mysqli_real_escape_string($mysqli, (string) $from['email']),
'from_name' => mysqli_real_escape_string($mysqli, (string) $from['name']),
'from_name' => mysqli_real_escape_string($mysqli, emailFit($from['name'])),
'recipient' => mysqli_real_escape_string($mysqli, $address),
'recipient_name' => mysqli_real_escape_string($mysqli, $name !== '' ? $name : $address),
'recipient_name' => mysqli_real_escape_string($mysqli, emailFit($name !== '' ? $name : $address)),
'subject' => mysqli_real_escape_string($mysqli, (string) $subject),
'body' => mysqli_real_escape_string($mysqli, $html),
'attachments' => array_merge($attachments, $images),
@@ -619,13 +623,24 @@ function emailQueue(array $recipients, $subject, array $email, array $opts = [])
+ (!empty($opts['sensitive']) ? ['sensitive' => 1] : [])
+ ($cc_list ? ['cc' => $cc_list] : [])
+ ($thread !== null || $auto_headers ? ['headers' => ($thread !== null ? ticketMailHeaders($thread_id, (string) $from['email'], $thread) : []) + $auto_headers] : []);
}
if ($data) {
addToMailQueue($data);
// Each on its own, so who it went to is only those queued - one that couldn't be is logged (addToMailQueue())
if (addToMailQueue([$row]) === 1) {
$sent += $went;
}
}
return array_values($sent);
}
/*
* $text cut to the $length characters a column of the mail queue holds - its subject and names (255) -
* with an ellipsis. A ticket's subject can be 500 characters, and an email about one too long for
* email_subject failed to queue under strict SQL mode, MariaDB's default.
*/
function emailFit($text, $length = 255) {
$text = (string) $text;
return mb_strlen($text) > $length ? rtrim(mb_substr($text, 0, $length - 1)) . '…' : $text;
}
/** The test email in Admin > Mail, from one of the mailboxes - system, ticket, invoice or quote. Who it went to - [] when it didn't. */
function emailTestSend($to, $mailbox) {
$box = ['ticket' => emailFrom('ticket'), 'invoice' => billingEmailFrom('invoice'), 'quote' => billingEmailFrom('quote')][$mailbox] ?? emailFrom();

View File

@@ -601,15 +601,14 @@ function emailPreviewSendCopy($key, $to, $name) {
return false;
}
$from = $email['from'] !== '' ? ['email' => $email['from'], 'name' => $email['from_name']] : emailFrom();
addToMailQueue([[
return addToMailQueue([[
'from' => mysqli_real_escape_string($mysqli, $from['email']),
'from_name' => mysqli_real_escape_string($mysqli, $from['name']),
'from_name' => mysqli_real_escape_string($mysqli, emailFit($from['name'])),
'recipient' => mysqli_real_escape_string($mysqli, (string) $to),
'recipient_name' => mysqli_real_escape_string($mysqli, (string) $name),
'subject' => mysqli_real_escape_string($mysqli, '[Preview] ' . $email['subject']),
'recipient_name' => mysqli_real_escape_string($mysqli, emailFit($name)),
'subject' => mysqli_real_escape_string($mysqli, emailFit('[Preview] ' . $email['subject'])),
'body' => mysqli_real_escape_string($mysqli, $email['html']),
]]);
return true;
]]) === 1;
}
/*

View File

@@ -8,9 +8,19 @@
const TICKET_ATTACHMENT_EXTENSIONS = [
'jpg', 'jpeg', 'gif', 'png', 'webp', 'pdf', 'txt', 'md', 'doc', 'docx',
'odt', 'csv', 'xls', 'xlsx', 'ods', 'pptx', 'odp', 'zip', 'tar', 'gz',
'xml', 'msg', 'json', 'wav', 'mp3', 'ogg', 'mov', 'mp4', 'av1', 'ovpn'
'xml', 'msg', 'json', 'wav', 'mp3', 'ogg', 'mov', 'mp4', 'av1', 'ovpn',
// Phone photos, scans, forwarded emails, macro workbooks, older slides, rich text and logs - the
// email parser took .xlsm on a list of its own, and now takes this one (cron/ticket_email_parser.php)
'heic', 'heif', 'tif', 'tiff', 'eml', 'xlsm', 'ppt', 'rtf', 'log'
];
/** A file input's accept list for ticket files - ".jpg, .jpeg, ..." from TICKET_ATTACHMENT_EXTENSIONS, raw. */
function ticketAttachmentAccept() {
return implode(', ', array_map(function ($extension) {
return ".$extension";
}, TICKET_ATTACHMENT_EXTENSIONS));
}
function removeDirectory($path) {
if (!file_exists($path)) {
return;

View File

@@ -251,6 +251,38 @@ function ticketMailCopiedCcs($ticket_id, array $addresses, $sender) {
return ticketAddCcs($ticket_id, array_slice($emails, 0, 20));
}
/** 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));
return $extension === '' ? 'its name has no file type' : "ITFlow doesn't take .$extension files";
}
/*
* Say on a ticket which files with an email weren't added to it - $dropped [['name', 'why'], ...], a kind
* ITFlow doesn't take (ticketMailFileTypeWhy()) or too big - in an internal note, as they used to be in
* the audit log alone. They are still in the email as it came, kept with the ticket. The note's id, or 0.
*/
function ticketMailFilesLeftOut($ticket_id, array $dropped) {
global $mysqli;
$ticket_id = intval($ticket_id);
$items = [];
foreach ($dropped as $file) {
$name = trim((string) ($file['name'] ?? ''));
$items[] = '<li>' . escapeHtml($name !== '' ? $name : 'A file with no name') . ' - ' . escapeHtml((string) ($file['why'] ?? '')) . '</li>';
}
if (!$ticket_id || !$items) {
return 0;
}
$count = count($items);
$note = '<b>' . ($count === 1 ? 'A file with the email was' : "$count files with the email were") . ' not added to the ticket</b><ul>' . implode('', $items) . '</ul>'
. "<i>They are in the email as it came - Original-parsed-email.eml on this ticket - and in the mailbox's ITFlow folder.</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, $count === 1 ? "A file with an email wasn't added - noted" : "$count files with an email weren't added - noted");
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

View File

@@ -143,9 +143,7 @@ $files = !empty($r['canned_response_id']) ? cannedResponseFiles($r['canned_respo
<label class="form-label" for="cannedResponseFiles">Add files</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-paperclip"></i></span>
<input type="file" class="form-control" id="cannedResponseFiles" name="files[]" multiple accept="<?= escapeHtml(implode(', ', array_map(function ($extension) {
return ".$extension";
}, TICKET_ATTACHMENT_EXTENSIONS))) ?>">
<input type="file" class="form-control" id="cannedResponseFiles" name="files[]" multiple accept="<?= escapeHtml(ticketAttachmentAccept()) ?>">
</div>
<small class="text-secondary">The kinds a ticket takes - PDFs, documents, spreadsheets, images, zips.</small>
</div>