Unprocessed Email (agent/email_unprocessed.php, functions/mail_unprocessed.php): * Make a Ticket, Add to a Ticket, Try Again and Dismiss - one at a time or Bulk Action - Ignore Sender, and Download .eml. A ticket, a * reply or a try again is asked of the email parser, which takes the email from its copy within a minute. */ defined('FROM_POST_HANDLER') || die("Direct file access is not allowed"); /* The email to act on, still waiting - or a message and back to the list */ function unprocessedEmailForRequest($id) { $row = mailUnprocessedReady() ? mailUnprocessedRow($id) : null; if (!$row) { flashAlert("That email isn't there any more", 'error'); redirect('email_unprocessed.php'); } if ($row['email_unprocessed_done_at'] !== null) { flashAlert("That email was sorted already", 'error'); redirect('email_unprocessed.php'); } return $row; } /* Who it is from, for the messages and the audit log - escaped for HTML */ function unprocessedEmailFrom(array $row) { $from = trim((string) $row['email_unprocessed_from_email']); return escapeHtml($from !== '' ? $from : 'no sender address'); } // Make a Ticket - at the client picked, for the contact picked, or the sender made a contact there first if (isset($_POST['unprocessed_email_ticket'])) { validateCSRFToken(); enforceUserPermission('module_support', 2); $row = unprocessedEmailForRequest($_POST['email_unprocessed_id'] ?? 0); $client_id = intval($_POST['client_id'] ?? 0); $contact_id = intval($_POST['contact_id'] ?? 0); $add = !empty($_POST['make_contact']) && !$contact_id; if (!$client_id || !mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM clients WHERE client_id = $client_id AND client_archived_at IS NULL"))) { flashAlert("Pick the client the ticket is for", 'error'); redirect(); } enforceClientAccess($client_id); if ($contact_id && !mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM contacts WHERE contact_id = $contact_id AND contact_client_id = $client_id AND contact_archived_at IS NULL"))) { flashAlert("That contact isn't at the client picked", 'error'); redirect(); } if ($add && !filter_var((string) $row['email_unprocessed_from_email'], FILTER_VALIDATE_EMAIL)) { flashAlert("The sender can't be made a contact - the email has no address it can be answered at", 'error'); redirect(); } mailUnprocessedDecide([$row['email_unprocessed_id']], 'ticket', ['client_id' => $client_id, 'contact_id' => $contact_id, 'add' => $add], $session_user_id); $client_name = escapeSql((string) getFieldById('clients', $client_id, 'client_name')); logAudit("Email", "Unprocessed", "$session_name asked for a ticket at $client_name from the email from " . escapeSql((string) $row['email_unprocessed_from_email']), $client_id); flashAlert("A ticket at " . escapeHtml(stripslashes($client_name)) . " is being made from the email from " . unprocessedEmailFrom($row) . " - within a minute"); redirect(); } // Add to a Ticket - on the ticket picked, the sender made a CC on it if asked if (isset($_POST['unprocessed_email_reply'])) { validateCSRFToken(); enforceUserPermission('module_support', 2); $row = unprocessedEmailForRequest($_POST['email_unprocessed_id'] ?? 0); $ticket = ticketForRequest($_POST['ticket_id'] ?? 0, 2); $add = !empty($_POST['make_cc']); if ($add && !filter_var((string) $row['email_unprocessed_from_email'], FILTER_VALIDATE_EMAIL)) { flashAlert("The sender can't be made a CC - the email has no address it can be answered at", 'error'); redirect(); } mailUnprocessedDecide([$row['email_unprocessed_id']], 'reply', ['ticket_id' => intval($ticket['ticket_id']), 'add' => $add], $session_user_id); $reference = escapeSql(ticketReference($ticket)); logAudit("Email", "Unprocessed", "$session_name asked for the email from " . escapeSql((string) $row['email_unprocessed_from_email']) . " to be added to ticket $reference", intval($ticket['ticket_client_id']), intval($ticket['ticket_id'])); flashAlert("The email from " . unprocessedEmailFrom($row) . " is being added to " . escapeHtml(ticketReference($ticket)) . " - within a minute"); redirect(); } // Ignore Sender - its address, or everyone at its domain, set aside from now on (functions/mail_ignore.php); this one // dismissed, and the others waiting from them if asked. Never a contact's address, a client's domain, or one anyone can // have an address at - those are an admin's, in Admin > Ignored Senders if (isset($_POST['ignore_unprocessed_sender'])) { validateCSRFToken(); enforceUserPermission('module_support', 2); $row = unprocessedEmailForRequest($_POST['email_unprocessed_id'] ?? 0); $from = strtolower(trim((string) $row['email_unprocessed_from_email'])); if (!mailIgnoreReady() || !filter_var($from, FILTER_VALIDATE_EMAIL)) { flashAlert("That email has no sender address to ignore", 'error'); redirect(); } $domain = substr($from, strrpos($from, '@') + 1); $match = ($_POST['ignore'] ?? '') === 'domain' ? $domain : $from; $reach = mailIgnoreReach($match); if ($reach['contacts'] || $reach['clients'] || ($match === $domain && in_array($domain, EMAIL_PUBLIC_DOMAINS, true))) { flashAlert("" . escapeHtml($match) . " can't be ignored from here - " . ($reach['contacts'] || $reach['clients'] ? "it would set aside contacts' email" : "anyone can have an address there") . ". An admin can, in Admin > Ignored Senders", 'error'); redirect(); } mailIgnoreAdd(mailIgnoreClean($match), trim((string) ($_POST['ignored_sender_note'] ?? '')) !== '' ? $_POST['ignored_sender_note'] : 'From Unprocessed Email', $session_user_id); mailUnprocessedDecide([$row['email_unprocessed_id']], 'dismiss', [], $session_user_id); $others = !empty($_POST['dismiss_others']) ? mailIgnoreDismissWaiting($match, $session_user_id) : 0; logAudit("Email", "Unprocessed", "$session_name ignored email from " . escapeSql($match)); flashAlert("Email from " . escapeHtml($match === $domain ? "@$domain" : $from) . " is set aside from now on" . ($others ? " - and $others more waiting from them dismissed" : '')); redirect(); } // Try Again or Dismiss - one, from its menu if (isset($_GET['unprocessed_email'])) { validateCSRFToken(); enforceUserPermission('module_support', 2); $action = (string) $_GET['unprocessed_email']; if (!in_array($action, ['retry', 'dismiss'], true)) { redirect('email_unprocessed.php'); } $row = unprocessedEmailForRequest($_GET['id'] ?? 0); mailUnprocessedDecide([$row['email_unprocessed_id']], $action, [], $session_user_id); logAudit("Email", "Unprocessed", "$session_name " . ($action === 'retry' ? 'tried again' : 'dismissed') . " the email from " . escapeSql((string) $row['email_unprocessed_from_email'])); flashAlert($action === 'retry' ? "The email from " . unprocessedEmailFrom($row) . " is being tried again - within a minute" : "The email from " . unprocessedEmailFrom($row) . " was dismissed", $action === 'retry' ? 'success' : 'error'); redirect(); } // Bulk Action - Try Again or Dismiss the ones ticked if (isset($_POST['bulk_unprocessed_email'])) { validateCSRFToken(); enforceUserPermission('module_support', 2); $action = (string) $_POST['bulk_unprocessed_email']; $ids = array_map('intval', (array) ($_POST['email_unprocessed_ids'] ?? [])); if (!in_array($action, ['retry', 'dismiss'], true) || !$ids) { flashAlert("Tick the emails first", 'error'); redirect(); } $count = count(mailUnprocessedDecide($ids, $action, [], $session_user_id)); logAudit("Email", "Unprocessed", "$session_name " . ($action === 'retry' ? 'tried again' : 'dismissed') . " $count unprocessed email(s)"); flashAlert($action === 'retry' ? "$count email(s) are being tried again - within a minute" : "$count email(s) dismissed", $action === 'retry' ? 'success' : 'error'); redirect(); } // Download .eml - the email as it came if (isset($_GET['download_unprocessed_email'])) { enforceUserPermission('module_support'); $row = mailUnprocessedReady() ? mailUnprocessedRow($_GET['download_unprocessed_email']) : null; $path = $row ? mailUnprocessedPath($row['email_unprocessed_file']) : ''; if ($path === '' || !is_file($path)) { flashAlert("That email's copy isn't there any more", 'error'); redirect('email_unprocessed.php'); } $name = trim(preg_replace('/[^A-Za-z0-9_-]+/', '_', (string) $row['email_unprocessed_subject']), '_'); $name = ($name !== '' ? mb_substr($name, 0, 60) : 'email') . '-' . intval($row['email_unprocessed_id']); header('Content-Type: message/rfc822'); header("Content-Disposition: attachment; filename=\"$name.eml\""); header('Content-Length: ' . filesize($path)); header('X-Content-Type-Options: nosniff'); header('Cache-Control: no-store'); readfile($path); exit; }