This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

E-mail with attachment
#7
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
Reply


Messages In This Thread
E-mail with attachment - by admin - 21.09.2016, 06:34
RE: E-mail with attachment - by Erwin van der Zwart - 07.12.2016, 11:42
RE: E-mail with attachment - by buuuudzik - 11.03.2017, 07:29
RE: E-mail with attachment - by buuuudzik - 15.03.2017, 09:35
RE: E-mail with attachment - by admin - 15.03.2017, 10:46
RE: E-mail with attachment - by buuuudzik - 15.03.2017, 11:00
RE: E-mail with attachment - by admin - 15.03.2017, 11:06
RE: E-mail with attachment - by buuuudzik - 15.03.2017, 11:12
RE: E-mail with attachment - by Keitz - 22.10.2017, 18:11
RE: E-mail with attachment - by buuuudzik - 17.03.2017, 16:59
RE: E-mail with attachment - by buuuudzik - 22.10.2017, 18:24
RE: E-mail with attachment - by iJAF - 06.04.2022, 17:31
RE: E-mail with attachment - by admin - 11.04.2022, 09:44

Forum Jump: