소스 검색

messages-to-email

Anthony Ricci 7 달 전
부모
커밋
623feb4b1a
2개의 변경된 파일144개의 추가작업 그리고 10개의 파일을 삭제
  1. 74 4
      app_defaults.php
  2. 70 6
      index.php

+ 74 - 4
app_defaults.php

@@ -1,9 +1,79 @@
 <?php
 
-//process this code online once
-if ($domains_processed == 1) {
+//add fax email templates
+	if ($domains_processed == 1) {
 
+		//build the array
+		$x = 0;
+		$array['email_templates'][$x]['email_template_uuid'] = 'f91bbd91-46a6-4805-9fa3-e622bbb22989';
+		$array['email_templates'][$x]['template_language'] = 'en-us';
+		$array['email_templates'][$x]['template_category'] = 'message';
+		$array['email_templates'][$x]['template_subcategory'] = 'inbound';
+		$array['email_templates'][$x]['template_subject'] = "New message received from \${message_from}";
+		$array['email_templates'][$x]['template_body'] = "<html>\n";
+		$array['email_templates'][$x]['template_body'] .= "<body>\n";
+		$array['email_templates'][$x]['template_body'] .= "Message from <a href='tel:\${message_from}'>\${message_from}</a><br><br>\n";
+		$array['email_templates'][$x]['template_body'] .= "To: \${message_to}<br>\n";
+		$array['email_templates'][$x]['template_body'] .= "Received: \${message_date}<br>\n";
+		$array['email_templates'][$x]['template_body'] .= "Message: \${message_text}<br>\n";
+		$array['email_templates'][$x]['template_body'] .= "</body>\n";
+		$array['email_templates'][$x]['template_body'] .= "</html>\n";
+		$array['email_templates'][$x]['template_type'] = "html";
+		$array['email_templates'][$x]['template_enabled'] = "true";
+		$x++;
 
-}
+		//build array of email template uuids
+		foreach ($array['email_templates'] as $row) {
+			if (is_uuid($row['email_template_uuid'])) {
+				$uuids[] = $row['email_template_uuid'];
+			}
+		}
 
-?>
+		//add the email templates to the database
+		if (!empty($uuids)) {
+			$sql = "select * from v_email_templates where ";
+			foreach ($uuids as $index => $uuid) {
+				$sql_where[] = "email_template_uuid = :email_template_uuid_".$index;
+				$parameters['email_template_uuid_'.$index] = $uuid;
+			}
+			$sql .= implode(' or ', $sql_where);
+			$email_templates = $database->select($sql, $parameters, 'all');
+			unset($sql, $sql_where, $parameters);
+
+			//remove templates that already exist from the array
+			foreach ($array['email_templates'] as $index => $row) {
+				if (is_array($email_templates) && @sizeof($email_templates) != 0) {
+					foreach($email_templates as $email_template) {
+						if ($row['email_template_uuid'] == $email_template['email_template_uuid']) {
+							unset($array['email_templates'][$index]);
+						}
+					}
+				}
+			}
+			unset($email_templates, $index);
+		}
+
+		//add the missing email templates
+		if (!empty($array['email_templates'])) {
+			//add the temporary permission
+			$p = new permissions;
+			$p->add("email_template_add", 'temp');
+			$p->add("email_template_edit", 'temp');
+
+			//save the data
+			$database->app_name = 'email_templates';
+			$database->app_uuid = '8173e738-2523-46d5-8943-13883befd2fd';
+			$database->save($array);
+			//$message = $database->message;
+
+			//remove the temporary permission
+			$p->delete("email_template_add", 'temp');
+			$p->delete("email_template_edit", 'temp');
+		}
+
+		//remove the array
+		unset($array);
+
+	}
+
+?>

+ 70 - 6
index.php

@@ -13,13 +13,11 @@
 //connect to the database
 	$database = database::new();
 
+//initialize database and settings
+	$settings = new settings(['database' => $database, $_SESSION['domain_uuid'] ?? '']);
+
 //debug
-	if ($_SESSION['message']['debug']['boolean'] == 'true') {
-		$debug = true;
-	}
-	else {
-		$debug = false;
-	}
+	$debug = $settings->get('message','debug', false);
 
 //log file
 	$log_file = '/tmp/message.log';
@@ -425,6 +423,8 @@ if (count($message_content) == 3) {
 	if ($debug) {
 		file_put_contents($log_file, print_r($row, true)."\n", FILE_APPEND);
 	}
+	//check if message to email is enabled
+	$destination_email_enabled = $row['destination_email'];
 	unset($sql, $parameters, $row);
 
 //get the contact uuid
@@ -533,6 +533,7 @@ if (count($message_content) == 3) {
 
 			//build the array for the media
 			if ($message_media_type !== 'xml' && strlen($message_media_url) > 0) {
+				//build the message media
 				$array['message_media'][$index]['message_media_uuid'] = uuid();
 				$array['message_media'][$index]['message_uuid'] = $message_uuid;
 				$array['message_media'][$index]['domain_uuid'] = $domain_uuid;
@@ -544,6 +545,13 @@ if (count($message_content) == 3) {
 
 				$array['message_media'][$index]['message_media_url'] = $message_media_url;
 				$array['message_media'][$index]['message_media_content'] = base64_encode(url_get_contents($message_media_url));
+
+				//get email attachments
+				if($destination_email_enabled == 'true'){
+					$email_attachments[$index]['base64'] = $array['message_media'][$index]['message_media_content'];
+					$email_attachments[$index]['name'] = $message_media_name;
+					$email_attachments[$index]['type'] = $message_media_type;
+				}
 			}
 		}
 	}
@@ -570,6 +578,7 @@ if (count($message_content) == 3) {
 
 		//build the array for the media
 		if (!empty($message_media_url) && strlen($message_media_url) > 0 && $message_media_type !== 'xml') {
+			//build the message media array
 			$index = 0;
 			$array['message_media'][$index]['message_media_uuid'] = uuid();
 			$array['message_media'][$index]['message_uuid'] = $message_uuid;
@@ -578,6 +587,13 @@ if (count($message_content) == 3) {
 			$array['message_media'][$index]['message_media_type'] = $message_media_type;
 			$array['message_media'][$index]['message_media_url'] = $message_media_url;
 			$array['message_media'][$index]['message_media_content'] = base64_encode(url_get_contents($message_media_url));
+
+			//prepare the email attachments array
+			if($destination_email_enabled == 'true'){
+				$email_attachments[$index]['base64'] = $array['message_media'][$index]['message_media_content'];
+				$email_attachments[$index]['name'] = $message_media_name;
+				$email_attachments[$index]['type'] = $message_media_type;
+			}
 		}
 	}
 	//if ($debug) {
@@ -668,6 +684,54 @@ if (count($message_content) == 3) {
 		}
 	}
 
+	if ($destination_email_enabled == 'true') {
+		//get the email template from the database - Category: message; Subcategory:new
+		$sql = "select template_subcategory, 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_subcategory = :template_subcategory ";
+		$sql .= "and template_enabled = 'true' ";
+		$parameters['domain_uuid'] = $domain_uuid;
+		$parameters['template_language'] = $settings->get('domain','language');
+		$parameters['template_category'] = 'message';
+		$parameters['template_subcategory'] = 'inbound';
+		$message_inbound_template = $database->select($sql, $parameters, 'row');
+		unset($sql, $parameters);
+
+		$email_subject = $message_inbound_template['template_subject'] ?? "";
+		$email_body = $message_inbound_template['template_body']  ?? "";
+
+		//add placeholders as needed
+		$email_subject = str_replace('${message_to}', $message_to, $email_subject);
+		$email_subject = str_replace('${message_from}', $message_from, $email_subject);
+
+		$email_body = str_replace('${message_to}', $message_to, $email_body);
+		$email_body = str_replace('${message_from}', $message_from, $email_body);
+		$email_body = str_replace('${message_text}', $message_content, $email_body);
+
+		$time_zone = $settings->get('domain','time_zone');
+		$dt = new DateTime("now", new DateTimeZone($time_zone)); 
+		$email_body = str_replace('${message_date}', $dt->format('m/d/Y, H:i:s').' '.$time_zone, $email_body);
+
+		$sql = "select user_email from v_users ";
+		$sql .= "where user_uuid = :user_uuid";
+		$parameters['user_uuid'] = $user_uuid;
+		$user_email = $database->select($sql, $parameters, 'column');
+		unset($sql, $parameters);
+
+		$email = new email;
+		$email->domain_uuid = $domain_uuid;
+		$email->recipients = $user_email; 
+		$email->subject = $email_subject; 
+		$email->body = $email_body; 
+		$email->from_address = $settings->get('email','smtp_from');
+		$email->from_name = $settings->get('email','smtp_from_name');
+		$email->debug_level = 3;
+		$email->attachments = $email_attachments;
+		$email->send();
+	}
+
 //set the file
 	//$file = '/tmp/sms.txt';