Use the send_email function to send fax to email.

This commit is contained in:
FusionPBX 2022-02-04 16:01:27 -07:00 committed by GitHub
parent 18512a80c1
commit 4f6e5da0af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 69 additions and 135 deletions

View File

@ -17,7 +17,7 @@
The Initial Developer of the Original Code is The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2018 Portions created by the Initial Developer are Copyright (C) 2008-2022
the Initial Developer. All Rights Reserved. the Initial Developer. All Rights Reserved.
Contributor(s): Contributor(s):
@ -388,64 +388,16 @@ if (!function_exists('fax_split_dtmf')) {
unset($sql, $parameters, $result); unset($sql, $parameters, $result);
//prepare smtp server settings //prepare smtp server settings
// load default smtp settings $email_from = $_SESSION['email']['smtp_from']['text'];
$smtp['method'] = $_SESSION['email']['smtp_method']['text']; $email_from_name = $_SESSION['email']['smtp_from_name']['text'];
$smtp['host'] = (strlen($_SESSION['email']['smtp_host']['text'])?$_SESSION['email']['smtp_host']['text']:'127.0.0.1');
if (isset($_SESSION['email']['smtp_port'])) {
$smtp['port'] = (int)$_SESSION['email']['smtp_port']['numeric'];
}
else {
$smtp['port'] = 0;
}
$smtp['secure'] = $_SESSION['email']['smtp_secure']['text'];
$smtp['auth'] = $_SESSION['email']['smtp_auth']['text'];
$smtp['username'] = $_SESSION['email']['smtp_username']['text'];
$smtp['password'] = $_SESSION['email']['smtp_password']['text'];
$smtp['from'] = $_SESSION['email']['smtp_from']['text'];
$smtp['from_name'] = $_SESSION['email']['smtp_from_name']['text'];
if (isset($_SESSION['fax']['smtp_from']['text']) && strlen($_SESSION['fax']['smtp_from']['text']) > 0) { if (isset($_SESSION['fax']['smtp_from']['text']) && strlen($_SESSION['fax']['smtp_from']['text']) > 0) {
$smtp['from'] = $_SESSION['fax']['smtp_from']['text']; $email_from = $_SESSION['fax']['smtp_from']['text'];
} }
if (isset($_SESSION['fax']['smtp_from_name']['text']) && strlen($_SESSION['fax']['smtp_from_name']['text']) > 0) { if (isset($_SESSION['fax']['smtp_from_name']['text']) && strlen($_SESSION['fax']['smtp_from_name']['text']) > 0) {
$smtp['from_name'] = $_SESSION['fax']['smtp_from_name']['text']; $email_from_name = $_SESSION['fax']['smtp_from_name']['text'];
} }
// overwrite with domain-specific smtp server settings, if any //get the fax settings from the database
if (is_uuid($domain_uuid)) {
$sql = "select ";
$sql .= "domain_setting_subcategory, ";
$sql .= "domain_setting_value ";
$sql .= "from v_domain_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and ( ";
$sql .= "domain_setting_category = 'email' ";
$sql .= "or domain_setting_category = 'fax' ";
$sql .= ") ";
$sql .= "and domain_setting_name = 'text' ";
$sql .= "and domain_setting_enabled = 'true' ";
$parameters['domain_name'] = $domain_name;
$database = new database;
$result = $database->select($sql, $parameters, 'all');
if (is_array($result) && @sizeof($result) != 0) {
foreach ($result as $row) {
if ($row['domain_setting_value'] != '') {
$smtp[str_replace('smtp_','',$row["domain_setting_subcategory"])] = $row['domain_setting_value'];
}
}
}
unset($sql, $parameters, $result, $row);
}
// value adjustments
$smtp['method'] = ($smtp['method'] == '') ? 'smtp' : $smtp['method'];
$smtp['auth'] = ($smtp['auth'] == "true") ? true : false;
$smtp['password'] = ($smtp['password'] != '') ? $smtp['password'] : null;
$smtp['secure'] = ($smtp['secure'] != "none") ? $smtp['secure'] : null;
$smtp['username'] = ($smtp['username'] != '') ? $smtp['username'] : null;
//get the fax details from the database
$sql = "select * from v_fax "; $sql = "select * from v_fax ";
$sql .= "where domain_uuid = :domain_uuid "; $sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and fax_extension = :fax_extension "; $sql .= "and fax_extension = :fax_extension ";
@ -583,7 +535,7 @@ if (!function_exists('fax_split_dtmf')) {
fclose($fp); fclose($fp);
} }
} }
else{ else {
$wav_file = ''; $wav_file = '';
$response = fax_enqueue($fax_uuid, $fax_file, $wav_file, $mailto_address, $fax_uri, $fax_dtmf, $dial_string); $response = fax_enqueue($fax_uuid, $fax_file, $wav_file, $mailto_address, $fax_uri, $fax_dtmf, $dial_string);
} }
@ -592,101 +544,83 @@ if (!function_exists('fax_split_dtmf')) {
//send the email //send the email
if (strlen($fax_email) > 0 && file_exists($fax_file)) { if (strlen($fax_email) > 0 && file_exists($fax_file)) {
//prepare the message
$tmp_subject = (($fax_email_inbound_subject_tag != '') ? "[".$fax_email_inbound_subject_tag."]" : "Fax Received").": ".$fax_file_name;
$tmp_text_html = "<br><strong>Fax Received</strong><br><br>"; //get the language code
$tmp_text_html .= "Name: ".$fax_file_name."<br>"; $language_code = $_SESSION['domain']['language']['code'];
$tmp_text_html .= "Extension: ".$fax_extension."<br>";
$tmp_text_html .= "Messages: ".$fax_messages."<br>"; //get the template subcategory
$tmp_text_html .= $fax_file_warning."<br>";
if ($fax_relay == 'yes') { if ($fax_relay == 'yes') {
$tmp_subject = "Fax Received for Relay: ".$fax_file_name; $template_subcategory = 'relay';
$tmp_text_html .= "<br>This message arrived successfully from your fax machine, and has been queued for outbound fax delivery. You will be notified later as to the success or failure of this fax.<br>";
}
$tmp_text_plain = strip_tags(str_replace("<br>", "\n", $tmp_text_html));
//prepare the mail object
$mail = new PHPMailer();
if (isset($smtp['method'])) {
switch($smtp['method']) {
case 'sendmail': $mail->IsSendmail(); break;
case 'qmail': $mail->IsQmail(); break;
case 'mail': $mail->IsMail(); break;
default: $mail->IsSMTP(); break;
}
} }
else { else {
$mail->IsSMTP(); // set mailer to use SMTP $template_subcategory = 'inbound';
} }
//optionally skip certificate validation //get the email template from the database
if (isset($_SESSION['email']['smtp_validate_certificate'])) { if (isset($email_address) && strlen($email_address) > 0) {
if ($_SESSION['email']['smtp_validate_certificate']['boolean'] == "false") { $sql = "select template_subject, template_body from v_email_templates ";
$sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
// this works around TLS certificate problems e.g. self-signed certificates $sql .= "and template_language = :template_language ";
$mail->SMTPOptions = array( $sql .= "and template_category = :template_category ";
'ssl' => array( $sql .= "and template_subcategory = :template_subcategory ";
'verify_peer' => false, $sql .= "and template_type = :template_type ";
'verify_peer_name' => false, $sql .= "and template_enabled = 'true' ";
'allow_self_signed' => true $parameters['domain_uuid'] = $domain_uuid;
) $parameters['template_language'] = $language_code;
); $parameters['template_category'] = 'fax';
$parameters['template_subcategory'] = $template_subcategory;
$parameters['template_type'] = 'html';
$database = new database;
$row = $database->select($sql, $parameters, 'row');
if (is_array($row)) {
$email_subject = $row['template_subject'];
$email_body = $row['template_body'];
} }
unset($sql, $parameters);
} }
if ($smtp['auth'] == "true") { //replace variables in email subject
$mail->SMTPAuth = $smtp['auth']; // turn on/off SMTP authentication $email_subject = str_replace('${domain_name}', $_SESSION['domain_name'], $email_subject);
} $email_subject = str_replace('${fax_file_name}', $fax_file_name, $email_subject);
$mail->Host = $smtp['host']; $email_subject = str_replace('${fax_extension}', $fax_extension, $email_subject);
if (strlen($smtp['port']) > 0) { $email_subject = str_replace('${fax_messages}', $fax_messages, $email_subject);
$mail->Port = $smtp['port']; $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);
if (strlen($smtp['secure']) > 0 && $smtp['secure'] != 'none') {
$mail->SMTPSecure = $smtp['secure'];
}
if ($smtp['username'] != '') {
$mail->Username = $smtp['username'];
$mail->Password = $smtp['password'];
}
$mail->SMTPDebug = 2;
$mail->From = $smtp['from'];
$mail->FromName = $smtp['from_name'];
$mail->Subject = $tmp_subject;
$mail->AltBody = $tmp_text_plain;
$mail->MsgHTML($tmp_text_html);
$tmp_to = $fax_email; //replace variables in email body
$tmp_to = str_replace(";", ",", $tmp_to); $email_body = str_replace('${domain_name}', $_SESSION['domain_name'], $email_body);
$tmp_to_array = explode(",", $tmp_to); $email_body = str_replace('${fax_file_name}', $fax_file_name, $email_body);
foreach($tmp_to_array as $tmp_to_row) { $email_body = str_replace('${fax_extension}', $fax_extension, $email_body);
if (strlen($tmp_to_row) > 0) { $email_body = str_replace('${fax_messages}', $fax_messages, $email_body);
echo "tmp_to_row: $tmp_to_row\n"; $email_body = str_replace('${fax_file_warning}', $fax_file_warning, $email_body);
$mail->AddAddress(trim($tmp_to_row)); $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
$email_attachments[0]['type'] = 'file';
$email_attachments[0]['name'] = $fax_file_name;
$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, 3, 3, $email_attachments) ? false : true;
} }
//output to the log //output to the log
echo "smtp_host: ".$smtp['host']."\n"; echo "email_from: ".$email_from."\n";
echo "smtp_from: ".$smtp['from']."\n"; echo "email_from_name: ".$email_from_address."\n";
echo "smtp_from_name: ".$smtp['from_name']."\n"; echo "email_subject: $email_subject\n";
echo "tmp_subject: $tmp_subject\n";
//add the attachments
if (strlen($fax_file_name) > 0) {
if ($pdf_file && file_exists($pdf_file)) {
$mail->AddAttachment($pdf_file); // pdf attachment
}
else {
$mail->AddAttachment($fax_file); // tif attachment
}
//$filename='fax.tif'; $encoding = "base64"; $type = "image/tif";
//$mail->AddStringAttachment(base64_decode($strfax),$filename,$encoding,$type);
}
//send the email //send the email
if (!$mail->Send()) { if ($email_response) {
echo "Mailer Error: " . $mail->ErrorInfo; echo "Mailer Error";
$email_status=$mail; $email_status=$mail;
} }
else { else {