06.12.2016, 12:24
(This post was last modified: 06.12.2016, 12:29 by Domoticatorino.)
(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',
},
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)
Dear all,
further to this, I would like to save in HL an image of a cam and sending it by mail or pushover to a customer. The goal is the following:
- when there's a bus event, image of the cam (obviously I have to verify if cam can save image to an external storage like homelynk) I would like to send the image saved to a customer via mail or via pushover.
What do you suggest about it?
Thanks.