Logic Machine Forum
Sending email - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Sending email (/showthread.php?tid=341)



Sending email - rocfusion - 12.07.2016

Hi

After some testing I discovered that when sending an email to multiple recipients the 'to' variable wasn't being converted to an table.  It would create a table but only contained one row.  Below is a revised version of the send email script.  Make sure that each recipient is separated with a comma,  ie...  mail('your@first.com, your@second.com, your@third.com', 'some subject', 'some message')

In addition to this, when you want to send via an email server that does not require ssl, just comment out the line, secure='sslv23'

Thanks,


Roger



Code:
-- send an e-mail
function mail(to, subject, message)
 -- 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 = 'sslv23',
 }

 local smtp = require('socket.smtp')

 if type(to) ~= 'table' then
    to = string.split(to,', ')
 end

 for index, email in ipairs(to) do
   to[ index ] = '<' .. tostring(email) .. '>'
 end

 -- message headers and body
 settings.source = smtp.message({
   headers = {
     to = table.concat(to, ', '),
     subject = subject,
     ['Content-type'] = 'text/html; charset=utf-8',
   },
   body = message
 })

 -- fixup from field
 settings.from = '<' .. tostring(settings.from) .. '>'
 settings.rcpt = to

 return smtp.send(settings)
end