Use the email class.
This commit is contained in:
parent
1585a42256
commit
8c84c9c837
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
The Initial Developer of the Original Code is
|
||||
Mark J Crane <markjcrane@fusionpbx.com>
|
||||
Portions created by the Initial Developer are Copyright (C) 2008-2021
|
||||
Portions created by the Initial Developer are Copyright (C) 2008-2022
|
||||
the Initial Developer. All Rights Reserved.
|
||||
|
||||
Contributor(s):
|
||||
|
|
@ -80,292 +80,37 @@
|
|||
//echo "Message: \n".$msg."\n";
|
||||
|
||||
//includes
|
||||
require_once('resources/pop3/mime_parser.php');
|
||||
require_once('resources/pop3/rfc822_addresses.php');
|
||||
if (file_exists($_SERVER["PROJECT_ROOT"]."/app/emails/email_transcription.php")) {
|
||||
require_once($_SERVER["PROJECT_ROOT"]."/app/emails/email_transcription.php");
|
||||
}
|
||||
//require_once('resources/pop3/mime_parser.php');
|
||||
//require_once('resources/pop3/rfc822_addresses.php');
|
||||
//if (file_exists($_SERVER["PROJECT_ROOT"]."/app/emails/email_transcription.php")) {
|
||||
// require_once($_SERVER["PROJECT_ROOT"]."/app/emails/email_transcription.php");
|
||||
//}
|
||||
|
||||
//parse the email message
|
||||
$mime = new mime_parser_class;
|
||||
$mime->decode_bodies = 1;
|
||||
$parameters = array(
|
||||
//'File'=>$message_file,
|
||||
//parse the email
|
||||
$email = new email;
|
||||
$email->parse($msg);
|
||||
$email->debug_level = 0;
|
||||
$email->method = 'direct';
|
||||
$headers = $email->headers;
|
||||
$sent = $email->send();
|
||||
|
||||
// Read a message from a string instead of a file
|
||||
'Data' => $msg,
|
||||
|
||||
// Save the message body parts to a directory
|
||||
// 'SaveBody' => '/tmp',
|
||||
|
||||
// Do not retrieve or save message body parts
|
||||
// 'SkipBody' => 1,
|
||||
);
|
||||
$success = $mime->Decode($parameters, $decoded);
|
||||
unset($parameters);
|
||||
|
||||
if (!$success) {
|
||||
echo "MIME message decoding error: ".HtmlSpecialChars($mime->error)."\n";
|
||||
}
|
||||
else {
|
||||
//get the headers
|
||||
//print_r($decoded[0]);
|
||||
$headers = json_decode($decoded[0]["Headers"]["x-headers:"], true);
|
||||
$subject = $decoded[0]["Headers"]["subject:"];
|
||||
$from = $decoded[0]["Headers"]["from:"];
|
||||
$reply_to = $decoded[0]["Headers"]["reply-to:"];
|
||||
$to = $decoded[0]["Headers"]["to:"];
|
||||
$date = $decoded[0]["Headers"]["date:"];
|
||||
|
||||
//get the body
|
||||
$body = ''; //$parts_array["Parts"][0]["Headers"]["content-type:"];
|
||||
|
||||
//get the body
|
||||
$body = '';
|
||||
$content_type = $decoded[0]['Headers']['content-type:'];
|
||||
if (substr($content_type, 0, 15) == "multipart/mixed" || substr($content_type, 0, 21) == "multipart/alternative") {
|
||||
foreach ($decoded[0]["Parts"] as $row) {
|
||||
$body_content_type = $row["Headers"]["content-type:"];
|
||||
if (substr($body_content_type, 0, 9) == "text/html") { $body = $row["Body"]; }
|
||||
if (substr($body_content_type, 0, 10) == "text/plain") { $body_plain = $row["Body"]; $body = $body_plain; }
|
||||
}
|
||||
}
|
||||
else {
|
||||
$content_type_array = explode(";", $content_type);
|
||||
$body = $decoded[0]["Body"];
|
||||
//if ($content_type_array[0] == "text/html" || $content_type_array[0] == "text/plain") {
|
||||
// $body = $row["Body"];
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
//prepare smtp server settings
|
||||
//load default smtp settings
|
||||
if ($_SESSION['email']['smtp_hostname']['text'] != '') {
|
||||
$smtp['hostname'] = $_SESSION['email']['smtp_hostname']['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['voicemail']['smtp_from']) && strlen($_SESSION['voicemail']['smtp_from']['text']) > 0) {
|
||||
$smtp['from'] = $_SESSION['voicemail']['smtp_from']['text'];
|
||||
}
|
||||
if (isset($_SESSION['voicemail']['smtp_from_name']) && strlen($_SESSION['voicemail']['smtp_from_name']['text']) > 0) {
|
||||
$smtp['from_name'] = $_SESSION['voicemail']['smtp_from_name']['text'];
|
||||
}
|
||||
|
||||
//overwrite with domain-specific smtp server settings, if any
|
||||
if (is_uuid($headers["X-FusionPBX-Domain-UUID"])) {
|
||||
$sql = "select domain_setting_subcategory, domain_setting_value ";
|
||||
$sql .= "from v_domain_settings ";
|
||||
$sql .= "where domain_uuid = :domain_uuid ";
|
||||
$sql .= "and (domain_setting_category = 'email' or domain_setting_category = 'voicemail') ";
|
||||
$sql .= "and domain_setting_enabled = 'true' ";
|
||||
$parameters['domain_uuid'] = $headers["X-FusionPBX-Domain-UUID"];
|
||||
$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['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;
|
||||
//debug information
|
||||
/*
|
||||
echo "<pre>\n";
|
||||
echo "recipients ".$email->recipients."\n";
|
||||
echo "subject ".$email->subject."\n";
|
||||
echo "body ".$email->body."\n";
|
||||
echo "from_address ".$email->from_address."\n";
|
||||
echo "from_name ".$email->from_name."\n";
|
||||
echo "headers ".print_r($email->headers)."\n";
|
||||
//echo "attachments ".print_r($email->attachments)."\n";
|
||||
echo "</pre>\n";
|
||||
exit;
|
||||
*/
|
||||
|
||||
//send the email
|
||||
include_once "resources/phpmailer/class.phpmailer.php";
|
||||
include_once "resources/phpmailer/class.smtp.php";
|
||||
|
||||
//create the mail object
|
||||
$mail = new PHPMailer();
|
||||
if (isset($_SESSION['email']['method'])) {
|
||||
switch ($_SESSION['email']['method']['text']) {
|
||||
case 'sendmail': $mail->IsSendmail(); break;
|
||||
case 'qmail': $mail->IsQmail(); break;
|
||||
case 'mail': $mail->IsMail(); break;
|
||||
default: $mail->IsSMTP(); break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$mail->IsSMTP();
|
||||
}
|
||||
|
||||
//optional bypass TLS certificate check e.g. for self-signed certificates
|
||||
if (isset($_SESSION['email']['smtp_validate_certificate'])) {
|
||||
if ($_SESSION['email']['smtp_validate_certificate']['boolean'] == "false") {
|
||||
// this is needed to work around TLS certificate problems
|
||||
$mail->SMTPOptions = array(
|
||||
'ssl' => array(
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false,
|
||||
'allow_self_signed' => true
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//smtp connection details
|
||||
$mail->SMTPAuth = $smtp['auth'];
|
||||
if (isset($smtp['hostname'])) {
|
||||
$mail->Hostname = $smtp['hostname'];
|
||||
}
|
||||
$mail->Host = $smtp['host'];
|
||||
if ($smtp['port']!=0) $mail->Port=$smtp['port'];
|
||||
if ($smtp['secure'] != '') {
|
||||
$mail->SMTPSecure = $smtp['secure'];
|
||||
}
|
||||
if ($smtp['auth']) {
|
||||
$mail->Username = $smtp['username'];
|
||||
$mail->Password = $smtp['password'];
|
||||
}
|
||||
$mail->SMTPDebug = 2;
|
||||
|
||||
//add information to the log
|
||||
if (sizeof($headers)>0) {
|
||||
foreach ($headers as $header => $value) {
|
||||
echo $header.": ".$value."\n";
|
||||
}
|
||||
}
|
||||
echo "Subject: ".$subject."\n";
|
||||
echo "From: ".$from."\n";
|
||||
echo "Reply-to: ".$reply_to."\n";
|
||||
echo "To: ".$to."\n";
|
||||
echo "Date: ".$date."\n";
|
||||
//echo "Body: ".$body."\n";
|
||||
|
||||
//add to, from, fromname, custom headers and subject to the email
|
||||
$mail->From = $smtp['from'] ;
|
||||
$mail->FromName = $smtp['from_name'];
|
||||
if (sizeof($headers)>0) {
|
||||
foreach ($headers as $header => $value) {
|
||||
$mail->addCustomHeader($header.": ".$value);
|
||||
}
|
||||
}
|
||||
$mail->Subject = $subject;
|
||||
|
||||
//add the reciepients
|
||||
$to = trim($to, "<>");
|
||||
$to = str_replace(" ", "", $to);
|
||||
$to = str_replace(";", ",", $to);
|
||||
$to_array = explode(",", $to);
|
||||
if (count($to_array) == 1) {
|
||||
$mail->AddAddress($to);
|
||||
}
|
||||
else {
|
||||
foreach ($to_array as $to_row) {
|
||||
if (strlen($to_row) > 0) {
|
||||
echo "Add Address: $to_row\n";
|
||||
if ($_SESSION['email']['address_type']['text'] == 'add_address') {
|
||||
$mail->AddAddress(trim($to_row));
|
||||
}
|
||||
elseif ($_SESSION['email']['address_type']['text'] == 'add_bcc') {
|
||||
$mail->AddBCC(trim($to_row));
|
||||
}
|
||||
else {
|
||||
$mail->AddAddress(trim($to_row));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//get the attachments and add to the email
|
||||
if ($success) {
|
||||
foreach ($decoded[0]["Parts"] as &$parts_array) {
|
||||
$content_type = $parts_array["Parts"][0]["Headers"]["content-type:"];
|
||||
//image/tiff;name="testfax.tif"
|
||||
//text/plain; charset=ISO-8859-1; format=flowed
|
||||
$content_transfer_encoding = $parts_array["Parts"][0]["Headers"]["content-transfer-encoding:"];
|
||||
//base64
|
||||
//7bit
|
||||
$content_disposition = $parts_array["Parts"][0]["Headers"]["content-disposition"];
|
||||
//inline;filename="testfax.tif"
|
||||
$file = $parts_array["FileName"];
|
||||
//testfax.tif
|
||||
$filedisposition = $parts_array["FileDisposition"];
|
||||
//inline
|
||||
$bodypart = $parts_array["BodyPart"];
|
||||
$bodylength = $parts_array["BodyLength"];
|
||||
|
||||
if (strlen($file) > 0) {
|
||||
//get the file information
|
||||
$file_ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||
$file_name = substr($file, 0, (strlen($file) - strlen($file_ext))-1 );
|
||||
$encoding = "base64"; //base64_decode
|
||||
|
||||
switch ($file_ext){
|
||||
case "wav":
|
||||
$mime_type = "audio/x-wav";
|
||||
break;
|
||||
case "mp3":
|
||||
$mime_type = "audio/x-mp3";
|
||||
break;
|
||||
case "pdf":
|
||||
$mime_type = "application/pdf";
|
||||
break;
|
||||
case "tif":
|
||||
$mime_type = "image/tiff";
|
||||
break;
|
||||
case "tiff":
|
||||
$mime_type = "image/tiff";
|
||||
break;
|
||||
default:
|
||||
$mime_type = "binary/octet-stream";
|
||||
break;
|
||||
}
|
||||
|
||||
//add an attachment
|
||||
$mail->AddStringAttachment($parts_array["Body"],$file,$encoding,$mime_type);
|
||||
if (function_exists('get_transcription')) {
|
||||
$attachments_array = $mail->GetAttachments();
|
||||
$transcription = get_transcription($attachments_array[0]);
|
||||
echo "Transcription: " . $transcription;
|
||||
}
|
||||
else {
|
||||
$transcription = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//add the body to the email
|
||||
$body_plain = remove_tags($body);
|
||||
//echo "body_plain = $body_plain\n";
|
||||
if ((substr($body, 0, 5) == "<html") || (substr($body, 0, 9) == "<!doctype")) {
|
||||
$mail->ContentType = "text/html";
|
||||
$mail->Body = $body."<br><br>".nl2br($transcription);
|
||||
$mail->AltBody = $body_plain."\n\n$transcription";
|
||||
$mail->isHTML(true);
|
||||
}
|
||||
else {
|
||||
//$mail->Body = ($body != '') ? $body : $body_plain;
|
||||
$mail->Body = $body_plain."\n\n$transcription";
|
||||
$mail->AltBody = $body_plain."\n\n$transcription";
|
||||
$mail->isHTML(false);
|
||||
}
|
||||
$mail->CharSet = "utf-8";
|
||||
|
||||
//send the email
|
||||
if (!$mail->Send()) {
|
||||
$mailer_error = $mail->ErrorInfo;
|
||||
echo "Mailer Error: ".$mailer_error."\n\n";
|
||||
if (!$sent) {
|
||||
echo "Mailer Error: ".$email_error."\n\n";
|
||||
|
||||
$call_uuid = $headers["X-FusionPBX-Call-UUID"];
|
||||
if ($resend == true) {
|
||||
|
|
@ -380,9 +125,13 @@
|
|||
if (is_uuid($call_uuid)) {
|
||||
$array['email_logs'][0]['call_uuid'] = $call_uuid;
|
||||
}
|
||||
$array['email_logs'][0]['domain_uuid'] = $headers["X-FusionPBX-Domain-UUID"];
|
||||
if (isset($headers["X-FusionPBX-Domain-UUID"])) {
|
||||
$array['email_logs'][0]['domain_uuid'] = $headers["X-FusionPBX-Domain-UUID"];
|
||||
}
|
||||
$array['email_logs'][0]['sent_date'] = 'now()';
|
||||
$array['email_logs'][0]['type'] = $headers["X-FusionPBX-Email-Type"];
|
||||
if (isset($headers["X-FusionPBX-Email-Type"])) {
|
||||
$array['email_logs'][0]['type'] = $headers["X-FusionPBX-Email-Type"];
|
||||
}
|
||||
$array['email_logs'][0]['status'] = 'failed';
|
||||
$array['email_logs'][0]['email'] = str_replace("'", "''", $msg);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue