Tell me what you want, What you really, really want.

Moderator: Mmiscool

User avatar
By heckler
#34152 Is it possible to generate an email to be sent via an smtp email server??
If not this would be an awesome addition to the BASIC functions.

I would like to generate an email if a Temperature goes out of limits...

Here is what it takes to send an email via NODEMCU and LUA

Or is it possible to compile this in LUA and call it from the BASIC OS ??


Code: Select allrequire("base64")

-- The email and password from the account you want to send emails from
local MY_EMAIL = ""
local EMAIL_PASSWORD = "xxx"

local SMTP_SERVER = ""
local SMTP_PORT = "25"

-- The account you want to send email to
local mail_to = ""

-- These are global variables. Don't change their values
-- they will be changed in the functions below
local email_subject = ""
local email_body = ""
local count = 0

local smtp_socket = nil -- will be used as socket to email server

-- The display() function will be used to print the SMTP server's response
function display(sck,response)

-- The do_next() function is used to send the SMTP commands to the SMTP server in the required sequence.
-- I was going to use socket callbacks but the code would not run callbacks after the first 3.
function do_next()
            if(count == 0)then
                count = count+1
                local IP_ADDRESS = wifi.sta.getip()
                smtp_socket:send("HELO "..IP_ADDRESS.."\r\n")
            elseif(count==1) then
                count = count+1
                smtp_socket:send("AUTH LOGIN\r\n")
            elseif(count == 2) then
                count = count + 1
            elseif(count == 3) then
                count = count + 1
            elseif(count==4) then
                count = count+1
               smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n")
            elseif(count==5) then
                count = count+1
               smtp_socket:send("RCPT TO:<" .. mail_to ..">\r\n")
            elseif(count==6) then
                count = count+1
            elseif(count==7) then
                count = count+1
                local message = string.gsub(
                "From: \"".. MY_EMAIL .."\"<"..MY_EMAIL..">\r\n" ..
                "To: \"".. mail_to .. "\"<".. mail_to..">\r\n"..
                "Subject: ".. email_subject .. "\r\n\r\n"  ..
            elseif(count==8) then
               count = count+1

-- The connectted() function is executed when the SMTP socket is connected to the SMTP server.
-- This function will create a timer to call the do_next function which will send the SMTP commands
-- in sequence, one by one, every 5000 seconds.
-- You can change the time to be smaller if that works for you, I used 5000ms just because.
function connected(sck)

-- @name send_email
-- @description Will initiated a socket connection to the SMTP server and trigger the connected() function
-- @param subject The email's subject
-- @param body The email's body
function send_email(subject,body)
     count = 0
     email_subject = subject
     email_body = body
     smtp_socket = net.createConnection(net.TCP,0)

-- Send an email
How are your IoT projects coming along?
Best Wishes,

-- Restart the module to clear the Heap
tmr.alarm(1, 240000, 1, function()
User avatar
By Mmiscool
#34159 Looking in to this now. Possibly in a day or so I will have it working in a build. Possibly sooner.

This basic is really turning in to everything but the kitchen sink. That is a good thing. :-)

The more it can do out of the box without much effort the better.
User avatar
By Rotohammer
Mmiscool wrote:The more it can do out of the box without much effort the better.

I agree, commonly used functions make sense to add a command, especially when they would be tedious if not impossible to do from BASIC. I would use the email command a lot :)
User avatar
By Mmiscool
#34238 Added email commands to latest version.

Code: Select allsetupemail "" 2525 "username" "password"
email "" "" "Subject" "Body"