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.

Sending email
#1
Big Grin 
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
Reply


Forum Jump: