Scheduled tickets

- Notify agent DL when scheduled tickets are raised (if configured)

- Bugfix: Ticket numbering if two or more scheduled tickets are raised in the same script execution
- Bugfix: Email processing if a contact replied to a scheduled ticket email (still showing original #--itflow# line rather than ##- Please type your reply above this line -##)
This commit is contained in:
Marcus Hill 2023-11-18 11:53:24 +00:00
parent fe1b8ce88f
commit 41cfd8d27b
4 changed files with 63 additions and 54 deletions

View File

@ -45,12 +45,12 @@ $config_recurring_auto_send_invoice = intval($row['config_recurring_auto_send_in
// Tickets // Tickets
$config_ticket_prefix = $row['config_ticket_prefix']; $config_ticket_prefix = $row['config_ticket_prefix'];
$config_ticket_next_number = intval($row['config_ticket_next_number']); $config_ticket_from_name = $row['config_ticket_from_name']; // TODO: Sanitize from_name and from_email on assignment, once everything is moved to the database queue
$config_ticket_from_name = $row['config_ticket_from_name'];
$config_ticket_from_email = $row['config_ticket_from_email']; $config_ticket_from_email = $row['config_ticket_from_email'];
$config_ticket_client_general_notifications = intval($row['config_ticket_client_general_notifications']); $config_ticket_client_general_notifications = intval($row['config_ticket_client_general_notifications']);
$config_ticket_autoclose = intval($row['config_ticket_autoclose']); $config_ticket_autoclose = intval($row['config_ticket_autoclose']);
$config_ticket_autoclose_hours = intval($row['config_ticket_autoclose_hours']); $config_ticket_autoclose_hours = intval($row['config_ticket_autoclose_hours']);
$config_ticket_new_ticket_notification_email = $row['config_ticket_new_ticket_notification_email'];
// Get Config for Telemetry // Get Config for Telemetry
$config_theme = $row['config_theme']; $config_theme = $row['config_theme'];
@ -239,6 +239,7 @@ $sql_scheduled_tickets = mysqli_query($mysqli, "SELECT * FROM scheduled_tickets
if (mysqli_num_rows($sql_scheduled_tickets) > 0) { if (mysqli_num_rows($sql_scheduled_tickets) > 0) {
while ($row = mysqli_fetch_array($sql_scheduled_tickets)) { while ($row = mysqli_fetch_array($sql_scheduled_tickets)) {
$schedule_id = intval($row['scheduled_ticket_id']); $schedule_id = intval($row['scheduled_ticket_id']);
$subject = sanitizeInput($row['scheduled_ticket_subject']); $subject = sanitizeInput($row['scheduled_ticket_subject']);
$details_escaped = mysqli_real_escape_string($mysqli, $row['scheduled_ticket_details']); $details_escaped = mysqli_real_escape_string($mysqli, $row['scheduled_ticket_details']);
@ -250,9 +251,12 @@ if (mysqli_num_rows($sql_scheduled_tickets) > 0) {
$contact_id = intval($row['scheduled_ticket_contact_id']); $contact_id = intval($row['scheduled_ticket_contact_id']);
$asset_id = intval($row['scheduled_ticket_asset_id']); $asset_id = intval($row['scheduled_ticket_asset_id']);
//Get the next Ticket Number and add 1 for the new ticket number // Assign this new ticket the next ticket number
$ticket_number = $config_ticket_next_number; $ticket_number_sql = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_ticket_next_number FROM settings WHERE company_id = 1"));
$new_config_ticket_next_number = $config_ticket_next_number + 1; $ticket_number = intval($ticket_number_sql['config_ticket_next_number']);
// Increment config_ticket_next_number by 1 (for the next ticket)
$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 = 1"); mysqli_query($mysqli, "UPDATE settings SET config_ticket_next_number = $new_config_ticket_next_number WHERE company_id = 1");
// Raise the ticket // Raise the ticket
@ -262,52 +266,57 @@ if (mysqli_num_rows($sql_scheduled_tickets) > 0) {
// Logging // Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'System created scheduled $frequency ticket - $subject', log_client_id = $client_id, log_user_id = $created_id"); mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'System created scheduled $frequency ticket - $subject', log_client_id = $client_id, log_user_id = $created_id");
// E-mail client // Notifications
if (!empty($config_smtp_host) && $config_ticket_client_general_notifications == 1) {
// Get contact/ticket/company details
// Get client/contact/ticket details
$sql = mysqli_query( $sql = mysqli_query(
$mysqli, $mysqli,
"SELECT contact_name, contact_email, ticket_prefix, ticket_number, ticket_subject FROM tickets "SELECT client_name, contact_name, contact_email, ticket_prefix, ticket_number, ticket_priority, ticket_subject, ticket_details FROM tickets
LEFT JOIN clients ON ticket_client_id = client_id LEFT JOIN clients ON ticket_client_id = client_id
LEFT JOIN contacts ON ticket_contact_id = contact_id LEFT JOIN contacts ON ticket_contact_id = contact_id
WHERE ticket_id = $id" WHERE ticket_id = $id"
); );
$row = mysqli_fetch_array($sql); $row = mysqli_fetch_array($sql);
// Escaped - Do not re-escape in the general subject/body escaping as this would re-escape
$config_ticket_from_name_escaped = sanitizeInput($config_ticket_from_name); // TODO: Move this sanitization to the start of cron, once everything uses the queue
$config_ticket_from_email_escaped = sanitizeInput($config_ticket_from_email); // TODO: Move this sanitization to the start of cron, once everything uses the queue
$contact_name_escaped = sanitizeInput($row['contact_name']);
$contact_email_escaped = sanitizeInput($row['contact_email']);
// Unescaped - DANGEROUS unless escaped within the general subject/body escape queries
$client_name = $row['client_name'];
$contact_name = $row['contact_name']; $contact_name = $row['contact_name'];
$contact_email = $row['contact_email']; $contact_email = $row['contact_email'];
$ticket_prefix = $row['ticket_prefix']; $ticket_prefix = $row['ticket_prefix'];
$ticket_number = intval($row['ticket_number']); $ticket_number = intval($row['ticket_number']);
$ticket_priority = $row['ticket_priority'];
$ticket_subject = $row['ticket_subject']; $ticket_subject = $row['ticket_subject'];
$ticket_details = $row['ticket_details']; // Output on settings_mail_queue.php is sanitized through HTML Purifier
// Verify contact email is valid
if (filter_var($contact_email, FILTER_VALIDATE_EMAIL)) {
$subject = "Ticket created - [$ticket_prefix$ticket_number] - $ticket_subject (scheduled)"; // Notify client by email their ticket has been raised, if general notifications are turned on & there is a valid contact email
$body = "<i style='color: #808080'>#--itflow--#</i><br><br>Hello, $contact_name<br><br>A ticket regarding \"$ticket_subject\" has been automatically created for you.<br><br>--------------------------------<br>$details--------------------------------<br><br>Ticket: $ticket_prefix$ticket_number<br>Subject: $ticket_subject<br>Status: Open<br>Portal: https://$config_base_url/portal/ticket.php?id=$id<br><br>~<br>$company_name<br>Support Department<br>$config_ticket_from_email<br>$company_phone"; if (!empty($config_smtp_host) && $config_ticket_client_general_notifications == 1 && filter_var($contact_email, FILTER_VALIDATE_EMAIL)) {
$mail = sendSingleEmail( $email_subject = mysqli_real_escape_string($mysqli, "Ticket created - [$ticket_prefix$ticket_number] - $ticket_subject (scheduled)");
$config_smtp_host, $email_body = mysqli_real_escape_string($mysqli, "<i style='color: #808080'>##- Please type your reply above this line -##</i><br><br>Hello, $contact_name<br><br>A ticket regarding \"$ticket_subject\" has been automatically created for you.<br><br>--------------------------------<br>$details--------------------------------<br><br>Ticket: $ticket_prefix$ticket_number<br>Subject: $ticket_subject<br>Status: Open<br>Portal: https://$config_base_url/portal/ticket.php?id=$id<br><br>~<br>$company_name<br>Support Department<br>$config_ticket_from_email<br>$company_phone");
$config_smtp_username,
$config_smtp_password,
$config_smtp_encryption,
$config_smtp_port,
$config_ticket_from_email,
$config_ticket_from_name,
$contact_email,
$contact_name,
$subject,
$body
);
if ($mail !== true) { mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$contact_email_escaped', email_recipient_name = '$contact_name_escaped', email_from = '$config_ticket_from_email_escaped', email_from_name = '$config_ticket_from_name_escaped', email_subject = '$email_subject', email_content = '$email_body'");
mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Mail', notification = 'Failed to send email to $contact_email'");
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'");
} }
// Notify agent's via the DL address of the new ticket, if it's populated with a valid email
if (filter_var($config_ticket_new_ticket_notification_email, FILTER_VALIDATE_EMAIL)) {
$email_subject = mysqli_real_escape_string($mysqli, "ITFlow - New Scheduled Ticket - $client_name: $ticket_subject");
$email_body = mysqli_real_escape_string($mysqli, "Hello, <br><br>This is a notification that a new scheduled ticket has been raised in ITFlow. <br>Ticket: $ticket_prefix$ticket_number<br>Client: $client_name<br>Priority: $priority<br>Link: https://$config_base_url/ticket.php?ticket_id=$id <br><br>--------------------------------<br><br><b>$ticket_subject</b><br>$ticket_details");
mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$config_ticket_new_ticket_notification_email', email_recipient_name = 'ITFlow Agents', email_from = '$config_ticket_from_email', email_from_name = '$config_ticket_from_name', email_subject = '$email_subject', email_content = '$email_body'");
} }
}
// Set the next run date // Set the next run date
if ($frequency == "weekly") { if ($frequency == "weekly") {

View File

@ -205,7 +205,7 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
$ticket_reply_type = 'Client'; $ticket_reply_type = 'Client';
// Capture just the latest/most recent email reply content // 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--) // based off the "##- Please type your reply above this line -##" line that we prepend the outgoing emails with
$message = explode("##- Please type your reply above this line -##", $message); $message = explode("##- Please type your reply above this line -##", $message);
$message = nl2br($message[0]); $message = nl2br($message[0]);
$message = "<i>Email from: $from_email at $date:-</i> <br><br>$message"; $message = "<i>Email from: $from_email at $date:-</i> <br><br>$message";

View File

@ -24,7 +24,7 @@ if (isset($_POST['add_ticket'])) {
$asset_id = intval($_POST['asset']); $asset_id = intval($_POST['asset']);
$use_primary_contact = intval($_POST['use_primary_contact']); $use_primary_contact = intval($_POST['use_primary_contact']);
// Add the primary contact as the ticket contact if use primary contact is checked // Add the primary contact as the ticket contact if "Use primary contact" is checked
if ($use_primary_contact == 1) { if ($use_primary_contact == 1) {
$sql = mysqli_query($mysqli,"SELECT contact_id FROM contacts WHERE contact_client_id = $client_id AND contact_primary = 1"); $sql = mysqli_query($mysqli,"SELECT contact_id FROM contacts WHERE contact_client_id = $client_id AND contact_primary = 1");
$row = mysqli_fetch_array($sql); $row = mysqli_fetch_array($sql);