06.04.2022, 17:31
(21.09.2016, 06:34)admin Wrote: Updated script for sending e-mails with attachment, place this function in Common functions:
Code:-- send an e-mail with attachment function mailattach(to, subject, message, filename, filedata, mimetype) -- make sure these settings are correct local settings = { -- "from" field, only e-mail must be specified here from = 'example@gmail.com', -- smtp username user = 'example@gmail.com', -- smtp password password = 'mypassword', -- smtp server server = 'smtp.gmail.com', -- smtp server port port = 465, -- enable ssl, required for gmail smtp secure = 'tlsv12', } local smtp = require('socket.smtp') if type(to) ~= 'table' then to = { to } end for index, email in ipairs(to) do to[ index ] = '<' .. tostring(email) .. '>' end -- escape double quotes in file name filename = filename:gsub('"', '\\"') -- message headers and body settings.source = smtp.message({ headers = { to = table.concat(to, ', '), subject = subject, }, body = { { headers = { ['Content-Type'] = 'text/html; charset=utf-8', ['From'] = '<' .. tostring(settings.from) .. '>', }, body = mime.eol(0, message) }, { headers = { ['Content-Type'] = mimetype or 'text/plain', ['Content-Disposition'] = 'attachment; filename="' .. filename .. '"', ['Content-Transfer-Encoding'] = 'BASE64', }, body = ltn12.source.chain( ltn12.source.string(filedata), ltn12.filter.chain(mime.encode('base64'), mime.wrap()) ) } } }) -- fixup from field settings.from = '<' .. tostring(settings.from) .. '>' settings.rcpt = to return smtp.send(settings) end
Function parameters:Example:
- to - recepient's e-mail address
- subject - e-mail subject
- message - the message itself
- filename - name of the attachment file that will appear in the e-mail
- filedata - attachment file data
- mimetype - mime type of attachment, defaults to text/plain when not specified
Code:-- read csv report file data = io.readfile('/home/ftp/report-2016.csv') -- send file as report.csv with text/csv mime type res, err = mailattach('someone@example.com', 'Report for 2016', 'CSV file attached', 'report.csv', data, 'text/csv') log(res, err)
Hello,
Is this function still working on the latest SpaceLynk firmware (2.7) ?
I'm thinking in using it to send csv energy measurements every start of a new month, with the last month measurements.
Thank you