This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

email via application
#1
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?

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)
Reply
#2
You cannot use Luasocket library for sending emails from the web server context (apps).

Attached is a modified single file version of this library: https://github.com/GUI/lua-resty-mail/

It should work from both scripts and apps. You can create a user library named mail and load it like this:
Code:
local mail = require('user.mail')

Check the link above for examples and options reference.

Attached Files
.lua   mail.lua (Size: 14.81 KB / Downloads: 1)
Reply
#3
Hi , i need help for that. For test purposes , i created a simple mailtest app. In , data/mailtest i have index.lp and icon.png. In,  libs/mailtest i have mail_lib.lua ( new library you shared.) and i can't send email i get connect failure: timeout . I shared files. Can you check it out ?

Attached Files Thumbnail(s)
   
.lua   mail_lib.lua (Size: 14.9 KB / Downloads: 1)
.lp   index.lp (Size: 3.86 KB / Downloads: 2)
Reply
#4
Remove timeout = 10 from index.lp and try again.
Reply


Forum Jump: