Lua code to send email via gmail ?


I’ve been looking for a while to find some lua code that works with Vera to send an email via gmail, with a dynamically created subject and body (I’m using a generic gmail account, which I’ve set up that only requires password authentication etc. so it does not require any other factors).

My user name, password, port and server gmail settings seem to work where I’m using them on a NAS or on web Cameras to send mail or alert notifications etc. but with nothing built-in for gmail Vera I’ve being triying to find some code to use.

Just curious - Does anyone have a working code/ script for sending email via gmail ?

I had found this online, (Sending email using luasocket smtp and ssl - Stack Overflow) but it does not seem to work, it goes through with no errors, but nothing is received.

[code] – Michal Kottman, 2011, public domain
local socket = require ‘socket’
local smtp = require ‘socket.smtp’
local ssl = require ‘ssl’
local https = require ‘ssl.https’
local ltn12 = require ‘ltn12’

function sslCreate()
local sock = socket.tcp()
return setmetatable({
connect = function(, host, port)
local r, e = sock:connect(host, port)
if not r then return r, e end
sock = ssl.wrap(sock, {mode=‘client’, protocol=‘tlsv1’})
return sock:dohandshake()
}, {
__index = function(t,n)
return function(
, …)
return sock[n](sock, …)

function sendMessage(subject, body)
local msg = {
headers = {
from = “”,
to = ‘’,
subject = subject
body = body

local ok, err = smtp.send {
    from = '',
    rcpt = '',
    source = smtp.message(msg),
    user = '',
    password = 'password',
    server = '',
    port = 465,
    create = sslCreate
if not ok then
    print("Mail send failed", err) -- better error handling required


This is the code I use to send email through an SMTP server. I use this every day successfully. I do not know if Gmail supplies SMTP service, and what their parameters are, perhaps its documented somewhere.

function send_email (email_to, email_subject, email_message)
-- change these to match the information from your email provider 
  local SMTP_SERVER = ""
  local SMTP_AUTH_USER = ""
  local SMTP_AUTH_PW = "password"
  local SMTP_PORT = "587"
  local USER_SENDING = ""

  local smtp = require("socket.smtp")
  local rcpt = {email_to}
  local mesgt = {
    headers = {
      to = email_to,
      from = USER_SENDING,
      subject = email_subject
      body = email_message
  local r, e = smtp.send{ 
    from  = USER_SENDING,
    rcpt  = rcpt,
    source  = smtp.message(mesgt),
    server = SMTP_SERVER,
    port = SMTP_PORT,
    user = SMTP_AUTH_USER,
    password = SMTP_AUTH_PW

Also see this plugin:

Near the end of the posts there is some information on using it with gmail.

The problem with this code, of course, is that it does not use a secure socket connection. Hence the more elaborate form posted by the OP.

eMail Notification app allows SSL GMail

Thanks all,

The email Notification app does indeed work with Gmail, however I was just curious if I there was a pure Lua/luup script/code someone was using. (SSL/TLS preferred.)

The Gmail smtp requirements are here - Send email from a printer, scanner, or app - Google Workspace Admin Help

Why you may ask ? :slight_smile: Well I’m slowly trying to build a set of global calls (functions?) in my Lua start up to simplify their use in scenes or ad-hoc code

(1 & 2 are now there, gmail was next attempt)

1) Prowl (Push Notifications)

I.e - prowl (apikey, application_name, event_text, rate_limit)

2) Polly (“Text to Speech”, vol, “Room”)

I.e Polly(“This is a test of Polly, the Amazon speech engine.”, 60, “living room”)

3) Gmail (Emails)

Ideally the eventual Gmail call I would have would work along the lines of…

Gmail(“target”, “recipient”, “subject”, “message”)

you could put a function wrapper around this for the plugin - it’s pretty close to what you want already:

luup.call_action("urn:upnp-org:serviceId:SmtpNotification1", "SendEmail", { Recipient_Name="Tony Stark", Recipient_eMail="", Subject= "Title", Message="Message" }, 99)

Or copy the functions out of the implementation file for the plugin. It has what you want - you’d just need to simplify it a little bit. But the above is easier.

Thanks @jswim788;

I’d actually already seen the wrapper on the email/smtp notification dedicated forum thread - but you make a good point about looking into the Implementation file

I’ll take a look at that - although pessimistically I’m not sure I have the ability to reverse engineer it as the settings are manually into the device/plugin afterwards - but I’ll give it a try.