diff --git a/app/email_queue/app_config.php b/app/email_queue/app_config.php index e03811271d..9eb4a5033f 100644 --- a/app/email_queue/app_config.php +++ b/app/email_queue/app_config.php @@ -219,6 +219,10 @@ $apps[$x]['db'][$y]['fields'][$z]['key']['reference']['table'] = 'v_email_queue'; $apps[$x]['db'][$y]['fields'][$z]['key']['reference']['field'] = 'email_queue_uuid'; $z++; + $apps[$x]['db'][$y]['fields'][$z]['name'] = 'email_attachment_mime_type'; + $apps[$x]['db'][$y]['fields'][$z]['type'] = 'text'; + $apps[$x]['db'][$y]['fields'][$z]['search_by'] = ''; + $z++; $apps[$x]['db'][$y]['fields'][$z]['name'] = 'email_attachment_type'; $apps[$x]['db'][$y]['fields'][$z]['type'] = 'text'; $apps[$x]['db'][$y]['fields'][$z]['search_by'] = ''; @@ -235,6 +239,10 @@ $apps[$x]['db'][$y]['fields'][$z]['type'] = 'text'; $apps[$x]['db'][$y]['fields'][$z]['search_by'] = ''; $z++; + $apps[$x]['db'][$y]['fields'][$z]['name'] = 'email_attachment_cid'; + $apps[$x]['db'][$y]['fields'][$z]['type'] = 'text'; + $apps[$x]['db'][$y]['fields'][$z]['search_by'] = ''; + $z++; $apps[$x]['db'][$y]['fields'][$z]['name'] = "insert_date"; $apps[$x]['db'][$y]['fields'][$z]['type']['pgsql'] = 'timestamptz'; $apps[$x]['db'][$y]['fields'][$z]['type']['sqlite'] = 'date'; diff --git a/app/scripts/resources/scripts/resources/functions/send_mail.lua b/app/scripts/resources/scripts/resources/functions/send_mail.lua index 6fb449a481..5312ad4de2 100644 --- a/app/scripts/resources/scripts/resources/functions/send_mail.lua +++ b/app/scripts/resources/scripts/resources/functions/send_mail.lua @@ -103,6 +103,29 @@ if (email_queue_enabled == 'true') then email_attachment_name = ''; email_attachment_base64 = ''; + --set the mime type + if (email_attachment_type == 'jpg' or email_attachment_type == 'peg') then --jpeg + email_attachment_mime_type = 'image/jpeg'; + elseif (email_attachment_type == 'gif') then + email_attachment_mime_type = 'image/gif'; + elseif (email_attachment_type == 'png') then + email_attachment_mime_type = 'image/png'; + elseif (email_attachment_type == 'pdf') then + email_attachment_mime_type = 'application/pdf'; + elseif (email_attachment_type == 'tif' or email_attachment_type == 'iff') then --tiff + email_attachment_mime_type = 'image/tiff'; + elseif (email_attachment_type == 'mp3') then + email_attachment_mime_type = 'audio/mpeg'; + elseif (email_attachment_type == 'wav') then + email_attachment_mime_type = 'audio/x-wav'; + elseif (email_attachment_type == 'pus') then --opus + email_attachment_mime_type = 'audio/opus'; + elseif (email_attachment_type == 'ogg') then + email_attachment_mime_type = 'audio/ogg'; + else + email_attachment_mime_type = 'binary/octet-stream'; + end + require "resources.functions.split" local email_table = split(email_file, '/', true) email_attachment_name = email_table[#email_table] @@ -120,6 +143,7 @@ if (email_queue_enabled == 'true') then sql = sql .. " email_queue_attachment_uuid, "; sql = sql .. " email_queue_uuid, "; sql = sql .. " domain_uuid, "; + sql = sql .. " email_attachment_mime_type, "; sql = sql .. " email_attachment_type, "; sql = sql .. " email_attachment_path, "; sql = sql .. " email_attachment_name, "; @@ -129,19 +153,21 @@ if (email_queue_enabled == 'true') then sql = sql .. " :email_queue_attachment_uuid, "; sql = sql .. " :email_queue_uuid, "; sql = sql .. " :domain_uuid, "; + sql = sql .. " :email_attachment_mime_type, "; sql = sql .. " :email_attachment_type, "; sql = sql .. " :email_attachment_path, "; sql = sql .. " :email_attachment_name, "; sql = sql .. " :email_attachment_base64 "; sql = sql .. ") "; local params = { - email_queue_attachment_uuid = email_queue_attachment_uuid; - email_queue_uuid = email_queue_uuid; + email_queue_attachment_uuid = email_queue_attachment_uuid; + email_queue_uuid = email_queue_uuid; domain_uuid = domain_uuid; - email_attachment_type = email_attachment_type; - email_attachment_path = email_attachment_path; - email_attachment_name = email_attachment_name; - email_attachment_base64 = email_attachment_base64; + email_attachment_mime_type = email_attachment_mime_type; + email_attachment_type = email_attachment_type; + email_attachment_path = email_attachment_path; + email_attachment_name = email_attachment_name; + email_attachment_base64 = email_attachment_base64; } if (debug["sql"]) then freeswitch.consoleLog("notice", "[send_email] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n"); diff --git a/resources/classes/email.php b/resources/classes/email.php index 96b658824d..3e4fdab110 100644 --- a/resources/classes/email.php +++ b/resources/classes/email.php @@ -256,24 +256,65 @@ if (!class_exists('email')) { if (is_array($this->attachments) && sizeof($this->attachments) > 0) { $y = 0; foreach ($this->attachments as $attachment) { - //set the name of the file - if (strlen($attachment['value']) < 255 && file_exists($attachment['value'])) { - $attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']); - $attachment['type'] = strtolower(pathinfo($attachment['value'], PATHINFO_EXTENSION)); + //set the name of the file, determine extension + if ($attachment['path'] && $attachment['name']) { + if (file_exists($attachment['path'] && $attachment['name'])) { + $attachment['type'] = strtolower(pathinfo($attachment['name'], PATHINFO_EXTENSION)); + } + } + else if ($attachment['value']) { + //old method + if (strlen($attachment['value']) < 255 && file_exists($attachment['value'])) { + $attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']); + $attachment['path'] = pathinfo($attachment['value'], PATHINFO_DIRNAME); + $attachment['type'] = strtolower(pathinfo($attachment['value'], PATHINFO_EXTENSION)); + } + } + + //set the mime type + switch ($attachment['type']) { + case "jpg": + case "jpeg": + $attachment['mime_type'] = 'image/jpeg'; + break; + case "gif": + $attachment['mime_type'] = 'image/gif'; + break; + case "png": + $attachment['mime_type'] = 'image/png'; + break; + case "pdf": + $attachment['mime_type'] = 'application/pdf'; + break; + case "tif": + case "tiff": + $attachment['mime_type'] = 'image/tiff'; + break; + case "mp3": + $attachment['mime_type'] = 'audio/mpeg'; + break; + case "wav": + $attachment['mime_type'] = 'audio/x-wav'; + break; + case "opus": + $attachment['mime_type'] = 'audio/opus'; + break; + case "ogg": + $attachment['mime_type'] = 'audio/ogg'; + break; + default: + $attachment['mime_type'] = 'binary/octet-stream'; } //add the attachments to the array $array['email_queue_attachments'][$y]['email_queue_attachment_uuid'] = uuid(); $array['email_queue_attachments'][$y]['email_queue_uuid'] = $email_queue_uuid; $array['email_queue_attachments'][$y]['domain_uuid'] = $this->domain_uuid; + $array['email_queue_attachments'][$y]['email_attachment_mime_type'] = $attachment['mime_type']; $array['email_queue_attachments'][$y]['email_attachment_type'] = $attachment['type']; $array['email_queue_attachments'][$y]['email_attachment_name'] = $attachment['name']; - if (strlen($attachment['value']) < 255 && file_exists($attachment['value'])) { - $array['email_queue_attachments'][$y]['email_attachment_path'] = pathinfo($attachment['value'], PATHINFO_DIRNAME); - } - else { - $array['email_queue_attachments'][$y]['email_attachment_base64'] = base64_decode($attachment['value']); - } + $array['email_queue_attachments'][$y]['email_attachment_path'] = $attachment['path']; + $array['email_queue_attachments'][$y]['email_attachment_base64'] = $attachment['base64']; $y++; } } @@ -345,14 +386,14 @@ if (!class_exists('email')) { Array ( [0] => Array ( - [type] => file (or 'path') + [mime_type] => image/jpeg (will be determined by file extension, if empty) [name] => filename.ext - [value] => /folder/filename.ext + [path] => /source/folder/ (not used if base64 content) + [base64] => file content as base64 (not used if name and path set) + [cid] => content id of file attachment (only used if referencing attached files in body content) ) [1] => Array ( - [type] => string - [name] => filename.ext - [value] => (string of file contents - if base64, will be decoded automatically) + ... ) ) @@ -521,41 +562,18 @@ if (!class_exists('email')) { if (is_array($this->attachments) && sizeof($this->attachments) > 0) { foreach ($this->attachments as $attachment) { - //set the name of the file - $attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']); - - //set the mime type - switch (substr($attachment['name'], -4)) { - case ".png": - $attachment['mime_type'] = 'image/png'; - break; - case ".pdf": - $attachment['mime_type'] = 'application/pdf'; - break; - case ".mp3": - $attachment['mime_type'] = 'audio/mpeg'; - break; - case ".wav": - $attachment['mime_type'] = 'audio/x-wav'; - break; - case "opus": - $attachment['mime_type'] = 'audio/opus'; - break; - case ".ogg": - $attachment['mime_type'] = 'audio/ogg'; - break; - } - //add the attachments - if (strlen($attachment['value']) < 255 && file_exists($attachment['value'])) { - $mail->AddAttachment($attachment['value'], $attachment['name'], 'base64', $attachment['mime_type']); + if (file_exists($attachment['path'].'/'.$attachment['name'])) { + $mail->AddAttachment($attachment['path'].'/'.$attachment['name'], $attachment['name'], 'base64', $attachment['mime_type']); } else { - if (base64_encode(base64_decode($attachment['value'], true)) === $attachment['value']) { - $mail->AddStringAttachment(base64_decode($attachment['value']), $attachment['name'], 'base64', $attachment['mime_type']); - } - else { - $mail->AddStringAttachment($attachment['value'], $attachment['name'], 'base64', $attachment['mime_type']); + if ($attachment['base64']) { + if ($attachment['cid']) { + $email->addStringEmbeddedImage(base64_decode($attachment['base64']), $attachment['cid'], $attachment['name'], 'base64', $attachment['mime_type']); + } + else { + $mail->AddStringAttachment(base64_decode($attachment['base64']), $attachment['name'], 'base64', $attachment['mime_type']); + } } } }