Update send_mail.lua
This commit is contained in:
parent
667dcdb84b
commit
f7b715b399
|
|
@ -1,5 +1,152 @@
|
|||
local Settings = require "resources.functions.lazy_settings"
|
||||
local Database = require "resources.functions.database"
|
||||
local Settings = require "resources.functions.lazy_settings";
|
||||
local Database = require "resources.functions.database";
|
||||
local cache = require"resources.functions.cache";
|
||||
local log = require "resources.functions.log".send_mail
|
||||
|
||||
|
||||
local db = dbh or Database.new('system');
|
||||
--local settings = Settings.new(db, nil, nil);
|
||||
--local email_method = settings:get('email', 'method', 'text');
|
||||
|
||||
--get the dialplan mode from the cache
|
||||
email_method_key = "settings:email:email_mode";
|
||||
email_method, err = cache.get(email_method_key);
|
||||
|
||||
--if not found in the cache then get it from the database
|
||||
if (err == 'NOT FOUND') then
|
||||
--get the mode from default settings
|
||||
sql = "select default_setting_value from v_default_settings "
|
||||
sql = sql .. "where default_setting_category = 'email' ";
|
||||
sql = sql .. "and default_setting_subcategory = 'method' ";
|
||||
email_method = dbh:first_value(sql, nil);
|
||||
if (email_method) then
|
||||
local ok, err = cache.set(email_method_key, email_method, expire["dialplan"]);
|
||||
end
|
||||
end
|
||||
|
||||
if (email_method == 'queue') then
|
||||
function send_mail(headers, email_address, email_message, email_file)
|
||||
|
||||
--include json library
|
||||
local json
|
||||
if (debug["sql"]) then
|
||||
json = require "resources.functions.lunajson"
|
||||
end
|
||||
|
||||
local domain_uuid = headers["X-FusionPBX-Domain-UUID"];
|
||||
local domain_name = headers["X-FusionPBX-Domain-Name"];
|
||||
local email_type = headers["X-FusionPBX-Email-Type"] or 'info';
|
||||
local call_uuid = headers["X-FusionPBX-Email-Call-UUID"];
|
||||
|
||||
local settings = Settings.new(db, domain_name, domain_uuid);
|
||||
|
||||
local email_from = settings:get('email', 'smtp_from', 'text');
|
||||
local from_name = settings:get('email', 'smtp_from_name', 'text');
|
||||
|
||||
if (email_from == nil or email_from == "") then
|
||||
email_from = address;
|
||||
elseif (from_name ~= nil and from_name ~= "") then
|
||||
email_from = from_name .. "<" .. email_from .. ">";
|
||||
end
|
||||
local email_subject = email_message[1];
|
||||
local email_body = email_message[2] or '';
|
||||
|
||||
api = freeswitch.API();
|
||||
local email_queue_uuid = api:executeString("create_uuid");
|
||||
|
||||
local sql = "insert into v_email_queue ( ";
|
||||
sql = sql .. " email_queue_uuid, ";
|
||||
sql = sql .. " domain_uuid, ";
|
||||
sql = sql .. " email_date, ";
|
||||
sql = sql .. " email_from, ";
|
||||
sql = sql .. " email_to, ";
|
||||
sql = sql .. " email_subject, ";
|
||||
sql = sql .. " email_body, ";
|
||||
sql = sql .. " email_status ";
|
||||
sql = sql .. ") ";
|
||||
sql = sql .. "values ( ";
|
||||
sql = sql .. " :email_queue_uuid, ";
|
||||
sql = sql .. " :domain_uuid, ";
|
||||
sql = sql .. " now(), ";
|
||||
sql = sql .. " :email_from, ";
|
||||
sql = sql .. " :email_to, ";
|
||||
sql = sql .. " :email_subject, ";
|
||||
sql = sql .. " :email_body, ";
|
||||
sql = sql .. " :email_status ";
|
||||
sql = sql .. ") ";
|
||||
local params = {
|
||||
email_queue_uuid = email_queue_uuid;
|
||||
domain_uuid = domain_uuid;
|
||||
email_from = email_from;
|
||||
email_to = email_address;
|
||||
email_subject = email_subject;
|
||||
email_body = email_body;
|
||||
email_status = email_type;
|
||||
}
|
||||
db:query(sql, params);
|
||||
|
||||
if (email_file) then
|
||||
email_attachment_type = string.sub(email_file, -3);
|
||||
email_attachment_path = email_file;
|
||||
email_attachment_name = '';
|
||||
email_attachment_base64 = '';
|
||||
|
||||
require "resources.functions.split"
|
||||
local email_table = split(email_file, '/', true)
|
||||
email_attachment_name = email_table[#email_table]
|
||||
|
||||
email_attachment_path = email_file.sub(email_file, 0, (string.len(email_file) - string.len(email_attachment_name)) - 1);
|
||||
|
||||
--base64 encode the file
|
||||
--local file = require "resources.functions.file"
|
||||
--email_attachment_base64 = assert(file.read_base64(email_file));
|
||||
|
||||
local email_queue_attachment_uuid = api:executeString("create_uuid");
|
||||
|
||||
local sql = "insert into v_email_queue_attachments ( ";
|
||||
sql = sql .. " email_queue_attachment_uuid, ";
|
||||
sql = sql .. " email_queue_uuid, ";
|
||||
sql = sql .. " domain_uuid, ";
|
||||
sql = sql .. " email_attachment_type, ";
|
||||
sql = sql .. " email_attachment_path, ";
|
||||
sql = sql .. " email_attachment_name, ";
|
||||
sql = sql .. " email_attachment_base64 ";
|
||||
sql = sql .. ") ";
|
||||
sql = sql .. "values ( ";
|
||||
sql = sql .. " :email_queue_attachment_uuid, ";
|
||||
sql = sql .. " :email_queue_uuid, ";
|
||||
sql = sql .. " :domain_uuid, ";
|
||||
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;
|
||||
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;
|
||||
}
|
||||
if (debug["sql"]) then
|
||||
freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
|
||||
end
|
||||
db:query(sql, params);
|
||||
end
|
||||
|
||||
log.infof("Email added to the queue as email_queue_uuid = %s", email_queue_uuid);
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
--local headers = {
|
||||
-- ["X-FusionPBX-Domain-UUID"] = '2d171c4c-b237-49ca-9d76-9cffc1618fa7';
|
||||
-- ["X-FusionPBX-Domain-Name"] = 'domain.com';
|
||||
-- ["X-FusionPBX-Email-Type"] = 'voicemail';
|
||||
--}
|
||||
--send_mail(headers, 'alexey@domain.com', {'hello', 'world'})
|
||||
|
||||
if not freeswitch then
|
||||
local log = require "resources.functions.log".sendmail
|
||||
|
|
@ -113,6 +260,8 @@ if freeswitch then
|
|||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
return send_mail
|
||||
|
||||
--local headers = {
|
||||
|
|
|
|||
Loading…
Reference in New Issue