Add fax email with templates and fax busy

This commit is contained in:
FusionPBX 2022-04-02 22:49:25 -06:00 committed by GitHub
parent a99395033f
commit ec7af36ba1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 287 additions and 98 deletions

View File

@ -57,7 +57,7 @@
$fax_queue_uuid = $_GET['fax_queue_uuid']; $fax_queue_uuid = $_GET['fax_queue_uuid'];
$hostname = $_GET['hostname']; $hostname = $_GET['hostname'];
//get the email details to send //get the fax queue details to send
$sql = "select q.*, d.domain_name "; $sql = "select q.*, d.domain_name ";
$sql .= "from v_fax_queue as q, v_domains as d "; $sql .= "from v_fax_queue as q, v_domains as d ";
$sql .= "where fax_queue_uuid = :fax_queue_uuid "; $sql .= "where fax_queue_uuid = :fax_queue_uuid ";
@ -85,9 +85,88 @@
} }
unset($parameters); unset($parameters);
//determine if the retry count exceed the limit
if ($fax_retry_count > $retry_limit) {
$fax_status = 'failed';
}
//get the default settings
$sql = "select default_setting_uuid, default_setting_name, default_setting_category, default_setting_subcategory, default_setting_value ";
$sql .= "from v_default_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and default_setting_category in ('domain', 'fax', fax_queue') ";
$sql .= "and default_setting_enabled = 'true' ";
$parameters['domain_uuid'] = $domain_uuid;
$database = new database;
$result = $database->select($sql, $parameters, 'all');
unset($sql, $parameters);
if (is_array($result) && sizeof($result) != 0) {
foreach ($result as $row) {
$name = $row['default_setting_name'];
$category = $row['default_setting_category'];
$subcategory = $row['default_setting_subcategory'];
if ($subcategory != '') {
if ($name == "array") {
$_SESSION[$category][] = $row['default_setting_value'];
}
else {
$_SESSION[$category][$name] = $row['default_setting_value'];
}
}
else {
if ($name == "array") {
$_SESSION[$category][$subcategory][] = $row['default_setting_value'];
}
else {
$_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
$_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
}
}
}
}
unset($result, $row);
//get the domain settings
$sql = "select domain_setting_uuid, domain_setting_name, domain_setting_category, domain_setting_subcategory, domain_setting_value ";
$sql .= "from v_domain_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and domain_setting_category in ('domain', 'fax', fax_queue') ";
$sql .= "and domain_setting_enabled = 'true' ";
$parameters['domain_uuid'] = $domain_uuid;
$database = new database;
$result = $database->select($sql, $parameters, 'all');
unset($sql, $parameters);
if (is_array($result) && sizeof($result) != 0) {
foreach ($result as $row) {
$name = $row['domain_setting_name'];
$category = $row['domain_setting_category'];
$subcategory = $row['domain_setting_subcategory'];
if ($subcategory != '') {
if ($name == "array") {
$_SESSION[$category][] = $row['domain_setting_value'];
}
else {
$_SESSION[$category][$name] = $row['domain_setting_value'];
}
}
else {
if ($name == "array") {
$_SESSION[$category][$subcategory][] = $row['domain_setting_value'];
}
else {
$_SESSION[$category][$subcategory]['uuid'] = $row['domain_setting_uuid'];
$_SESSION[$category][$subcategory][$name] = $row['domain_setting_value'];
}
}
}
}
unset($result, $row);
//get some more info to send the fax //get some more info to send the fax
$mail_from_address = (isset($_SESSION['fax']['smtp_from']['text'])) ? $_SESSION['fax']['smtp_from']['text'] : $_SESSION['email']['smtp_from']['text']; $mail_from_address = (isset($_SESSION['fax']['smtp_from']['text'])) ? $_SESSION['fax']['smtp_from']['text'] : $_SESSION['email']['smtp_from']['text'];
//attempt sending the fax
if ($fax_status == 'waiting' || $fax_status == 'trying' || $fax_status == 'busy') {
//get the call center settings //get the call center settings
$retry_limit = $_SESSION['fax_queue']['retry_limit']['numeric']; $retry_limit = $_SESSION['fax_queue']['retry_limit']['numeric'];
//$retry_interval = $_SESSION['fax_queue']['retry_interval']['numeric']; //$retry_interval = $_SESSION['fax_queue']['retry_interval']['numeric'];
@ -96,7 +175,7 @@
if (strlen($fax_retry_count) == 0) { if (strlen($fax_retry_count) == 0) {
$fax_retry_count = 0; $fax_retry_count = 0;
} }
else { elseif ($fax_status != 'busy') {
$fax_retry_count = $fax_retry_count + 1; $fax_retry_count = $fax_retry_count + 1;
} }
@ -168,12 +247,12 @@
//$dial_string .= "fax_retry_sleep=180" . ","; //$dial_string .= "fax_retry_sleep=180" . ",";
$dial_string .= "fax_verbose=true" . ","; $dial_string .= "fax_verbose=true" . ",";
//$dial_string .= "fax_use_ecm=off" . ","; //$dial_string .= "fax_use_ecm=off" . ",";
$dial_string .= "api_hangup_hook='lua app/fax/resources/scripts/hangup_rx.lua'"; $dial_string .= "api_hangup_hook='lua app/fax/resources/scripts/hangup_tx.lua'";
$fax_command = "originate {" . $dial_string . "}" . $fax_uri." &txfax('".$fax_file."')"; $fax_command = "originate {" . $dial_string . "}" . $fax_uri." &txfax('".$fax_file."')";
//echo $fax_command."\n"; //echo $fax_command."\n";
//connect to event socket and send the command //connect to event socket and send the command
if (file_exists($fax_file)) { if ($fax_status != 'failed' && file_exists($fax_file)) {
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']); $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp) { if ($fp) {
$response = event_socket_request($fp, "api " . $fax_command); $response = event_socket_request($fp, "api " . $fax_command);
@ -187,16 +266,12 @@
else { else {
echo "fax file missing: ".$fax_file."\n"; echo "fax file missing: ".$fax_file."\n";
} }
}
//update the database to say status to trying and set the command //update the database to say status to trying and set the command
$array['fax_queue'][0]['fax_queue_uuid'] = $fax_queue_uuid; $array['fax_queue'][0]['fax_queue_uuid'] = $fax_queue_uuid;
$array['fax_queue'][0]['domain_uuid'] = $domain_uuid; $array['fax_queue'][0]['domain_uuid'] = $domain_uuid;
if ($fax_retry_count >= $retry_limit) { $array['fax_queue'][0]['fax_status'] = $fax_status;
$array['fax_queue'][0]['fax_status'] = 'failed';
}
else {
$array['fax_queue'][0]['fax_status'] = 'trying';
}
$array['fax_queue'][0]['fax_retry_count'] = $fax_retry_count; $array['fax_queue'][0]['fax_retry_count'] = $fax_retry_count;
$array['fax_queue'][0]['fax_retry_date'] = 'now()'; $array['fax_queue'][0]['fax_retry_date'] = 'now()';
$array['fax_queue'][0]['fax_command'] = $fax_command; $array['fax_queue'][0]['fax_command'] = $fax_command;
@ -214,6 +289,120 @@
//remove temporary permissions //remove temporary permissions
$p->delete('fax_queue_edit', 'temp'); $p->delete('fax_queue_edit', 'temp');
//send the email
if (in_array($fax_status, array('sent', 'failed')) && strlen($fax_email_address) > 0 && file_exists($fax_file)) {
//get the language code
$language_code = $_SESSION['domain']['language']['code'];
//get the template subcategory
if (isset($fax_relay) && $fax_relay == 'true') {
$template_subcategory = 'relay';
}
else {
$template_subcategory = 'inbound';
}
//get the email template from the database
if (isset($fax_email) && strlen($fax_email) > 0) {
$sql = "select template_subject, template_body from v_email_templates ";
$sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
$sql .= "and template_language = :template_language ";
$sql .= "and template_category = :template_category ";
$sql .= "and template_type = :template_type ";
$sql .= "and template_enabled = 'true' ";
$parameters['domain_uuid'] = $domain_uuid;
$parameters['template_language'] = $language_code;
$parameters['template_category'] = 'fax';
$parameters['template_type'] = 'html';
$database = new database;
$fax_templates = $database->select($sql, $parameters, 'all');
unset($sql, $parameters);
}
//determine the template category: fail_busy, fail_default, fail_invalid, inbound, relay, success_default
switch ($fax_status) {
case 'sent':
$template_subcategory = 'success_default';
break;
case 'failed':
$template_subcategory = 'fail_default';
break;
case 'busy':
$template_subcategory = 'fail_busy';
break;
}
//determine the email template to use
if (is_array($fax_templates)) {
foreach($fax_templates as $row) {
if ($row['template_subcategory'] == $template_subcategory) {
$email_subject = $row['template_subject'];
$email_body = $row['template_body'];
}
}
}
//replace variables in email subject
$email_subject = str_replace('${domain_name}', $domain_uuid, $email_subject);
$email_subject = str_replace('${fax_file_name}', $fax_file_name, $email_subject);
$email_subject = str_replace('${fax_extension}', $fax_extension, $email_subject);
$email_subject = str_replace('${fax_messages}', $fax_messages, $email_subject);
$email_subject = str_replace('${fax_file_warning}', $fax_file_warning, $email_subject);
$email_subject = str_replace('${fax_subject_tag}', $fax_email_inbound_subject_tag, $email_subject);
//replace variables in email body
$email_body = str_replace('${domain_name}', $domain_uuid, $email_body);
$email_body = str_replace('${fax_file_name}', $fax_file_name, $email_body);
$email_body = str_replace('${fax_extension}', $fax_extension, $email_body);
$email_body = str_replace('${fax_messages}', $fax_messages, $email_body);
$email_body = str_replace('${fax_file_warning}', $fax_file_warning, $email_body);
$email_body = str_replace('${fax_subject_tag}', $fax_email_inbound_subject_tag, $email_body);
//debug info
//echo "<hr />\n";
//echo "email_address ".$fax_email."<br />\n";
//echo "email_subject ".$email_subject."<br />\n";
//echo "email_body ".$email_body."<br />\n";
//echo "<hr />\n";
//send the email
if (isset($fax_email) && strlen($fax_email) > 0) {
//add the attachment
if (strlen($fax_file_name) > 0) {
$email_attachments[0]['type'] = 'file';
if ($pdf_file && file_exists($pdf_file)) {
$email_attachments[0]['name'] = $fax_file_name.'.pdf';
$email_attachments[0]['value'] = $pdf_file;
}
else {
$email_attachments[0]['name'] = $fax_file_name.'.tif';
$email_attachments[0]['value'] = $fax_file;
}
}
//$email_response = send_email($email_address, $email_subject, $email_body);
$email_response = !send_email($fax_email, $email_subject, $email_body, $email_error, $email_from_address, $email_from_name, null, null, $email_attachments) ? false : true;
}
//output to the log
echo "email_from: ".$email_from."\n";
echo "email_from_name: ".$email_from_address."\n";
echo "email_subject: $email_subject\n";
//send the email
if ($email_response) {
echo "Mailer Error";
$email_status=$mail;
}
else {
echo "Message sent!";
$email_status="ok";
}
}
//wait for a few seconds //wait for a few seconds
//sleep(1); //sleep(1);