-- send an e-mail with attachmentfunctionmailattach(to, subject, message, filename, filedata, mimetype)
-- make sure these settings are correctlocalsettings = {
-- "from" field, only e-mail must be specified herefrom = 'example@gmail.com',
-- smtp usernameuser = 'example@gmail.com',
-- smtp passwordpassword = 'mypassword',
-- smtp serverserver = 'smtp.gmail.com',
-- smtp server portport = 465,
-- enable ssl, required for gmail smtpsecure = 'tlsv12',
}
localsmtp = require('socket.smtp')
iftype(to) ~= 'table'thento = { to }
endforindex, emailinipairs(to) doto[ index ] = '<' .. tostring(email) .. '>'end-- escape double quotes in file namefilename = filename:gsub('"', '\\"')
-- message headers and bodysettings.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'] = mimetypeor'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 fieldsettings.from = '<' .. tostring(settings.from) .. '>'settings.rcpt = toreturnsmtp.send(settings)
end
Function parameters:
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
Example:
Code:
12345
-- read csv report filedata = io.readfile('/home/ftp/report-2016.csv')
-- send file as report.csv with text/csv mime typeres, 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.
11.03.2017, 07:29 (This post was last modified: 11.03.2017, 19:07 by buuuudzik.)
Hello,
I've changed a little original script for sending an email. Below script can send an email to a few recipients with a few files in the attachement not only one file. You can use it for example for sending images from a few cameras or sending camera image and .csv file.
This script is an updated version of above script from Edgars:
url = 'http://camera_mpeg_address/'filename = 'snapshot.jpg'image, path = download2ftp(url, filename)
-- image is for using in email-- path is for using e.g. when you want remove image after email send-- remove downloaded fileos.remove(path)
You don't need that strange repeat loop. Wget is blocking, it does not background while downloading so when os.executes return file must be there unless there's an error with connection or file is not found on the remote server.
15.03.2017, 11:00 (This post was last modified: 15.03.2017, 11:08 by buuuudzik.)
(15.03.2017, 10:46)admin Wrote: You don't need that strange repeat loop. Wget is blocking, it does not background while downloading so when os.executes return file must be there unless there's an error with connection or file is not found on the remote server.
But I had such situation:
especially when I try download few files and after this I want immediately send them via email. Email was sent but with only a part of images(not fully loaded). Firstly I've added some intuitive os.sleep(n) (intuitive because it depends on size of file and on available bandwith) but after I've prepared this solution which start download an image, check if download is finished and then it returns full-loaded file and its path. When I am using this solution I didn't have some problem with not fully-loaded file sent via email.
But also when I've used Erwin example sometimes I had a problem with not-fully loaded images attached to an email. The solution was adding some os.sleep(n) after os.execute() (normally for camera snapshot 400kB 1-2s).
Does your camera provide a snapshot or are you trying to download the mjpeg stream? I would suggest use luasocket HTTP requests without saving the file to a local filesystem since you are deleting it right after sending.
(15.03.2017, 11:06)admin Wrote: Does your camera provide a snapshot or are you trying to download the mjpeg stream? I would suggest use luasocket HTTP requests without saving the file to a local filesystem since you are deleting it right after sending.
For me this would be a better option in some situations.
For my disposition are these urls:
1) /axis-cgi/mjpg/video.cgi
(15.03.2017, 11:06)admin Wrote: Does your camera provide a snapshot or are you trying to download the mjpeg stream? I would suggest use luasocket HTTP requests without saving the file to a local filesystem since you are deleting it right after sending.
For me this would be a better option in some situations.
For my disposition are these urls:
1) /axis-cgi/mjpg/video.cgi
I'am using the second which provides "snapshot.jpg" ~400kB.
Hello,
How exactly do you use luasocket to send an email with a snapshot.jpg.
I have a doorbird cam who use a image request to : http://ip-adres/bha-api/image.cgi
And returns a jpeg image.
can you send me the code please.
-- Download an image from camerarequire('socket.http')
image = socket.http.request('http://192.168.2.10/cgi-bin/snapshot.cgi?chn=1&u=admin&p=password')
to = 'example@gmail.com'subject = 'Snapshot from camera'message = 'This is the message from your LM with the snapshot from camera.'files = {{filename='Snapshot.jpg', filedata=image, mimetype='jpg'}}
-- Send an emailmailattach(to, subject, message, files)