07.12.2016, 11:42
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)
BR,
Erwin
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