Voicemail-to-Email: Backup email to database if sending fails, better detection of ContentType.

This commit is contained in:
Nate Jones
2014-07-08 06:38:23 +00:00
parent ed19a6cbac
commit b2051ca910
5 changed files with 131 additions and 20 deletions

View File

@@ -74,6 +74,53 @@
$apps[$x]['db'][$y]['fields'][$z]['type'] = "text";
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
//schema details
$y = 1; //table array index
$z = 0; //field array index
$apps[$x]['db'][$y]['table'] = "v_emails";
$apps[$x]['db'][$y]['fields'][$z]['name'] = "email_uuid";
$apps[$x]['db'][$y]['fields'][$z]['type']['pgsql'] = "uuid";
$apps[$x]['db'][$y]['fields'][$z]['type']['sqlite'] = "text";
$apps[$x]['db'][$y]['fields'][$z]['type']['mysql'] = "char(36)";
$apps[$x]['db'][$y]['fields'][$z]['key']['type'] = "primary";
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
$z++;
$apps[$x]['db'][$y]['fields'][$z]['name'] = "call_uuid";
$apps[$x]['db'][$y]['fields'][$z]['type']['pgsql'] = "uuid";
$apps[$x]['db'][$y]['fields'][$z]['type']['sqlite'] = "text";
$apps[$x]['db'][$y]['fields'][$z]['type']['mysql'] = "char(36)";
$apps[$x]['db'][$y]['fields'][$z]['key']['type'] = "foreign";
$apps[$x]['db'][$y]['fields'][$z]['key']['reference']['table'] = "v_xml_cdr";
$apps[$x]['db'][$y]['fields'][$z]['key']['reference']['field'] = "uuid";
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
$z++;
$apps[$x]['db'][$y]['fields'][$z]['name'] = "domain_uuid";
$apps[$x]['db'][$y]['fields'][$z]['type']['pgsql'] = "uuid";
$apps[$x]['db'][$y]['fields'][$z]['type']['sqlite'] = "text";
$apps[$x]['db'][$y]['fields'][$z]['type']['mysql'] = "char(36)";
$apps[$x]['db'][$y]['fields'][$z]['key']['type'] = "foreign";
$apps[$x]['db'][$y]['fields'][$z]['key']['reference']['table'] = "v_domains";
$apps[$x]['db'][$y]['fields'][$z]['key']['reference']['field'] = "domain_uuid";
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
$z++;
$apps[$x]['db'][$y]['fields'][$z]['name'] = "sent_date";
$apps[$x]['db'][$y]['fields'][$z]['type']['pgsql'] = "timestamp";
$apps[$x]['db'][$y]['fields'][$z]['type']['sqlite'] = "date";
$apps[$x]['db'][$y]['fields'][$z]['type']['mysql'] = "timestamp";
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
$z++;
$apps[$x]['db'][$y]['fields'][$z]['name'] = "type";
$apps[$x]['db'][$y]['fields'][$z]['type'] = "text";
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
$z++;
$apps[$x]['db'][$y]['fields'][$z]['name'] = "status";
$apps[$x]['db'][$y]['fields'][$z]['type'] = "text";
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
$z++;
$apps[$x]['db'][$y]['fields'][$z]['name'] = "email";
$apps[$x]['db'][$y]['fields'][$z]['type'] = "text";
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
/*
$y = 0; //table array index
$z = 0; //field array index

View File

@@ -1,7 +1,9 @@
<html>
<font face="arial">
<b>Message From "${caller_id_name}" <a href="tel:${caller_id_number}">${caller_id_number}</a></b><br/>
<hr noshade="noshade" size="1"/>
Created: ${message_date}<br/>
Duration: ${message_duration}<br/>
Account: ${account}@${domain_name}<br/>
</font>
</font>
</html>

View File

@@ -1,7 +1,9 @@
<html>
<font face="arial">
<b>Message From "${caller_id_name}" <a href="tel:${caller_id_number}">${caller_id_number}</a></b><br/>
<hr noshade="noshade" size="1"/>
Created: ${message_date}<br/>
Duration: ${message_duration}<br/>
Account: ${account}@${domain_name}<br/>
</font>
</font>
</html>

View File

@@ -36,7 +36,7 @@
--convert_ext (optional) to replace the file's extension
--Example
--luarun email.lua to@domain.com from@domain.com 'subject' 'body'
--luarun email.lua to@domain.com from@domain.com 'headers' 'subject' 'body'
--get the argv values
script_name = argv[0];

View File

@@ -95,21 +95,18 @@
//get the body
$body = '';
foreach($decoded[0]["Parts"] as $row) {
$content_type = $row['Headers']['content-type:'];
if (substr($content_type, 0, 21) == "multipart/alternative") {
$content_type = $row["Parts"][0]["Headers"]["content-type:"];
if (substr($content_type, 0, 9) == "text/html") { $body = $row["Parts"][0]["Body"]; }
if (substr($content_type, 0, 10) == "text/plain") { $body_plain = $row["Parts"][0]["Body"]; }
$content_type = $row["Parts"][1]["Headers"]["content-type:"];
if (substr($content_type, 0, 9) == "text/html") { $body = $row["Parts"][1]["Body"]; }
if (substr($content_type, 0, 10) == "text/plain") { $body_plain = $row["Parts"][1]["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"]; }
}
else {
$content_type_array = explode(";", $content_type);
if ($content_type_array[0] == "text/plain") {
$body = $row["Body"];
}
}
else {
$content_type_array = explode(";", $content_type);
if ($content_type_array[0] == "text/html" || $content_type_array[0] == "text/plain") {
$body = $row["Body"];
}
}
}
@@ -256,12 +253,44 @@
}
//add the body to the email
$mail->AltBody = $body_plain; // optional, comment out and test
$mail->MsgHTML($body);
if (substr($body, 0, 5) == "<html") {
$mail->ContentType = "text/html";
$mail->Body = $body;
}
else {
$mail->Body = ($body != '') ? $body : $body_plain;
$mail->AltBody = $body_plain;
}
//send the email
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
$mailer_error = $mail->ErrorInfo;
echo "Mailer Error: ".$mailer_error."\n\n";
// log/store message in database for review
$email_uuid = uuid();
$sql = "insert into v_emails ( ";
$sql .= "email_uuid, ";
$sql .= "call_uuid, ";
$sql .= "domain_uuid, ";
$sql .= "sent_date, ";
$sql .= "type, ";
$sql .= "status, ";
$sql .= "email ";
$sql .= ") values ( ";
$sql .= "'".$email_uuid."', ";
$sql .= "'".$headers["X-FusionPBX-Call-UUID"]."', ";
$sql .= "'".$headers["X-FusionPBX-Domain-UUID"]."', ";
$sql .= "now(),";
$sql .= "'".$headers["X-FusionPBX-Email-Type"]."', ";
$sql .= "'failed', ";
$sql .= "'".str_replace("'", "''", $msg)."' ";
$sql .= ") ";
$db->exec(check_sql($sql));
unset($sql);
echo "Retained in v_emails as email_uuid = ".$email_uuid."\n";
}
else {
echo "Message sent!";
@@ -276,4 +305,35 @@
fwrite($fp, $content);
fclose($fp);
/********************************************************************************************
// save in /tmp as eml file
$fp = fopen(sys_get_temp_dir()."/email.eml", "w");
ob_end_clean();
ob_start();
$sql = "select email from v_emails where email_uuid = '".$email_uuid."'";
$prep_statement = $db->prepare($sql);
if ($prep_statement) {
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach ($result as &$row) {
echo $row["email"];
break;
}
}
unset($sql, $prep_statement, $result);
$content = ob_get_contents(); //get the output from the buffer
$content = str_replace("<br />", "", $content);
ob_end_clean(); //clean the buffer
fwrite($fp, $content);
fclose($fp);
*/
?>