Yesterday, 23:09
Hello, one of the features of the application we are developing is to periodically send an email with a CSV report attached. We implemented this using a script, and it works very successfully.
However, when we tried to embed this functionality into the application's index.lp file, we were unable to send even a plain email, let alone one with an attachment. This is the same Logic Machine where the email works perfectly fine via scripts.
I am sharing the working script below. How can we properly integrate this into the application?
However, when we tried to embed this functionality into the application's index.lp file, we were unable to send even a plain email, let alone one with an attachment. This is the same Logic Machine where the email works perfectly fine via scripts.
I am sharing the working script below. How can we properly integrate this into the application?
Code:
--Gmail (smtp) username
user = '***@gmail.com'
--Gmail (smtp) password
password = '***'
--From
from = '<' .. user .. '>'
alias_from = 'gonderenhesap@gmail.com'
--REceiver
to = '***@***.com'
alias_to = '***@***.com'
--Email subject
subjectpart1 = 'Alarmlar'
subjectpart2 = 'Logic Machine Tarafindan Gonderildi'
--Message
epilogue = 'Mesajin sonu'
-- Get all alerts from DB
alerts_table = db:getall('SELECT * FROM alerts')
-- csv buffer
buffer = {}
-- format csv row
csv = string.format('%q,%q,%q', "ID", "ALERT", "ADDED TO LIST")
-- add to buffer
table.insert(buffer, csv)
-- add empty line to buffer
table.insert(buffer, "")
-- Loop through alerts_table
for _, alerts in ipairs(alerts_table) do
-- format csv row
csv = string.format('%q,%q,%q', alerts.id, alerts.alert, os.date("%x %X", alerts.alerttime))
-- add to buffer
table.insert(buffer, csv)
end
--Create table to include mail settings
local settings = {
from = from,
rcpt = to,
user = user,
password = password,
server = 'smtp.gmail.com',
port = 465,
secure = 'sslv23',
}
--Create attachment inside FTP server
src = 'AlertExport_'.. os.date('%Y-%m-%d %H#%M#%S') .. '.csv'
dst = '/home/ftp/' .. src
log(io.writefile(dst, buffer))
log("file created")
log(dst)
--Create subject
subject = subjectpart1 .. ": " .. src .. " " .. subjectpart2
--Load required modules to send email with attachment
local smtp = require("socket.smtp")
local mime = require("mime")
local ltn12 = require("ltn12")
--Create e-mail header
settings.source = smtp.message{
headers = {
from = '' .. alias_from .. ' ' .. from .. '',
to = '' .. alias_to .. ' ' .. to .. '',
subject = subject
},
--Load attachment inside body
body = {
preamble = "",
[1] = {
headers = {
["content-type"] = 'text/plain',
["content-disposition"] = 'attachment; filename="'..src..'"',
["content-description"] = '.. src ..',
["content-transfer-encoding"] = "BASE64",
},
body = ltn12.source.chain(
ltn12.source.file(io.open(dst, "rb")),
ltn12.filter.chain(
mime.encode("base64"),
mime.wrap()
)
)
},
epilogue = epilogue
}
}
--Send the email
r, e = smtp.send(settings)
--Create alert when sending gives an error with error message
if (e) then
log (e)
log (r)
end
-- FTP Uploader --------------------------------------------------------------------------
-- local ftp = require("socket.ftp")
-- local ltn12 = require("ltn12")
-- f, e = ftp.put{
-- host = "ftp.test.tv.tr",
-- user = "fgtest@test.tv.tr",
-- password = «ftpsifresi",
-- command = "appe",
-- argument = src,
-- source = ltn12.source.file(io.open(dst, "r"))
-- }
--Delete created file from ftp folder inside HL
os.remove(dst)
