Posts: 7764
Threads: 42
Joined: Jun 2015
Reputation:
447
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: - 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: -- 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)
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
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:- 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: -- 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.
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
06.12.2016, 14:30
(This post was last modified: 06.12.2016, 14:32 by Erwin van der Zwart.)
Hi,
What FW version do you use? I have done this before in 1.3 but if you have 1.5.1 i need to update the script for you.
BR,
Erwin
PS: I don't think you can send attachment with pushover...
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
Hi Erwin,
I am using 1.5.1 firmware on HL.
Not pushover but telegram does. I'll tru it and I keep you informed.
I await for your script.
Thanks.
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
Ciao Erwin,
are you referring to this following:
http://forum.logicmachine.net/showthread.php?tid=195
Thanks.
Br
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
Hi,
Yes but this is the 1.0 to 1.3 version, there are a lot of changes in the latest FW so i think this version won't work on 1.5+ FW.
I will check this, and if needed post a new version.
BR,
Erwin
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
Hi,
Here is a working sample on fetching a image from URL and add it as attachment to a mail message in new FW 1.5.1:
Add your credentials and mail adresses and aliases in the script, make sure less secure apps in your gmail account is enable.
https://www.google.com/settings/security/lesssecureapps
Change the source of your image (now linked to test logo image on my SL)
Code: --***********************************************************--
--** Email image attachment created by Erwin van der Zwart **--
--***********************************************************--
--******************* Start of parameters *******************--
--***********************************************************--
--Gmail (smtp) username !IMPORTANT!
user = 'yourname@gmail.com'
--Gmail (smtp) password !IMPORTANT!
password = 'yourgmailpassword'
--Sender for e-mail
from = '<sender@gmail.com>'
alias_from = 'Sender'
--Recipient for e-mail
to = '<reveiver@gmail.com>'
alias_to = 'Receiver'
--Subject for e-mail
subject = 'Snapshot from camera automaticly send by homeLYnk'
--Attachment and filetype (set filetype as 'gif', 'png', 'bmp', 'jpg' or 'jpeg' according image source)
image_type = 'jpg'
image_name_attachment = 'snapshot'
source_image_url = 'http://www.vdrzwart.nl:8085/scada/resources/img/logo.jpg'
image_description = 'Snapshot from IP Camera'
--Message on bottom of email (will only be showed when client don't understand attachment)
epilogue = 'End of message'
--***********************************************************--
--******************** End of parameters ********************--
--***********************************************************--
--********** DON'T CHANGE ANYTHING UNDER THIS LINE **********--
--***********************************************************--
--Get remote (from HTTP) image and put image file to HL (will be deleted when end of script)
os.execute('wget -O /home/ftp/' .. image_name_attachment .. '.' .. image_type .. ' ' .. source_image_url .. '')
--Create filename and location
local fileName = '/home/ftp/' .. image_name_attachment .. '.' .. image_type
--Create table to include mail settings
local settings = {
from = from,
rcpt = to,
user = user,
password = password,
server = 'smtp.gmail.com',
port = 465,
secure = 'sslv23',
}
--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"] = 'image/' .. image_type .. '; name="' .. image_name_attachment .. '.' .. image_type .. '"',
["content-disposition"] = 'attachment; filename="' .. image_name_attachment .. '.' .. image_type .. '"',
["content-description"] = '' .. image_description .. '',
["content-transfer-encoding"] = "BASE64"
},
body = ltn12.source.chain(
ltn12.source.file(io.open(fileName, "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("Could not send email: ", e, "\n")
end
--Delete downloaded image file from HL
os.remove(fileName)
BR,
Erwin
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
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:
Code: -- send an e-mail with attachments
function mailattach(to, subject, message, files)
-- make sure these settings are correct
local settings = {
-- "from" field, only e-mail must be specified here
from = 'user@gmail.com',
-- smtp username
user = 'user@gmail.com',
-- smtp password
password = 'user',
-- smtp server
server = 'smtp.gmail.com',
-- smtp server port
port = 465,
-- enable ssl, required for gmail smtp
secure = 'sslv23',
}
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
for _, object in ipairs(table) do
files[_].filename = files[_].filename:gsub('"', '\\"')
end
-- message headers and body
email = {headers = {}, body = {}}
email.headers = { to = table.concat(to, ', '), subject = subject, }
email.body[1] = { headers = { ['Content-Type'] = 'text/html; charset=utf-8', }, body = mime.eol(0, message) }
-- adding attachements
for _, object in ipairs(files) do
email.body[_+1] = {
headers = { ['Content-Type'] = files[_].mimetype or 'text/plain', ['Content-Disposition'] = 'attachment; filename="' .. files[_].filename ..'"', ['Content-Transfer-Encoding'] = 'BASE64', },
body = ltn12.source.chain( ltn12.source.string(files[_].filedata), ltn12.filter.chain(mime.encode('base64'), mime.wrap() ) )
}
end
settings.source = smtp.message(email)
-- fixup from field
settings.from = '<' .. tostring(settings.from) .. '>'
settings.rcpt = to
return smtp.send(settings)
end
Example of use:
Code: -- email parameters
to = {
'first@gmail.com',
'second@gmail.com'
}
subject = 'Email with a few attachements'
message = 'Email with a few attachements. Please check them.'
files = {
{filename = 'Image1.jpg', filedata = io.readfile('/www/scada/resources/img/Image1.jpg'), mimetype = 'jpg'},
{filename = 'Image2.jpg', filedata = io.readfile('/www/scada/resources/img/Image2.jpg'), mimetype = 'jpg'}
}
-- send an e-mail with attachments
mailattach(to, subject, message, files)
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
15.03.2017, 09:35
(This post was last modified: 15.03.2017, 09:43 by buuuudzik.)
You're welcome
And here you have some library for easy download a file to ftp:
Code: -- download a file to ftp filename e.g. snapshot.jpg
function download2ftp(url, filename)
path = '/home/ftp/' .. filename
os.execute('wget -O ' .. path .. ' ' .. url .. '')
file = io.readfile(path)
-- Download start
repeat
os.sleep(0.2)
file = io.readfile(path)
until file
-- Download finish
repeat
before = #file
os.sleep(0.2)
file = io.readfile(path)
current = #file
until current == before
return file, path
end
Example:
Code: 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 file
os.remove(path)
Posts: 7764
Threads: 42
Joined: Jun 2015
Reputation:
447
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.
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
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).
Posts: 7764
Threads: 42
Joined: Jun 2015
Reputation:
447
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.
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
(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
2) cgi-bin/snapshot.cgi?chn=[CHANNEL]&u=[USERNAME]&p=[PASSWORD]
I'am using the second which provides "snapshot.jpg" ~400kB.
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
Yes, for sure luasocket is a better option. In this case perfect Thanks
Posts: 13
Threads: 1
Joined: Jun 2016
Reputation:
0
(15.03.2017, 11:12)buuuudzik Wrote: (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
2) cgi-bin/snapshot.cgi?chn=[CHANNEL]&u=[USERNAME]&p=[PASSWORD]
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.
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
Something like below:
Code: -- Download an image from camera
require('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 email
mailattach(to, subject, message, files)
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
22.10.2017, 23:04
(This post was last modified: 22.10.2017, 23:04 by Erwin van der Zwart.)
Hi,
Try this script, image can be taken from http or https source, just fill in the correct parameters and run from event or planned script:
BR,
Erwin
Posts: 55
Threads: 18
Joined: Jun 2017
Reputation:
1
(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:- 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: -- 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
Posts: 7764
Threads: 42
Joined: Jun 2015
Reputation:
447
It should be working. If if does not then check Logs tab for an error message from the script.
|