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 - 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();
}
// 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;
}