diff --git a/cron_ticket_email_parser.php b/cron_ticket_email_parser.php index 73ff869d..67881aa2 100644 --- a/cron_ticket_email_parser.php +++ b/cron_ticket_email_parser.php @@ -10,58 +10,73 @@ TODO: - Process unregistered contacts/clients into an inbox to allow a ticket to be created/ignored - Better handle replying to closed tickets - Support for authenticating with OAuth - - Documentation - Separate Mailbox Account for tickets 2022-12-14 - JQ - - Properly parse base64 encoded emails (if an Outlook user sends a smiley everything breaks :( - https://electrictoolbox.com/php-imap-message-parts/) Relate PRs to https://github.com/itflow-org/itflow/issues/225 & https://forum.itflow.org/d/11-road-map & https://forum.itflow.org/d/31-tickets-from-email */ // Get ITFlow config & helper functions -include_once("config.php"); -include_once("functions.php"); +require_once("config.php"); +require_once("functions.php"); // Get settings for the "default" company $company_id = 1; $session_company_id = 1; -include_once("get_settings.php"); +require_once("get_settings.php"); // Check setting enabled if ($config_ticket_email_parse == 0) { - exit("Feature is not enabled - see Settings > Ticketing > Email-to-ticket parsing"); + exit("Email Parser: Feature is not enabled - check Settings > Ticketing > Email-to-ticket parsing. See https://wiki.itflow.org/doku.php?id=wiki:ticket_email_parse -- Quitting.."); } -// Check IMAP function exists +// Check IMAP extension works/installed if (!function_exists('imap_open')) { - echo "PHP IMAP extension is not installed, quitting.."; - exit(); + exit("Email Parser: PHP IMAP extension is not installed. See https://wiki.itflow.org/doku.php?id=wiki:ticket_email_parse -- Quitting.."); } +// Check mailparse extension works/installed +if (!function_exists('mailparse_msg_parse_file')) { + exit("Email Parser: PHP mailparse extension is not installed. See https://wiki.itflow.org/doku.php?id=wiki:ticket_email_parse -- Quitting.."); +} + +// PHP Mail Parser +require_once("plugins/php-mime-mail-parser/src/Contracts/CharsetManager.php"); +require_once("plugins/php-mime-mail-parser/src/Contracts/Middleware.php"); +require_once("plugins/php-mime-mail-parser/src/Attachment.php"); +require_once("plugins/php-mime-mail-parser/src/Charset.php"); +require_once("plugins/php-mime-mail-parser/src/Exception.php"); +require_once("plugins/php-mime-mail-parser/src/Middleware.php"); +require_once("plugins/php-mime-mail-parser/src/MiddlewareStack.php"); +require_once("plugins/php-mime-mail-parser/src/MimePart.php"); +require_once("plugins/php-mime-mail-parser/src/Parser.php"); + // Function to raise a new ticket for a given contact and email them confirmation (if configured) -function createTicket($contact_id, $contact_name, $contact_email, $client_id, $company_id, $date, $subject, $message) { +function addTicket($contact_id, $contact_name, $contact_email, $client_id, $company_id, $date, $subject, $message) +{ // Access global variables - global $mysqli, $config_ticket_next_number, $config_ticket_prefix, $config_ticket_client_general_notifications, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $config_smtp_host, $config_smtp_port, $config_smtp_encryption, $config_smtp_username, $config_smtp_password; + global $mysqli, $config_ticket_prefix, $config_ticket_client_general_notifications, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $config_smtp_host, $config_smtp_port, $config_smtp_encryption, $config_smtp_username, $config_smtp_password; + + // Get the next Ticket Number and add 1 for the new ticket number + $ticket_number_sql = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_ticket_next_number FROM settings WHERE company_id = $company_id")); + $ticket_number = intval($ticket_number_sql['config_ticket_next_number']); + $new_config_ticket_next_number = $ticket_number + 1; + mysqli_query($mysqli, "UPDATE settings SET config_ticket_next_number = $new_config_ticket_next_number WHERE company_id = $company_id"); // Prep ticket details $message = nl2br(htmlentities(strip_tags($message))); - $message = trim(mysqli_real_escape_string($mysqli,"Email from: $contact_email at $date:-

$message")); + $message = trim(mysqli_real_escape_string($mysqli, "Email from: $contact_email at $date:-

$message")); - // Get the next Ticket Number and add 1 for the new ticket number - $ticket_number = $config_ticket_next_number; - $new_config_ticket_next_number = $config_ticket_next_number + 1; - mysqli_query($mysqli,"UPDATE settings SET config_ticket_next_number = $new_config_ticket_next_number WHERE company_id = $company_id"); - - mysqli_query($mysqli,"INSERT INTO tickets SET ticket_prefix = '$config_ticket_prefix', ticket_number = $ticket_number, ticket_subject = '$subject', ticket_details = '$message', ticket_priority = 'Low', ticket_status = 'Open', ticket_created_at = NOW(), ticket_created_by = '0', ticket_contact_id = $contact_id, ticket_client_id = $client_id, company_id = $company_id"); + mysqli_query($mysqli, "INSERT INTO tickets SET ticket_prefix = '$config_ticket_prefix', ticket_number = $ticket_number, ticket_subject = '$subject', ticket_details = '$message', ticket_priority = 'Low', ticket_status = 'Open', ticket_created_at = NOW(), ticket_created_by = '0', ticket_contact_id = $contact_id, ticket_client_id = $client_id, company_id = $company_id"); $id = mysqli_insert_id($mysqli); // Logging echo "Created new ticket.
"; - mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'Email parser: Client contact $contact_email created ticket $config_ticket_prefix$ticket_number ($subject)', log_created_at = NOW(), log_client_id = $client_id, company_id = $company_id"); + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'Email parser: Client contact $contact_email created ticket $config_ticket_prefix$ticket_number ($subject) ($id)', log_created_at = NOW(), log_client_id = $client_id, company_id = $company_id"); // Get company name & phone - $sql = mysqli_query($mysqli,"SELECT company_name, company_phone FROM companies WHERE company_id = $company_id"); + $sql = mysqli_query($mysqli, "SELECT company_name, company_phone FROM companies WHERE company_id = $company_id"); $row = mysqli_fetch_array($sql); $company_phone = formatPhoneNumber($row['company_phone']); $company_name = $row['company_name']; @@ -79,8 +94,8 @@ function createTicket($contact_id, $contact_name, $contact_email, $client_id, $c $email_subject, $email_body); if ($mail !== true) { - mysqli_query($mysqli,"INSERT INTO notifications SET notification_type = 'Mail', notification = 'Failed to send email to $contact_email', notification_timestamp = NOW(), company_id = $company_id"); - mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Mail', log_action = 'Error', log_description = 'Failed to send email to $contact_email regarding $subject. $mail', company_id = $company_id"); + mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Mail', notification = 'Failed to send email to $contact_email', notification_timestamp = NOW(), company_id = $company_id"); + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Mail', log_action = 'Error', log_description = 'Failed to send email to $contact_email regarding $subject. $mail', company_id = $company_id"); } } @@ -89,6 +104,83 @@ function createTicket($contact_id, $contact_name, $contact_email, $client_id, $c } +function addReply($from_email, $date, $subject, $ticket_number, $message) +{ + // Add email as a comment/reply to an existing ticket + + // Access global variables + global $mysqli, $config_ticket_prefix, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $config_smtp_host, $config_smtp_port, $config_smtp_encryption, $config_smtp_username, $config_smtp_password; + + // Set default reply type + $ticket_reply_type = 'Client'; + + // Capture just the latest/most recent email reply content + // based off the "#--itflow#" line that we prepend the outgoing emails with (similar to the old school --reply above this line--) + $message = explode("#--itflow--#", $message); + $message = nl2br(htmlentities(strip_tags($message[0]))); + $message = "Email from: $from_email at $date:-

$message"; + + // Lookup the ticket ID + $row = mysqli_fetch_array(mysqli_query($mysqli, "SELECT ticket_id, ticket_subject, ticket_status, ticket_contact_id, ticket_client_id, tickets.company_id, contact_email + FROM tickets + LEFT JOIN contacts on tickets.ticket_contact_id = contacts.contact_id + WHERE ticket_number = '$ticket_number' LIMIT 1")); + + if ($row) { + + // Get ticket details + $ticket_id = $row['ticket_id']; + $ticket_status = $row['ticket_status']; + $ticket_reply_contact = $row['ticket_contact_id']; + $ticket_contact_email = $row['contact_email']; + $client_id = $row['ticket_client_id']; + $company_id = $row['company_id']; + + // Check ticket isn't closed + if ($ticket_status == "Closed") { + mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Ticket', notification = 'Email parser: $from_email attempted to re-open ticket $config_ticket_prefix$ticket_number (ID $ticket_id) - check inbox manually to see email', notification_timestamp = NOW(), notification_client_id = '$client_id', company_id = '$company_id'"); + return false; + } + + // Check WHO replied (was it the owner of the ticket or someone else on CC?) + if (empty($ticket_contact_email) || $ticket_contact_email !== $from_email) { + + // It wasn't the contact currently assigned to the ticket, check if it's another registered contact for that client + + $row = mysqli_fetch_array(mysqli_query($mysqli, "SELECT contact_id FROM contacts WHERE contact_email = '$from_email' AND contact_client_id = $client_id LIMIT 1")); + if ($row) { + + // Contact is known - we can keep the reply type as client + $ticket_reply_contact = $row['contact_id']; + + } else { + // Mark the reply as internal as we don't recognise the contact (so the actual contact doesn't see it, and the tech can edit/delete if needed) + $ticket_reply_type = 'Internal'; + $ticket_reply_contact = '0'; + $message = "WARNING: Contact email mismatch
$message"; // Add a warning at the start of the message - for the techs benefit (think phishing/scams) + } + } + + // Sanitize ticket reply + $comment = trim(mysqli_real_escape_string($mysqli, $message)); + + // Add the comment + mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = '$comment', ticket_reply_type = '$ticket_reply_type', ticket_reply_time_worked = '00:00:00', ticket_reply_created_at = NOW(), ticket_reply_by = '$ticket_reply_contact', ticket_reply_ticket_id = '$ticket_id', company_id = '$company_id'"); + + // Update Ticket Last Response Field & set ticket to open as client has replied + mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 'Open', ticket_updated_at = NOW() WHERE ticket_id = $ticket_id AND ticket_client_id = '$client_id' LIMIT 1"); + + echo "Updated existing ticket.
"; + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Update', log_description = 'Email parser: Client contact $from_email updated ticket $config_ticket_prefix$ticket_number ($subject)', log_created_at = NOW(), log_client_id = $client_id, company_id = $company_id"); + + return true; + + } else { + // Invalid ticket number + return false; + } +} + // Prepare connection string with encryption (TLS/SSL/) $imap_mailbox = "$config_imap_host:$config_imap_port/imap/$config_imap_encryption"; @@ -99,7 +191,7 @@ $imap = imap_open("{{$imap_mailbox}}INBOX", $config_smtp_username, $config_smtp_ if (!$imap) { // Logging $extended_log_description = var_export(imap_errors(), true); - mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Mail', log_action = 'Error', log_description = 'Email parser: Failed to connect to IMAP. Details: $extended_log_description', company_id = $company_id"); + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Mail', log_action = 'Error', log_description = 'Email parser: Failed to connect to IMAP. Details: $extended_log_description', company_id = $company_id"); exit("Could not connect to IMAP"); } @@ -124,108 +216,66 @@ if ($emails) { // Default false $email_processed = false; - // Get message details - $metadata = imap_fetch_overview($imap, $email,0); // Date, Subject, Size - $header = imap_headerinfo($imap, $email); // To get the From as an email, not a contact name - $message = (imap_fetchbody($imap, $email, 1)); // Body + // Get details from message and invoke PHP Mime Mail Parser + $msg_to_parse = imap_fetchheader($imap, $email, FT_PREFETCHTEXT) . imap_body($imap, $email); + $parser = new PhpMimeMailParser\Parser(); + $parser->setText($msg_to_parse); - $from = trim(mysqli_real_escape_string($mysqli, htmlentities(strip_tags($header->from[0]->mailbox . "@" . $header->from[0]->host)))); - $subject = trim(mysqli_real_escape_string($mysqli, htmlentities(strip_tags($metadata[0]->subject)))); - $date = trim(mysqli_real_escape_string($mysqli, htmlentities(strip_tags($metadata[0]->date)))); + // Process message attributes + + $from_array = $parser->getAddresses('from')[0]; + $from_name = trim(mysqli_real_escape_string($mysqli, htmlentities(strip_tags($from_array['display'])))); + $from_email = trim(mysqli_real_escape_string($mysqli, htmlentities(strip_tags($from_array['address'])))); + $from_domain = explode("@", $from_array['address']); + $from_domain = trim(mysqli_real_escape_string($mysqli, htmlentities(strip_tags(end($from_domain))))); // Use the final element in the array (as technically legal to have multiple @'s) + + $subject = trim(mysqli_real_escape_string($mysqli, htmlentities(strip_tags($parser->getHeader('subject'))))); + $date = trim(mysqli_real_escape_string($mysqli, htmlentities(strip_tags($parser->getHeader('date'))))); + + $message = $parser->getMessageBody('text'); - $domain = trim(mysqli_real_escape_string($mysqli, $header->from[0]->host)); - $from_name = trim(mysqli_real_escape_string($mysqli, $header->from[0]->mailbox)); // Check if we can identify a ticket number (in square brackets) if (preg_match("/\[$config_ticket_prefix\d+\]/", $subject, $ticket_number)) { + // Looks like there's a ticket number in the subject line (e.g. [TCK-091] + // Process as a ticket reply + // Get the actual ticket number (without the brackets) preg_match('/\d+/', $ticket_number[0], $ticket_number); $ticket_number = intval($ticket_number[0]); - // Split the email into just the latest reply, with some metadata - // We base this off the string "#--itflow--#" that we prepend the outgoing emails with (similar to the old school --reply above this line--) - $message = explode("#--itflow--#", $message); - $message = nl2br(htmlentities(strip_tags($message[0]))); - $message = "Email from: $from at $date:-

$message"; - - // Lookup the ticket ID to add the reply to (just to check in-case the ID is different from the number). - $ticket_sql = mysqli_query($mysqli, "SELECT * FROM tickets WHERE ticket_number = '$ticket_number' LIMIT 1"); - $row = mysqli_fetch_array($ticket_sql); - $ticket_id = $row['ticket_id']; - $ticket_reply_contact = $row['ticket_contact_id']; - $ticket_assigned_to = $row['ticket_assigned_to']; - $client_id = $row['ticket_client_id']; - $company_id = $row['company_id']; - $ticket_reply_type = 'Client'; // Setting to client as a default value - - // Check the ticket ID is valid - if (intval($ticket_id) && $ticket_id !== '0') { - - // Check that ticket is open - if ($row['ticket_status'] == "Closed") { - - // It's closed - let's notify someone that a client tried to reply - mysqli_query($mysqli,"INSERT INTO notifications SET notification_type = 'Ticket', notification = 'Email parser: $from attempted to re-open ticket ID $ticket_id ($config_ticket_prefix$ticket_number) - check inbox manually to see email', notification_timestamp = NOW(), notification_client_id = '$client_id', company_id = '$company_id'"); - - } else { - - // Ticket is open, proceed. - - // Check the email matches the contact's email - if it doesn't then mark the reply as internal (so the contact doesn't see it, and the tech can edit/delete if needed) - // Niche edge case - possibly where CC's on an email reply to a ticket? - $contact_sql = mysqli_query($mysqli, "SELECT contact_email FROM contacts WHERE contact_id = '$ticket_reply_contact'"); - $row = mysqli_fetch_array($contact_sql); - if ($from !== $row['contact_email']) { - $ticket_reply_type = 'Internal'; - $ticket_reply_contact = '0'; - $message = "WARNING: Contact email mismatch
$message"; // Add a warning at the start of the message - for the techs benefit (think phishing/scams) - } - - // Sanitize ticket reply - $comment = trim(mysqli_real_escape_string($mysqli, $message)); - - // Add the comment - mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = '$comment', ticket_reply_type = '$ticket_reply_type', ticket_reply_time_worked = '00:00:00', ticket_reply_created_at = NOW(), ticket_reply_by = '$ticket_reply_contact', ticket_reply_ticket_id = '$ticket_id', company_id = '$company_id'"); - - // Update Ticket Last Response Field & set ticket to open as client has replied - mysqli_query($mysqli,"UPDATE tickets SET ticket_status = 'Open', ticket_updated_at = NOW() WHERE ticket_id = $ticket_id AND ticket_client_id = '$client_id' LIMIT 1"); - - echo "Updated existing ticket.
"; - mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Ticket', log_action = 'Update', log_description = 'Email parser: Client contact $from updated ticket $config_ticket_prefix$ticket_number ($subject)', log_created_at = NOW(), log_client_id = $client_id, company_id = $company_id"); - - $email_processed = true; - } - + if (addReply($from_email, $date, $subject, $ticket_number, $message)) { + $email_processed = true; } - } else { // Couldn't match this email to an existing ticket // Check if we can match the sender to a pre-existing contact - $any_contact_sql = mysqli_query($mysqli, "SELECT * FROM contacts WHERE contact_email = '$from' LIMIT 1"); + $any_contact_sql = mysqli_query($mysqli, "SELECT * FROM contacts WHERE contact_email = '$from_email' LIMIT 1"); $row = mysqli_fetch_array($any_contact_sql); - $contact_name = $row['contact_name']; - $contact_id = $row['contact_id']; - $contact_email = $row['contact_email']; - $client_id = $row['contact_client_id']; - $company_id = $row['company_id']; + if ($row) { + // Sender exists as a contact + $contact_name = $row['contact_name']; + $contact_id = $row['contact_id']; + $contact_email = $row['contact_email']; + $client_id = $row['contact_client_id']; + $company_id = $row['company_id']; - if ($from == $contact_email) { - - createTicket($contact_id, $contact_name, $contact_email, $client_id, $company_id, $date, $subject, $message); - $email_processed = true; + if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $company_id, $date, $subject, $message)) { + $email_processed = true; + } } else { // Couldn't match this email to an existing ticket or an existing client contact // Checking to see if the sender domain matches a client website - $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM clients WHERE client_website = '$domain' LIMIT 1")); + $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM clients WHERE client_website = '$from_domain' LIMIT 1")); - if ($row && $domain == $row['client_website']) { + if ($row && $from_domain == $row['client_website']) { // We found a match - create a contact under this client and raise a ticket for them @@ -236,22 +286,22 @@ if ($emails) { // Contact details $password = password_hash(randomString(), PASSWORD_DEFAULT); $contact_name = $from_name; - $contact_email = $from; - mysqli_query($mysqli,"INSERT INTO contacts SET contact_name = '$contact_name', contact_email = '$contact_email', contact_notes = 'Added automatically via email parsing.', contact_password_hash = '$password', contact_client_id = $client_id, company_id = $company_id"); + $contact_email = $from_email; + mysqli_query($mysqli, "INSERT INTO contacts SET contact_name = '$contact_name', contact_email = '$contact_email', contact_notes = 'Added automatically via email parsing.', contact_password_hash = '$password', contact_client_id = $client_id, company_id = $company_id"); $contact_id = mysqli_insert_id($mysqli); // Logging for contact creation echo "Created new contact.
"; - mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = 'Email parser: created contact $contact_name', log_client_id = $client_id, company_id = $company_id"); + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = 'Email parser: created contact $contact_name', log_client_id = $client_id, company_id = $company_id"); - createTicket($contact_id, $contact_name, $contact_email, $client_id, $company_id, $date, $subject, $message); - - $email_processed = true; + if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $company_id, $date, $subject, $message)) { + $email_processed = true; + } } else { // Couldn't match this email to an existing ticket, existing contact or an existing client via the "from" domain - // In the future we might make a page where these can be nicely viewed / managed, but for now we'll just flag them as needing attention + // In the future we might make a page where these can be nicely viewed / managed, but for now we'll just flag them in the Inbox as needing attention } @@ -259,10 +309,11 @@ if ($emails) { } - // Deal with the message + // Deal with the message (move it if processed, flag it if not) if ($email_processed) { imap_mail_move($imap, $email, $imap_folder); } else { + echo "Failed to process email - flagging for manual review."; imap_setflag_full($imap, $email, "\\Flagged"); } @@ -271,6 +322,5 @@ if ($emails) { } - imap_expunge($imap); imap_close($imap);