mirror of
https://github.com/itflow-org/itflow
synced 2026-03-28 00:05:40 +00:00
Migrate cron_ticket_email_parser.php back to using php-mime-mail-parser libraries and php extensions php imap and php Mailparse while still keeping the new functionality from the webklex php-imap version of this script. There were wayyy to many dependcies with WebKlex and ran into way too many problems. This delays OAAUTH2 until we can find a better solution
This commit is contained in:
@@ -1,15 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
/*
|
||||||
* CRON - Email Parser
|
* CRON - Email Parser
|
||||||
|
* Based on Libraries php-mime-mail-parser and PHP Extensions PHP IMAP and PHP MAilparse
|
||||||
* Process emails and create/update tickets
|
* Process emails and create/update tickets
|
||||||
|
* To Do:
|
||||||
|
* -- Show Mail from header in HTML Emails currently only work in text based emails
|
||||||
|
* -- When replying via email do not show original reply in Ticket Reply Body
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Set working directory to the directory this cron script lives at.
|
// Set working directory to the directory this cron script lives at.
|
||||||
chdir(dirname(__FILE__));
|
chdir(dirname(__FILE__));
|
||||||
|
|
||||||
// Autoload Composer dependencies
|
|
||||||
require_once __DIR__ . '/plugins/php-imap/vendor/autoload.php';
|
|
||||||
|
|
||||||
// Get ITFlow config & helper functions
|
// Get ITFlow config & helper functions
|
||||||
require_once "config.php";
|
require_once "config.php";
|
||||||
|
|
||||||
@@ -65,16 +66,33 @@ if (file_exists($lock_file_path)) {
|
|||||||
// Create a lock file
|
// Create a lock file
|
||||||
file_put_contents($lock_file_path, "Locked");
|
file_put_contents($lock_file_path, "Locked");
|
||||||
|
|
||||||
// Webklex PHP-IMAP
|
// PHP Mail Parser
|
||||||
use Webklex\PHPIMAP\ClientManager;
|
use PhpMimeMailParser\Parser;
|
||||||
use Webklex\PHPIMAP\Message\Attachment;
|
|
||||||
|
require_once "plugins/php-mime-mail-parser/Contracts/CharsetManager.php";
|
||||||
|
|
||||||
|
require_once "plugins/php-mime-mail-parser/Contracts/Middleware.php";
|
||||||
|
|
||||||
|
require_once "plugins/php-mime-mail-parser/Attachment.php";
|
||||||
|
|
||||||
|
require_once "plugins/php-mime-mail-parser/Charset.php";
|
||||||
|
|
||||||
|
require_once "plugins/php-mime-mail-parser/Exception.php";
|
||||||
|
|
||||||
|
require_once "plugins/php-mime-mail-parser/Middleware.php";
|
||||||
|
|
||||||
|
require_once "plugins/php-mime-mail-parser/MiddlewareStack.php";
|
||||||
|
|
||||||
|
require_once "plugins/php-mime-mail-parser/MimePart.php";
|
||||||
|
|
||||||
|
require_once "plugins/php-mime-mail-parser/Parser.php";
|
||||||
|
|
||||||
// Allowed attachment extensions
|
// Allowed attachment extensions
|
||||||
$allowed_extensions = array('jpg', 'jpeg', 'gif', 'png', 'webp', 'pdf', 'txt', 'md', 'doc', 'docx', 'csv', 'xls', 'xlsx', 'xlsm', 'zip', 'tar', 'gz');
|
$allowed_extensions = array('jpg', 'jpeg', 'gif', 'png', 'webp', 'pdf', 'txt', 'md', 'doc', 'docx', 'csv', 'xls', 'xlsx', 'xlsm', 'zip', 'tar', 'gz');
|
||||||
|
|
||||||
// Function to raise a new ticket for a given contact and email them confirmation (if configured)
|
// Function to raise a new ticket for a given contact and email them confirmation (if configured)
|
||||||
function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message, $attachments, $original_message_file) {
|
function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message, $attachments, $original_message_file) {
|
||||||
global $mysqli, $config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_ticket_client_general_notifications, $config_ticket_new_ticket_notification_email, $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, $allowed_extensions;
|
global $mysqli, $config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_ticket_client_general_notifications, $config_ticket_new_ticket_notification_email, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $allowed_extensions;
|
||||||
|
|
||||||
$ticket_number_sql = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_ticket_next_number FROM settings WHERE company_id = 1"));
|
$ticket_number_sql = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_ticket_next_number FROM settings WHERE company_id = 1"));
|
||||||
$ticket_number = intval($ticket_number_sql['config_ticket_next_number']);
|
$ticket_number = intval($ticket_number_sql['config_ticket_next_number']);
|
||||||
@@ -85,7 +103,7 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
|
|||||||
$message = trim($message); // Remove leading/trailing whitespace
|
$message = trim($message); // Remove leading/trailing whitespace
|
||||||
$message = preg_replace('/\s+/', ' ', $message); // Replace multiple spaces with a single space
|
$message = preg_replace('/\s+/', ' ', $message); // Replace multiple spaces with a single space
|
||||||
$message = nl2br($message); // Convert newlines to <br>
|
$message = nl2br($message); // Convert newlines to <br>
|
||||||
|
|
||||||
// Wrap the message in a div with controlled line height
|
// Wrap the message in a div with controlled line height
|
||||||
$message = "<i>Email from: <b>$contact_name</b> <$contact_email> at $date:-</i> <br><br><div style='line-height:1.5;'>$message</div>";
|
$message = "<i>Email from: <b>$contact_name</b> <$contact_email> at $date:-</i> <br><br><div style='line-height:1.5;'>$message</div>";
|
||||||
|
|
||||||
@@ -112,15 +130,14 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
|
|||||||
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_ticket_id = $id");
|
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_ticket_id = $id");
|
||||||
|
|
||||||
foreach ($attachments as $attachment) {
|
foreach ($attachments as $attachment) {
|
||||||
$att_name = $attachment->getName();
|
$att_name = $attachment->getFilename();
|
||||||
$att_extarr = explode('.', $att_name);
|
$att_extarr = explode('.', $att_name);
|
||||||
$att_extension = strtolower(end($att_extarr));
|
$att_extension = strtolower(end($att_extarr));
|
||||||
|
|
||||||
if (in_array($att_extension, $allowed_extensions)) {
|
if (in_array($att_extension, $allowed_extensions)) {
|
||||||
$att_saved_filename = md5(uniqid(rand(), true)) . '.' . $att_extension;
|
$att_saved_filename = md5(uniqid(rand(), true)) . '.' . $att_extension;
|
||||||
$att_saved_path = $att_dir . $att_saved_filename;
|
$att_saved_path = $att_dir . $att_saved_filename;
|
||||||
$attachment->save($att_dir); // Save the attachment to the directory
|
file_put_contents($att_saved_path, $attachment->getContent());
|
||||||
rename($att_dir . $attachment->getName(), $att_saved_path); // Rename the saved file to the hashed name
|
|
||||||
|
|
||||||
$ticket_attachment_name = sanitizeInput($att_name);
|
$ticket_attachment_name = sanitizeInput($att_name);
|
||||||
$ticket_attachment_reference_name = sanitizeInput($att_saved_filename);
|
$ticket_attachment_reference_name = sanitizeInput($att_saved_filename);
|
||||||
@@ -150,12 +167,12 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
|
|||||||
|
|
||||||
if ($config_ticket_new_ticket_notification_email) {
|
if ($config_ticket_new_ticket_notification_email) {
|
||||||
if ($client_id == 0){
|
if ($client_id == 0){
|
||||||
$client_name = "Guest";
|
$client_name = "Guest";
|
||||||
} else {
|
} else {
|
||||||
$client_sql = mysqli_query($mysqli, "SELECT client_name FROM clients WHERE client_id = $client_id");
|
$client_sql = mysqli_query($mysqli, "SELECT client_name FROM clients WHERE client_id = $client_id");
|
||||||
$client_row = mysqli_fetch_array($client_sql);
|
$client_row = mysqli_fetch_array($client_sql);
|
||||||
$client_name = sanitizeInput($client_row['client_name']);
|
$client_name = sanitizeInput($client_row['client_name']);
|
||||||
}
|
}
|
||||||
$email_subject = "$config_app_name - New Ticket - $client_name: $subject";
|
$email_subject = "$config_app_name - New Ticket - $client_name: $subject";
|
||||||
$email_body = "Hello, <br><br>This is a notification that a new ticket has been raised in ITFlow. <br>Client: $client_name<br>Priority: Low (email parsed)<br>Link: https://$config_base_url/ticket.php?ticket_id=$id <br><br>--------------------------------<br><br><b>$subject</b><br>$message";
|
$email_body = "Hello, <br><br>This is a notification that a new ticket has been raised in ITFlow. <br>Client: $client_name<br>Priority: Low (email parsed)<br>Link: https://$config_base_url/ticket.php?ticket_id=$id <br><br>--------------------------------<br><br><b>$subject</b><br>$message";
|
||||||
|
|
||||||
@@ -179,7 +196,7 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
|
|||||||
|
|
||||||
// Add Reply Function
|
// Add Reply Function
|
||||||
function addReply($from_email, $date, $subject, $ticket_number, $message, $attachments) {
|
function addReply($from_email, $date, $subject, $ticket_number, $message, $attachments) {
|
||||||
global $mysqli, $config_app_name, $company_name, $company_phone, $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, $allowed_extensions;
|
global $mysqli, $config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $allowed_extensions;
|
||||||
|
|
||||||
$ticket_reply_type = 'Client';
|
$ticket_reply_type = 'Client';
|
||||||
// Clean up the message
|
// Clean up the message
|
||||||
@@ -256,15 +273,14 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
|
|||||||
|
|
||||||
mkdirMissing('uploads/tickets/');
|
mkdirMissing('uploads/tickets/');
|
||||||
foreach ($attachments as $attachment) {
|
foreach ($attachments as $attachment) {
|
||||||
$att_name = $attachment->getName();
|
$att_name = $attachment->getFilename();
|
||||||
$att_extarr = explode('.', $att_name);
|
$att_extarr = explode('.', $att_name);
|
||||||
$att_extension = strtolower(end($att_extarr));
|
$att_extension = strtolower(end($att_extarr));
|
||||||
|
|
||||||
if (in_array($att_extension, $allowed_extensions)) {
|
if (in_array($att_extension, $allowed_extensions)) {
|
||||||
$att_saved_filename = md5(uniqid(rand(), true)) . '.' . $att_extension;
|
$att_saved_filename = md5(uniqid(rand(), true)) . '.' . $att_extension;
|
||||||
$att_saved_path = "uploads/tickets/" . $ticket_id . "/" . $att_saved_filename;
|
$att_saved_path = "uploads/tickets/" . $ticket_id . "/" . $att_saved_filename;
|
||||||
$attachment->save("uploads/tickets/" . $ticket_id); // Save the attachment to the directory
|
file_put_contents($att_saved_path, $attachment->getContent());
|
||||||
rename("uploads/tickets/" . $ticket_id . "/" . $attachment->getName(), $att_saved_path); // Rename the saved file to the hashed name
|
|
||||||
|
|
||||||
$ticket_attachment_name = sanitizeInput($att_name);
|
$ticket_attachment_name = sanitizeInput($att_name);
|
||||||
$ticket_attachment_reference_name = sanitizeInput($att_saved_filename);
|
$ticket_attachment_reference_name = sanitizeInput($att_saved_filename);
|
||||||
@@ -278,10 +294,10 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$ticket_assigned_to = mysqli_query($mysqli, "SELECT ticket_assigned_to FROM tickets WHERE ticket_id = $ticket_id LIMIT 1");
|
$ticket_assigned_to_sql = mysqli_query($mysqli, "SELECT ticket_assigned_to FROM tickets WHERE ticket_id = $ticket_id LIMIT 1");
|
||||||
|
|
||||||
if ($ticket_assigned_to) {
|
if ($ticket_assigned_to_sql) {
|
||||||
$row = mysqli_fetch_array($ticket_assigned_to);
|
$row = mysqli_fetch_array($ticket_assigned_to_sql);
|
||||||
$ticket_assigned_to = intval($row['ticket_assigned_to']);
|
$ticket_assigned_to = intval($row['ticket_assigned_to']);
|
||||||
|
|
||||||
if ($ticket_assigned_to) {
|
if ($ticket_assigned_to) {
|
||||||
@@ -321,132 +337,111 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Function to create a folder in the mailbox if it doesn't exist
|
// Function to create a folder in the mailbox if it doesn't exist
|
||||||
function createMailboxFolder($client, $folderName) {
|
function createMailboxFolder($imap, $mailbox, $folderName) {
|
||||||
try {
|
$folders = imap_list($imap, $mailbox, '*');
|
||||||
// Attempt to get the folder
|
$folderExists = false;
|
||||||
$folder = $client->getFolder($folderName);
|
if ($folders !== false) {
|
||||||
|
foreach ($folders as $folder) {
|
||||||
// If the folder doesn't exist, create it
|
$folder = str_replace($mailbox, '', $folder);
|
||||||
if (!$folder) {
|
if ($folder == $folderName) {
|
||||||
$client->createFolder($folderName);
|
$folderExists = true;
|
||||||
echo "Folder '$folderName' created successfully.";
|
break;
|
||||||
|
}
|
||||||
// Disconnect and reconnect to ensure the server registers the new folder
|
|
||||||
$client->disconnect();
|
|
||||||
sleep(1); // Pause before reconnecting
|
|
||||||
$client->connect();
|
|
||||||
} else {
|
|
||||||
echo "Folder '$folderName' already exists.";
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Re-fetch the folder after reconnecting
|
if (!$folderExists) {
|
||||||
return $client->getFolder($folderName);
|
imap_createmailbox($imap, $mailbox . imap_utf7_encode($folderName));
|
||||||
|
imap_subscribe($imap, $mailbox . $folderName);
|
||||||
} catch (Exception $e) {
|
|
||||||
echo "Error creating folder '$folderName': " . $e->getMessage();
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to subscribe to a folder in the mailbox
|
// Initialize IMAP connection
|
||||||
function subscribeMailboxFolder($folder) {
|
$validate_cert = true; // or false based on your configuration
|
||||||
if ($folder) {
|
|
||||||
try {
|
$imap_encryption = $config_imap_encryption; // e.g., 'ssl' or 'tls'
|
||||||
// Subscribe to the folder
|
|
||||||
$folder->subscribe();
|
$mailbox = '{' . $config_imap_host . ':' . $config_imap_port . '/' . $imap_encryption;
|
||||||
echo "Folder '{$folder->name}' subscribed successfully.";
|
if ($validate_cert) {
|
||||||
} catch (Exception $e) {
|
$mailbox .= '/validate-cert';
|
||||||
echo "Error subscribing to folder '{$folder->name}': " . $e->getMessage();
|
} else {
|
||||||
}
|
$mailbox .= '/novalidate-cert';
|
||||||
} else {
|
|
||||||
echo "Cannot subscribe to folder because it does not exist.";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$mailbox .= '}';
|
||||||
|
$inbox_mailbox = $mailbox . 'INBOX';
|
||||||
|
|
||||||
// Initialize the client manager and create the client
|
$imap = imap_open($inbox_mailbox, $config_imap_username, $config_imap_password);
|
||||||
$clientManager = new ClientManager();
|
|
||||||
$client = $clientManager->make([
|
|
||||||
'host' => $config_imap_host,
|
|
||||||
'port' => $config_imap_port,
|
|
||||||
'encryption' => $config_imap_encryption,
|
|
||||||
'validate_cert' => true,
|
|
||||||
'username' => $config_imap_username,
|
|
||||||
'password' => $config_imap_password,
|
|
||||||
'protocol' => 'imap'
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Connect to the IMAP server
|
if ($imap === false) {
|
||||||
$client->connect();
|
echo "Error connecting to IMAP server: " . imap_last_error();
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
// Create the "ITFlow" mailbox folder if it doesn't exist
|
// Create the "ITFlow" mailbox folder if it doesn't exist
|
||||||
$folder = createMailboxFolder($client, 'ITFlow');
|
createMailboxFolder($imap, $mailbox, 'ITFlow');
|
||||||
|
|
||||||
// Subscribe to the "ITFlow" mailbox folder
|
// Search for unseen messages
|
||||||
subscribeMailboxFolder($folder);
|
$emails = imap_search($imap, 'UNSEEN');
|
||||||
|
|
||||||
// Possible names for the inbox folder
|
if ($emails !== false) {
|
||||||
$inboxNames = ['Inbox', 'INBOX', 'inbox'];
|
foreach ($emails as $email_number) {
|
||||||
|
|
||||||
// Function to get the correct inbox folder
|
|
||||||
function getInboxFolder($client, $inboxNames) {
|
|
||||||
foreach ($inboxNames as $name) {
|
|
||||||
try {
|
|
||||||
$folder = $client->getFolder($name);
|
|
||||||
if ($folder) {
|
|
||||||
return $folder;
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
// Continue to the next name if the current one fails
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new Exception("No inbox folder found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$inbox = getInboxFolder($client, $inboxNames);
|
|
||||||
$messages = $inbox->query()->unseen()->get();
|
|
||||||
} catch (Exception $e) {
|
|
||||||
echo "Error: " . $e->getMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($messages->count() > 0) {
|
|
||||||
foreach ($messages as $message) {
|
|
||||||
$email_processed = false;
|
$email_processed = false;
|
||||||
|
|
||||||
// Save original message
|
// Save original message
|
||||||
mkdirMissing('uploads/tmp/');
|
mkdirMissing('uploads/tmp/');
|
||||||
$original_message_file = "processed-eml-" . randomString(200) . ".eml";
|
$original_message_file = "processed-eml-" . randomString(200) . ".eml";
|
||||||
$eml_content = json_decode(json_encode($message->getHeader()), true)['raw'];
|
|
||||||
$eml_content .= $message->getRawBody();
|
|
||||||
file_put_contents("uploads/tmp/{$original_message_file}", $eml_content);
|
|
||||||
|
|
||||||
$from_address = $message->getFrom();
|
$raw_message = imap_fetchheader($imap, $email_number) . imap_body($imap, $email_number);
|
||||||
$from_name = sanitizeInput($from_address[0]->personal ?? 'Unknown');
|
file_put_contents("uploads/tmp/{$original_message_file}", $raw_message);
|
||||||
$from_email = sanitizeInput($from_address[0]->mail ?? 'itflow-guest@example.com');
|
|
||||||
|
// Parse the message using php-mime-mail-parser
|
||||||
|
$parser = new \PhpMimeMailParser\Parser();
|
||||||
|
$parser->setText($raw_message);
|
||||||
|
|
||||||
|
// Get from address
|
||||||
|
$from_addresses = $parser->getAddresses('from');
|
||||||
|
$from_email = sanitizeInput($from_addresses[0]['address'] ?? 'itflow-guest@example.com');
|
||||||
|
$from_name = sanitizeInput($from_addresses[0]['display'] ?? 'Unknown');
|
||||||
|
|
||||||
$from_domain = explode("@", $from_email);
|
$from_domain = explode("@", $from_email);
|
||||||
$from_domain = sanitizeInput(end($from_domain));
|
$from_domain = sanitizeInput(end($from_domain));
|
||||||
|
|
||||||
$subject = sanitizeInput($message->getSubject() ?? 'No Subject');
|
// Get subject
|
||||||
$date = sanitizeInput($message->getDate() ?? date('Y-m-d H:i:s'));
|
$subject = sanitizeInput($parser->getHeader('subject') ?? 'No Subject');
|
||||||
$message_body = $message->getHtmlBody() ?? '';
|
|
||||||
|
|
||||||
if (empty($message_body)) {
|
// Get date
|
||||||
$text_body = $message->getTextBody() ?? '';
|
$date = sanitizeInput($parser->getHeader('date') ?? date('Y-m-d H:i:s'));
|
||||||
$message_body = nl2br(htmlspecialchars($text_body));
|
|
||||||
|
// Get message body
|
||||||
|
$message_body_html = $parser->getMessageBody('html');
|
||||||
|
$message_body_text = $parser->getMessageBody('text');
|
||||||
|
$message_body = $message_body_html ?: nl2br(htmlspecialchars($message_body_text));
|
||||||
|
|
||||||
|
// Handle inline images
|
||||||
|
$attachments = $parser->getAttachments();
|
||||||
|
$inline_attachments = [];
|
||||||
|
foreach ($attachments as $attachment) {
|
||||||
|
if ($attachment->getContentDisposition() === 'inline' && $attachment->getContentID()) {
|
||||||
|
$cid = trim($attachment->getContentID(), '<>');
|
||||||
|
$data = base64_encode($attachment->getContent());
|
||||||
|
$mime = $attachment->getContentType(); // Use getContentType() instead of getMimeType()
|
||||||
|
$dataUri = "data:$mime;base64,$data";
|
||||||
|
$message_body = str_replace("cid:$cid", $dataUri, $message_body);
|
||||||
|
} else {
|
||||||
|
$inline_attachments[] = $attachment;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
$attachments = $inline_attachments;
|
||||||
|
|
||||||
if (preg_match("/\[$config_ticket_prefix\d+\]/", $subject, $ticket_number)) {
|
// Process the email
|
||||||
preg_match('/\d+/', $ticket_number[0], $ticket_number);
|
if (preg_match("/\[$config_ticket_prefix(\d+)\]/", $subject, $ticket_number_matches)) {
|
||||||
$ticket_number = intval($ticket_number[0]);
|
$ticket_number = intval($ticket_number_matches[1]);
|
||||||
|
|
||||||
if (addReply($from_email, $date, $subject, $ticket_number, $message_body, $message->getAttachments())) {
|
if (addReply($from_email, $date, $subject, $ticket_number, $message_body, $attachments)) {
|
||||||
$email_processed = true;
|
$email_processed = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Check if the sender is a known contact
|
||||||
$from_email_esc = mysqli_real_escape_string($mysqli, $from_email);
|
$from_email_esc = mysqli_real_escape_string($mysqli, $from_email);
|
||||||
$any_contact_sql = mysqli_query($mysqli, "SELECT * FROM contacts WHERE contact_email = '$from_email_esc' LIMIT 1");
|
$any_contact_sql = mysqli_query($mysqli, "SELECT * FROM contacts WHERE contact_email = '$from_email_esc' LIMIT 1");
|
||||||
$row = mysqli_fetch_array($any_contact_sql);
|
$row = mysqli_fetch_array($any_contact_sql);
|
||||||
@@ -457,16 +452,19 @@ if ($messages->count() > 0) {
|
|||||||
$contact_email = sanitizeInput($row['contact_email']);
|
$contact_email = sanitizeInput($row['contact_email']);
|
||||||
$client_id = intval($row['contact_client_id']);
|
$client_id = intval($row['contact_client_id']);
|
||||||
|
|
||||||
if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $message->getAttachments(), $original_message_file)) {
|
if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file)) {
|
||||||
$email_processed = true;
|
$email_processed = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Check if the domain is associated with a client
|
||||||
$from_domain_esc = mysqli_real_escape_string($mysqli, $from_domain);
|
$from_domain_esc = mysqli_real_escape_string($mysqli, $from_domain);
|
||||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM domains WHERE domain_name = '$from_domain_esc' LIMIT 1"));
|
$domain_sql = mysqli_query($mysqli, "SELECT * FROM domains WHERE domain_name = '$from_domain_esc' LIMIT 1");
|
||||||
|
$row = mysqli_fetch_assoc($domain_sql);
|
||||||
|
|
||||||
if ($row && $from_domain == $row['domain_name']) {
|
if ($row && $from_domain == $row['domain_name']) {
|
||||||
$client_id = intval($row['domain_client_id']);
|
$client_id = intval($row['domain_client_id']);
|
||||||
|
|
||||||
|
// Create a new contact
|
||||||
$password = password_hash(randomString(), PASSWORD_DEFAULT);
|
$password = password_hash(randomString(), PASSWORD_DEFAULT);
|
||||||
$contact_name = $from_name;
|
$contact_name = $from_name;
|
||||||
$contact_email = $from_email;
|
$contact_email = $from_email;
|
||||||
@@ -474,16 +472,16 @@ if ($messages->count() > 0) {
|
|||||||
$contact_id = mysqli_insert_id($mysqli);
|
$contact_id = mysqli_insert_id($mysqli);
|
||||||
|
|
||||||
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = 'Email parser: created contact ".mysqli_real_escape_string($mysqli, $contact_name)."', log_client_id = $client_id");
|
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = 'Email parser: created contact ".mysqli_real_escape_string($mysqli, $contact_name)."', log_client_id = $client_id");
|
||||||
customAction('contact_create', $ticket_id);
|
customAction('contact_create', $contact_id);
|
||||||
|
|
||||||
if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $message->getAttachments(), $original_message_file)) {
|
if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file)) {
|
||||||
$email_processed = true;
|
$email_processed = true;
|
||||||
}
|
}
|
||||||
} elseif ($config_ticket_email_parse_unknown_senders) {
|
} elseif ($config_ticket_email_parse_unknown_senders) {
|
||||||
// Parse even if the sender is unknown
|
// Parse even if the sender is unknown
|
||||||
$bad_from_pattern = "/daemon|postmaster/i";
|
$bad_from_pattern = "/daemon|postmaster/i";
|
||||||
if (!(preg_match($bad_from_pattern, $from_email))) {
|
if (!(preg_match($bad_from_pattern, $from_email))) {
|
||||||
if (addTicket(0, $from_name, $from_email, 0, $date, $subject, $message_body, $message->getAttachments(), $original_message_file)) {
|
if (addTicket(0, $from_name, $from_email, 0, $date, $subject, $message_body, $attachments, $original_message_file)) {
|
||||||
$email_processed = true;
|
$email_processed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -492,21 +490,28 @@ if ($messages->count() > 0) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($email_processed) {
|
if ($email_processed) {
|
||||||
$message->setFlag(['Seen']);
|
// Mark the message as seen
|
||||||
$message->move('ITFlow');
|
imap_setflag_full($imap, $email_number, "\\Seen");
|
||||||
|
// Move the message to the 'ITFlow' folder
|
||||||
|
imap_mail_move($imap, $email_number, 'ITFlow');
|
||||||
} else {
|
} else {
|
||||||
echo "Failed to process email - flagging for manual review.";
|
// Flag the message for manual review
|
||||||
$message->setFlag(['Flagged']);
|
imap_setflag_full($imap, $email_number, "\\Flagged");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete the temporary message file
|
||||||
if (file_exists("uploads/tmp/{$original_message_file}")) {
|
if (file_exists("uploads/tmp/{$original_message_file}")) {
|
||||||
unlink("uploads/tmp/{$original_message_file}");
|
unlink("uploads/tmp/{$original_message_file}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$client->expunge();
|
// Expunge deleted mails
|
||||||
$client->disconnect();
|
imap_expunge($imap);
|
||||||
|
|
||||||
|
// Close the IMAP connection
|
||||||
|
imap_close($imap);
|
||||||
|
|
||||||
// Remove the lock file
|
// Remove the lock file
|
||||||
unlink($lock_file_path);
|
unlink($lock_file_path);
|
||||||
|
?>
|
||||||
|
|||||||
@@ -7,6 +7,9 @@
|
|||||||
// Set working directory to the directory this cron script lives at.
|
// Set working directory to the directory this cron script lives at.
|
||||||
chdir(dirname(__FILE__));
|
chdir(dirname(__FILE__));
|
||||||
|
|
||||||
|
// Autoload Composer dependencies
|
||||||
|
require_once __DIR__ . '/plugins/php-imap/vendor/autoload.php';
|
||||||
|
|
||||||
// Get ITFlow config & helper functions
|
// Get ITFlow config & helper functions
|
||||||
require_once "config.php";
|
require_once "config.php";
|
||||||
|
|
||||||
@@ -62,33 +65,16 @@ if (file_exists($lock_file_path)) {
|
|||||||
// Create a lock file
|
// Create a lock file
|
||||||
file_put_contents($lock_file_path, "Locked");
|
file_put_contents($lock_file_path, "Locked");
|
||||||
|
|
||||||
// PHP Mail Parser
|
// Webklex PHP-IMAP
|
||||||
use PhpMimeMailParser\Parser;
|
use Webklex\PHPIMAP\ClientManager;
|
||||||
|
use Webklex\PHPIMAP\Message\Attachment;
|
||||||
require_once "plugins/php-mime-mail-parser/Contracts/CharsetManager.php";
|
|
||||||
|
|
||||||
require_once "plugins/php-mime-mail-parser/Contracts/Middleware.php";
|
|
||||||
|
|
||||||
require_once "plugins/php-mime-mail-parser/Attachment.php";
|
|
||||||
|
|
||||||
require_once "plugins/php-mime-mail-parser/Charset.php";
|
|
||||||
|
|
||||||
require_once "plugins/php-mime-mail-parser/Exception.php";
|
|
||||||
|
|
||||||
require_once "plugins/php-mime-mail-parser/Middleware.php";
|
|
||||||
|
|
||||||
require_once "plugins/php-mime-mail-parser/MiddlewareStack.php";
|
|
||||||
|
|
||||||
require_once "plugins/php-mime-mail-parser/MimePart.php";
|
|
||||||
|
|
||||||
require_once "plugins/php-mime-mail-parser/Parser.php";
|
|
||||||
|
|
||||||
// Allowed attachment extensions
|
// Allowed attachment extensions
|
||||||
$allowed_extensions = array('jpg', 'jpeg', 'gif', 'png', 'webp', 'pdf', 'txt', 'md', 'doc', 'docx', 'csv', 'xls', 'xlsx', 'xlsm', 'zip', 'tar', 'gz');
|
$allowed_extensions = array('jpg', 'jpeg', 'gif', 'png', 'webp', 'pdf', 'txt', 'md', 'doc', 'docx', 'csv', 'xls', 'xlsx', 'xlsm', 'zip', 'tar', 'gz');
|
||||||
|
|
||||||
// Function to raise a new ticket for a given contact and email them confirmation (if configured)
|
// Function to raise a new ticket for a given contact and email them confirmation (if configured)
|
||||||
function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message, $attachments, $original_message_file) {
|
function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message, $attachments, $original_message_file) {
|
||||||
global $mysqli, $config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_ticket_client_general_notifications, $config_ticket_new_ticket_notification_email, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $allowed_extensions;
|
global $mysqli, $config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_ticket_client_general_notifications, $config_ticket_new_ticket_notification_email, $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, $allowed_extensions;
|
||||||
|
|
||||||
$ticket_number_sql = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_ticket_next_number FROM settings WHERE company_id = 1"));
|
$ticket_number_sql = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_ticket_next_number FROM settings WHERE company_id = 1"));
|
||||||
$ticket_number = intval($ticket_number_sql['config_ticket_next_number']);
|
$ticket_number = intval($ticket_number_sql['config_ticket_next_number']);
|
||||||
@@ -99,7 +85,7 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
|
|||||||
$message = trim($message); // Remove leading/trailing whitespace
|
$message = trim($message); // Remove leading/trailing whitespace
|
||||||
$message = preg_replace('/\s+/', ' ', $message); // Replace multiple spaces with a single space
|
$message = preg_replace('/\s+/', ' ', $message); // Replace multiple spaces with a single space
|
||||||
$message = nl2br($message); // Convert newlines to <br>
|
$message = nl2br($message); // Convert newlines to <br>
|
||||||
|
|
||||||
// Wrap the message in a div with controlled line height
|
// Wrap the message in a div with controlled line height
|
||||||
$message = "<i>Email from: <b>$contact_name</b> <$contact_email> at $date:-</i> <br><br><div style='line-height:1.5;'>$message</div>";
|
$message = "<i>Email from: <b>$contact_name</b> <$contact_email> at $date:-</i> <br><br><div style='line-height:1.5;'>$message</div>";
|
||||||
|
|
||||||
@@ -126,14 +112,15 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
|
|||||||
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_ticket_id = $id");
|
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_ticket_id = $id");
|
||||||
|
|
||||||
foreach ($attachments as $attachment) {
|
foreach ($attachments as $attachment) {
|
||||||
$att_name = $attachment->getFilename();
|
$att_name = $attachment->getName();
|
||||||
$att_extarr = explode('.', $att_name);
|
$att_extarr = explode('.', $att_name);
|
||||||
$att_extension = strtolower(end($att_extarr));
|
$att_extension = strtolower(end($att_extarr));
|
||||||
|
|
||||||
if (in_array($att_extension, $allowed_extensions)) {
|
if (in_array($att_extension, $allowed_extensions)) {
|
||||||
$att_saved_filename = md5(uniqid(rand(), true)) . '.' . $att_extension;
|
$att_saved_filename = md5(uniqid(rand(), true)) . '.' . $att_extension;
|
||||||
$att_saved_path = $att_dir . $att_saved_filename;
|
$att_saved_path = $att_dir . $att_saved_filename;
|
||||||
file_put_contents($att_saved_path, $attachment->getContent());
|
$attachment->save($att_dir); // Save the attachment to the directory
|
||||||
|
rename($att_dir . $attachment->getName(), $att_saved_path); // Rename the saved file to the hashed name
|
||||||
|
|
||||||
$ticket_attachment_name = sanitizeInput($att_name);
|
$ticket_attachment_name = sanitizeInput($att_name);
|
||||||
$ticket_attachment_reference_name = sanitizeInput($att_saved_filename);
|
$ticket_attachment_reference_name = sanitizeInput($att_saved_filename);
|
||||||
@@ -163,12 +150,12 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
|
|||||||
|
|
||||||
if ($config_ticket_new_ticket_notification_email) {
|
if ($config_ticket_new_ticket_notification_email) {
|
||||||
if ($client_id == 0){
|
if ($client_id == 0){
|
||||||
$client_name = "Guest";
|
$client_name = "Guest";
|
||||||
} else {
|
} else {
|
||||||
$client_sql = mysqli_query($mysqli, "SELECT client_name FROM clients WHERE client_id = $client_id");
|
$client_sql = mysqli_query($mysqli, "SELECT client_name FROM clients WHERE client_id = $client_id");
|
||||||
$client_row = mysqli_fetch_array($client_sql);
|
$client_row = mysqli_fetch_array($client_sql);
|
||||||
$client_name = sanitizeInput($client_row['client_name']);
|
$client_name = sanitizeInput($client_row['client_name']);
|
||||||
}
|
}
|
||||||
$email_subject = "$config_app_name - New Ticket - $client_name: $subject";
|
$email_subject = "$config_app_name - New Ticket - $client_name: $subject";
|
||||||
$email_body = "Hello, <br><br>This is a notification that a new ticket has been raised in ITFlow. <br>Client: $client_name<br>Priority: Low (email parsed)<br>Link: https://$config_base_url/ticket.php?ticket_id=$id <br><br>--------------------------------<br><br><b>$subject</b><br>$message";
|
$email_body = "Hello, <br><br>This is a notification that a new ticket has been raised in ITFlow. <br>Client: $client_name<br>Priority: Low (email parsed)<br>Link: https://$config_base_url/ticket.php?ticket_id=$id <br><br>--------------------------------<br><br><b>$subject</b><br>$message";
|
||||||
|
|
||||||
@@ -192,7 +179,7 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
|
|||||||
|
|
||||||
// Add Reply Function
|
// Add Reply Function
|
||||||
function addReply($from_email, $date, $subject, $ticket_number, $message, $attachments) {
|
function addReply($from_email, $date, $subject, $ticket_number, $message, $attachments) {
|
||||||
global $mysqli, $config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $allowed_extensions;
|
global $mysqli, $config_app_name, $company_name, $company_phone, $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, $allowed_extensions;
|
||||||
|
|
||||||
$ticket_reply_type = 'Client';
|
$ticket_reply_type = 'Client';
|
||||||
// Clean up the message
|
// Clean up the message
|
||||||
@@ -269,14 +256,15 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
|
|||||||
|
|
||||||
mkdirMissing('uploads/tickets/');
|
mkdirMissing('uploads/tickets/');
|
||||||
foreach ($attachments as $attachment) {
|
foreach ($attachments as $attachment) {
|
||||||
$att_name = $attachment->getFilename();
|
$att_name = $attachment->getName();
|
||||||
$att_extarr = explode('.', $att_name);
|
$att_extarr = explode('.', $att_name);
|
||||||
$att_extension = strtolower(end($att_extarr));
|
$att_extension = strtolower(end($att_extarr));
|
||||||
|
|
||||||
if (in_array($att_extension, $allowed_extensions)) {
|
if (in_array($att_extension, $allowed_extensions)) {
|
||||||
$att_saved_filename = md5(uniqid(rand(), true)) . '.' . $att_extension;
|
$att_saved_filename = md5(uniqid(rand(), true)) . '.' . $att_extension;
|
||||||
$att_saved_path = "uploads/tickets/" . $ticket_id . "/" . $att_saved_filename;
|
$att_saved_path = "uploads/tickets/" . $ticket_id . "/" . $att_saved_filename;
|
||||||
file_put_contents($att_saved_path, $attachment->getContent());
|
$attachment->save("uploads/tickets/" . $ticket_id); // Save the attachment to the directory
|
||||||
|
rename("uploads/tickets/" . $ticket_id . "/" . $attachment->getName(), $att_saved_path); // Rename the saved file to the hashed name
|
||||||
|
|
||||||
$ticket_attachment_name = sanitizeInput($att_name);
|
$ticket_attachment_name = sanitizeInput($att_name);
|
||||||
$ticket_attachment_reference_name = sanitizeInput($att_saved_filename);
|
$ticket_attachment_reference_name = sanitizeInput($att_saved_filename);
|
||||||
@@ -290,10 +278,10 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$ticket_assigned_to_sql = mysqli_query($mysqli, "SELECT ticket_assigned_to FROM tickets WHERE ticket_id = $ticket_id LIMIT 1");
|
$ticket_assigned_to = mysqli_query($mysqli, "SELECT ticket_assigned_to FROM tickets WHERE ticket_id = $ticket_id LIMIT 1");
|
||||||
|
|
||||||
if ($ticket_assigned_to_sql) {
|
if ($ticket_assigned_to) {
|
||||||
$row = mysqli_fetch_array($ticket_assigned_to_sql);
|
$row = mysqli_fetch_array($ticket_assigned_to);
|
||||||
$ticket_assigned_to = intval($row['ticket_assigned_to']);
|
$ticket_assigned_to = intval($row['ticket_assigned_to']);
|
||||||
|
|
||||||
if ($ticket_assigned_to) {
|
if ($ticket_assigned_to) {
|
||||||
@@ -333,111 +321,132 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Function to create a folder in the mailbox if it doesn't exist
|
// Function to create a folder in the mailbox if it doesn't exist
|
||||||
function createMailboxFolder($imap, $mailbox, $folderName) {
|
function createMailboxFolder($client, $folderName) {
|
||||||
$folders = imap_list($imap, $mailbox, '*');
|
try {
|
||||||
$folderExists = false;
|
// Attempt to get the folder
|
||||||
if ($folders !== false) {
|
$folder = $client->getFolder($folderName);
|
||||||
foreach ($folders as $folder) {
|
|
||||||
$folder = str_replace($mailbox, '', $folder);
|
// If the folder doesn't exist, create it
|
||||||
if ($folder == $folderName) {
|
if (!$folder) {
|
||||||
$folderExists = true;
|
$client->createFolder($folderName);
|
||||||
break;
|
echo "Folder '$folderName' created successfully.";
|
||||||
}
|
|
||||||
|
// Disconnect and reconnect to ensure the server registers the new folder
|
||||||
|
$client->disconnect();
|
||||||
|
sleep(1); // Pause before reconnecting
|
||||||
|
$client->connect();
|
||||||
|
} else {
|
||||||
|
echo "Folder '$folderName' already exists.";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (!$folderExists) {
|
// Re-fetch the folder after reconnecting
|
||||||
imap_createmailbox($imap, $mailbox . imap_utf7_encode($folderName));
|
return $client->getFolder($folderName);
|
||||||
imap_subscribe($imap, $mailbox . $folderName);
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
echo "Error creating folder '$folderName': " . $e->getMessage();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize IMAP connection
|
// Function to subscribe to a folder in the mailbox
|
||||||
$validate_cert = true; // or false based on your configuration
|
function subscribeMailboxFolder($folder) {
|
||||||
|
if ($folder) {
|
||||||
$imap_encryption = $config_imap_encryption; // e.g., 'ssl' or 'tls'
|
try {
|
||||||
|
// Subscribe to the folder
|
||||||
$mailbox = '{' . $config_imap_host . ':' . $config_imap_port . '/' . $imap_encryption;
|
$folder->subscribe();
|
||||||
if ($validate_cert) {
|
echo "Folder '{$folder->name}' subscribed successfully.";
|
||||||
$mailbox .= '/validate-cert';
|
} catch (Exception $e) {
|
||||||
} else {
|
echo "Error subscribing to folder '{$folder->name}': " . $e->getMessage();
|
||||||
$mailbox .= '/novalidate-cert';
|
}
|
||||||
|
} else {
|
||||||
|
echo "Cannot subscribe to folder because it does not exist.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$mailbox .= '}';
|
|
||||||
$inbox_mailbox = $mailbox . 'INBOX';
|
|
||||||
|
|
||||||
$imap = imap_open($inbox_mailbox, $config_imap_username, $config_imap_password);
|
// Initialize the client manager and create the client
|
||||||
|
$clientManager = new ClientManager();
|
||||||
|
$client = $clientManager->make([
|
||||||
|
'host' => $config_imap_host,
|
||||||
|
'port' => $config_imap_port,
|
||||||
|
'encryption' => $config_imap_encryption,
|
||||||
|
'validate_cert' => true,
|
||||||
|
'username' => $config_imap_username,
|
||||||
|
'password' => $config_imap_password,
|
||||||
|
'protocol' => 'imap'
|
||||||
|
]);
|
||||||
|
|
||||||
if ($imap === false) {
|
// Connect to the IMAP server
|
||||||
echo "Error connecting to IMAP server: " . imap_last_error();
|
$client->connect();
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the "ITFlow" mailbox folder if it doesn't exist
|
// Create the "ITFlow" mailbox folder if it doesn't exist
|
||||||
createMailboxFolder($imap, $mailbox, 'ITFlow');
|
$folder = createMailboxFolder($client, 'ITFlow');
|
||||||
|
|
||||||
// Search for unseen messages
|
// Subscribe to the "ITFlow" mailbox folder
|
||||||
$emails = imap_search($imap, 'UNSEEN');
|
subscribeMailboxFolder($folder);
|
||||||
|
|
||||||
if ($emails !== false) {
|
// Possible names for the inbox folder
|
||||||
foreach ($emails as $email_number) {
|
$inboxNames = ['Inbox', 'INBOX', 'inbox'];
|
||||||
|
|
||||||
|
// Function to get the correct inbox folder
|
||||||
|
function getInboxFolder($client, $inboxNames) {
|
||||||
|
foreach ($inboxNames as $name) {
|
||||||
|
try {
|
||||||
|
$folder = $client->getFolder($name);
|
||||||
|
if ($folder) {
|
||||||
|
return $folder;
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// Continue to the next name if the current one fails
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Exception("No inbox folder found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$inbox = getInboxFolder($client, $inboxNames);
|
||||||
|
$messages = $inbox->query()->unseen()->get();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
echo "Error: " . $e->getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($messages->count() > 0) {
|
||||||
|
foreach ($messages as $message) {
|
||||||
$email_processed = false;
|
$email_processed = false;
|
||||||
|
|
||||||
// Save original message
|
// Save original message
|
||||||
mkdirMissing('uploads/tmp/');
|
mkdirMissing('uploads/tmp/');
|
||||||
$original_message_file = "processed-eml-" . randomString(200) . ".eml";
|
$original_message_file = "processed-eml-" . randomString(200) . ".eml";
|
||||||
|
$eml_content = json_decode(json_encode($message->getHeader()), true)['raw'];
|
||||||
|
$eml_content .= $message->getRawBody();
|
||||||
|
file_put_contents("uploads/tmp/{$original_message_file}", $eml_content);
|
||||||
|
|
||||||
$raw_message = imap_fetchheader($imap, $email_number) . imap_body($imap, $email_number);
|
$from_address = $message->getFrom();
|
||||||
file_put_contents("uploads/tmp/{$original_message_file}", $raw_message);
|
$from_name = sanitizeInput($from_address[0]->personal ?? 'Unknown');
|
||||||
|
$from_email = sanitizeInput($from_address[0]->mail ?? 'itflow-guest@example.com');
|
||||||
// Parse the message using php-mime-mail-parser
|
|
||||||
$parser = new \PhpMimeMailParser\Parser();
|
|
||||||
$parser->setText($raw_message);
|
|
||||||
|
|
||||||
// Get from address
|
|
||||||
$from_addresses = $parser->getAddresses('from');
|
|
||||||
$from_email = sanitizeInput($from_addresses[0]['address'] ?? 'itflow-guest@example.com');
|
|
||||||
$from_name = sanitizeInput($from_addresses[0]['display'] ?? 'Unknown');
|
|
||||||
|
|
||||||
$from_domain = explode("@", $from_email);
|
$from_domain = explode("@", $from_email);
|
||||||
$from_domain = sanitizeInput(end($from_domain));
|
$from_domain = sanitizeInput(end($from_domain));
|
||||||
|
|
||||||
// Get subject
|
$subject = sanitizeInput($message->getSubject() ?? 'No Subject');
|
||||||
$subject = sanitizeInput($parser->getHeader('subject') ?? 'No Subject');
|
$date = sanitizeInput($message->getDate() ?? date('Y-m-d H:i:s'));
|
||||||
|
$message_body = $message->getHtmlBody() ?? '';
|
||||||
|
|
||||||
// Get date
|
if (empty($message_body)) {
|
||||||
$date = sanitizeInput($parser->getHeader('date') ?? date('Y-m-d H:i:s'));
|
$text_body = $message->getTextBody() ?? '';
|
||||||
|
$message_body = nl2br(htmlspecialchars($text_body));
|
||||||
// Get message body
|
|
||||||
$message_body_html = $parser->getMessageBody('html');
|
|
||||||
$message_body_text = $parser->getMessageBody('text');
|
|
||||||
$message_body = $message_body_html ?: nl2br(htmlspecialchars($message_body_text));
|
|
||||||
|
|
||||||
// Handle inline images
|
|
||||||
$attachments = $parser->getAttachments();
|
|
||||||
$inline_attachments = [];
|
|
||||||
foreach ($attachments as $attachment) {
|
|
||||||
if ($attachment->getContentDisposition() === 'inline' && $attachment->getContentID()) {
|
|
||||||
$cid = trim($attachment->getContentID(), '<>');
|
|
||||||
$data = base64_encode($attachment->getContent());
|
|
||||||
$mime = $attachment->getContentType(); // Use getContentType() instead of getMimeType()
|
|
||||||
$dataUri = "data:$mime;base64,$data";
|
|
||||||
$message_body = str_replace("cid:$cid", $dataUri, $message_body);
|
|
||||||
} else {
|
|
||||||
$inline_attachments[] = $attachment;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$attachments = $inline_attachments;
|
|
||||||
|
|
||||||
// Process the email
|
if (preg_match("/\[$config_ticket_prefix\d+\]/", $subject, $ticket_number)) {
|
||||||
if (preg_match("/\[$config_ticket_prefix(\d+)\]/", $subject, $ticket_number_matches)) {
|
preg_match('/\d+/', $ticket_number[0], $ticket_number);
|
||||||
$ticket_number = intval($ticket_number_matches[1]);
|
$ticket_number = intval($ticket_number[0]);
|
||||||
|
|
||||||
if (addReply($from_email, $date, $subject, $ticket_number, $message_body, $attachments)) {
|
if (addReply($from_email, $date, $subject, $ticket_number, $message_body, $message->getAttachments())) {
|
||||||
$email_processed = true;
|
$email_processed = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Check if the sender is a known contact
|
|
||||||
$from_email_esc = mysqli_real_escape_string($mysqli, $from_email);
|
$from_email_esc = mysqli_real_escape_string($mysqli, $from_email);
|
||||||
$any_contact_sql = mysqli_query($mysqli, "SELECT * FROM contacts WHERE contact_email = '$from_email_esc' LIMIT 1");
|
$any_contact_sql = mysqli_query($mysqli, "SELECT * FROM contacts WHERE contact_email = '$from_email_esc' LIMIT 1");
|
||||||
$row = mysqli_fetch_array($any_contact_sql);
|
$row = mysqli_fetch_array($any_contact_sql);
|
||||||
@@ -448,19 +457,16 @@ if ($emails !== false) {
|
|||||||
$contact_email = sanitizeInput($row['contact_email']);
|
$contact_email = sanitizeInput($row['contact_email']);
|
||||||
$client_id = intval($row['contact_client_id']);
|
$client_id = intval($row['contact_client_id']);
|
||||||
|
|
||||||
if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file)) {
|
if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $message->getAttachments(), $original_message_file)) {
|
||||||
$email_processed = true;
|
$email_processed = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Check if the domain is associated with a client
|
|
||||||
$from_domain_esc = mysqli_real_escape_string($mysqli, $from_domain);
|
$from_domain_esc = mysqli_real_escape_string($mysqli, $from_domain);
|
||||||
$domain_sql = mysqli_query($mysqli, "SELECT * FROM domains WHERE domain_name = '$from_domain_esc' LIMIT 1");
|
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM domains WHERE domain_name = '$from_domain_esc' LIMIT 1"));
|
||||||
$row = mysqli_fetch_assoc($domain_sql);
|
|
||||||
|
|
||||||
if ($row && $from_domain == $row['domain_name']) {
|
if ($row && $from_domain == $row['domain_name']) {
|
||||||
$client_id = intval($row['domain_client_id']);
|
$client_id = intval($row['domain_client_id']);
|
||||||
|
|
||||||
// Create a new contact
|
|
||||||
$password = password_hash(randomString(), PASSWORD_DEFAULT);
|
$password = password_hash(randomString(), PASSWORD_DEFAULT);
|
||||||
$contact_name = $from_name;
|
$contact_name = $from_name;
|
||||||
$contact_email = $from_email;
|
$contact_email = $from_email;
|
||||||
@@ -468,16 +474,16 @@ if ($emails !== false) {
|
|||||||
$contact_id = mysqli_insert_id($mysqli);
|
$contact_id = mysqli_insert_id($mysqli);
|
||||||
|
|
||||||
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = 'Email parser: created contact ".mysqli_real_escape_string($mysqli, $contact_name)."', log_client_id = $client_id");
|
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = 'Email parser: created contact ".mysqli_real_escape_string($mysqli, $contact_name)."', log_client_id = $client_id");
|
||||||
customAction('contact_create', $contact_id);
|
customAction('contact_create', $ticket_id);
|
||||||
|
|
||||||
if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file)) {
|
if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $message->getAttachments(), $original_message_file)) {
|
||||||
$email_processed = true;
|
$email_processed = true;
|
||||||
}
|
}
|
||||||
} elseif ($config_ticket_email_parse_unknown_senders) {
|
} elseif ($config_ticket_email_parse_unknown_senders) {
|
||||||
// Parse even if the sender is unknown
|
// Parse even if the sender is unknown
|
||||||
$bad_from_pattern = "/daemon|postmaster/i";
|
$bad_from_pattern = "/daemon|postmaster/i";
|
||||||
if (!(preg_match($bad_from_pattern, $from_email))) {
|
if (!(preg_match($bad_from_pattern, $from_email))) {
|
||||||
if (addTicket(0, $from_name, $from_email, 0, $date, $subject, $message_body, $attachments, $original_message_file)) {
|
if (addTicket(0, $from_name, $from_email, 0, $date, $subject, $message_body, $message->getAttachments(), $original_message_file)) {
|
||||||
$email_processed = true;
|
$email_processed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -486,28 +492,21 @@ if ($emails !== false) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($email_processed) {
|
if ($email_processed) {
|
||||||
// Mark the message as seen
|
$message->setFlag(['Seen']);
|
||||||
imap_setflag_full($imap, $email_number, "\\Seen");
|
$message->move('ITFlow');
|
||||||
// Move the message to the 'ITFlow' folder
|
|
||||||
imap_mail_move($imap, $email_number, 'ITFlow');
|
|
||||||
} else {
|
} else {
|
||||||
// Flag the message for manual review
|
echo "Failed to process email - flagging for manual review.";
|
||||||
imap_setflag_full($imap, $email_number, "\\Flagged");
|
$message->setFlag(['Flagged']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the temporary message file
|
|
||||||
if (file_exists("uploads/tmp/{$original_message_file}")) {
|
if (file_exists("uploads/tmp/{$original_message_file}")) {
|
||||||
unlink("uploads/tmp/{$original_message_file}");
|
unlink("uploads/tmp/{$original_message_file}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expunge deleted mails
|
$client->expunge();
|
||||||
imap_expunge($imap);
|
$client->disconnect();
|
||||||
|
|
||||||
// Close the IMAP connection
|
|
||||||
imap_close($imap);
|
|
||||||
|
|
||||||
// Remove the lock file
|
// Remove the lock file
|
||||||
unlink($lock_file_path);
|
unlink($lock_file_path);
|
||||||
?>
|
|
||||||
Reference in New Issue
Block a user