Author Topic: Lua code to send email via gmail ?  (Read 302 times)

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Lua code to send email via gmail ?
« on: August 23, 2017, 04:47:04 am »
Hi

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 ?
« Last Edit: August 23, 2017, 05:06:04 am by parkerc »

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Lua code to send email via gmail ?
« Reply #1 on: August 23, 2017, 04:48:26 am »
I had found this online, (https://stackoverflow.com/questions/29312494/sending-email-using-luasocket-smtp-and-ssl) but it does not seem to work, it goes through with no errors, but nothing is received.

Code: [Select]
-- 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()
        end
    }, {
        __index = function(t,n)
            return function(_, ...)
                return sock[n](sock, ...)
            end
        end
    })
end


function sendMessage(subject, body)
    local msg = {
        headers = {
            from = "your.email@gmail.com",
            to = 'your.target.uk@ABC.com',
            subject = subject
        },
        body = body
    }


    local ok, err = smtp.send {
        from = 'your.email@gmail.com',
        rcpt = 'your.target.uk@ABC.com',
        source = smtp.message(msg),
        user = 'your.email',
        password = 'password',
        server = 'smtp.gmail.com',
        port = 465,
        create = sslCreate
    }
    if not ok then
        print("Mail send failed", err) -- better error handling required
    end
[\code]
« Last Edit: August 23, 2017, 07:05:09 am by parkerc »

Offline aa6vh

  • Hero Member
  • *****
  • Posts: 597
  • Karma: +12/-0
Re: Lua code to send email via gmail ?
« Reply #2 on: August 23, 2017, 10:52:33 am »
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.

Code: [Select]
function send_email (email_to, email_subject, email_message)
-- change these to match the information from your email provider
  local SMTP_SERVER = "mail.domain.com"
  local SMTP_AUTH_USER = "vera@domain.com"
  local SMTP_AUTH_PW = "password"
  local SMTP_PORT = "587"
  local USER_SENDING = "vera@domain.com"

  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
  }
end

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 483
  • Karma: +23/-2
Re: Lua code to send email via gmail ?
« Reply #3 on: August 23, 2017, 10:58:25 am »
Also see this plugin: http://forum.micasaverde.com/index.php?topic=11510.0

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

Online akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Lua code to send email via gmail ?
« Reply #4 on: August 23, 2017, 12:45:01 pm »
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.

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.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Online icare

  • Jr. Member
  • **
  • Posts: 68
  • Karma: +0/-0
Re: Lua code to send email via gmail ?
« Reply #5 on: August 23, 2017, 01:10:52 pm »
eMail Notification app allows SSL GMail

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Lua code to send email via gmail ?
« Reply #6 on: August 23, 2017, 03:32:16 pm »
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 - https://support.google.com/a/answer/176600?hl=en

Why you may ask ? :) 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")
« Last Edit: September 18, 2017, 02:43:23 pm by parkerc »

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 483
  • Karma: +23/-2
Re: Lua code to send email via gmail ?
« Reply #7 on: August 23, 2017, 04:17:05 pm »
you could put a function wrapper around this for the plugin - it's pretty close to what you want already:

Code: [Select]
luup.call_action("urn:upnp-org:serviceId:SmtpNotification1", "SendEmail", { Recipient_Name="Tony Stark", Recipient_eMail="tony@stark.com", 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.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Lua code to send email via gmail ?
« Reply #8 on: August 23, 2017, 05:29:02 pm »
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.