2015-03-16 06:39:29 +01:00
|
|
|
---
|
2015-03-16 10:17:19 +01:00
|
|
|
-- Working Example: https://www.youtube.com/watch?v=CcRbFIJ8aeU
|
2019-12-31 00:53:54 +01:00
|
|
|
-- @description a basic SMTP email example. You must use an account which can
|
|
|
|
-- provide unencrypted authenticated access.
|
|
|
|
-- This example was tested with an AOL and Time Warner email accounts.
|
|
|
|
-- GMail does not offer unencrypted authenticated access.
|
2015-03-16 06:39:29 +01:00
|
|
|
-- To obtain your email's SMTP server and port simply Google it e.g. [my email domain] SMTP settings
|
2019-12-31 00:53:54 +01:00
|
|
|
-- For example for timewarner you'll get to this page
|
|
|
|
-- http://www.timewarnercable.com/en/support/faqs/faqs-internet/e-mailacco/incoming-outgoing-server-addresses.html
|
2015-03-16 06:39:29 +01:00
|
|
|
-- To Learn more about SMTP email visit:
|
|
|
|
-- SMTP Commands Reference - http://www.samlogic.net/articles/smtp-commands-reference.htm
|
|
|
|
-- See "SMTP transport example" in this page http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
|
|
|
|
-- @author Miguel
|
|
|
|
|
2015-03-14 02:51:19 +01:00
|
|
|
-- The email and password from the account you want to send emails from
|
2015-03-13 20:39:14 +01:00
|
|
|
local MY_EMAIL = "esp8266@domain.com"
|
|
|
|
local EMAIL_PASSWORD = "123456"
|
|
|
|
|
2015-03-14 02:51:19 +01:00
|
|
|
-- The SMTP server and port of your email provider.
|
|
|
|
-- If you don't know it google [my email provider] SMTP settings
|
2015-03-13 20:39:14 +01:00
|
|
|
local SMTP_SERVER = "smtp.server.com"
|
|
|
|
local SMTP_PORT = "587"
|
|
|
|
|
2015-03-14 02:51:19 +01:00
|
|
|
-- The account you want to send email to
|
2015-03-13 20:39:14 +01:00
|
|
|
local mail_to = "to_email@domain.com"
|
|
|
|
|
2015-03-14 02:51:19 +01:00
|
|
|
-- Your access point's SSID and password
|
2015-03-13 20:39:14 +01:00
|
|
|
local SSID = "ssid"
|
|
|
|
local SSID_PASSWORD = "password"
|
|
|
|
|
2015-03-14 02:51:19 +01:00
|
|
|
-- configure ESP as a station
|
2015-03-13 20:39:14 +01:00
|
|
|
wifi.setmode(wifi.STATION)
|
|
|
|
wifi.sta.config(SSID,SSID_PASSWORD)
|
|
|
|
wifi.sta.autoconnect(1)
|
|
|
|
|
2015-03-14 02:51:19 +01:00
|
|
|
-- These are global variables. Don't change their values
|
|
|
|
-- they will be changed in the functions below
|
2015-03-13 20:39:14 +01:00
|
|
|
local email_subject = ""
|
|
|
|
local email_body = ""
|
|
|
|
local count = 0
|
2019-12-31 00:53:54 +01:00
|
|
|
local timer
|
2015-03-16 10:17:19 +01:00
|
|
|
|
|
|
|
local smtp_socket = nil -- will be used as socket to email server
|
2015-03-13 20:39:14 +01:00
|
|
|
|
2015-03-14 02:51:19 +01:00
|
|
|
-- The display() function will be used to print the SMTP server's response
|
2019-12-31 00:53:54 +01:00
|
|
|
local function display(sck, response) -- luacheck: no unused
|
2015-03-13 20:39:14 +01:00
|
|
|
print(response)
|
|
|
|
end
|
|
|
|
|
2015-03-14 02:51:19 +01:00
|
|
|
-- 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.
|
2019-12-31 00:53:54 +01:00
|
|
|
local function do_next()
|
2015-03-13 20:39:14 +01:00
|
|
|
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
|
2019-12-31 00:53:54 +01:00
|
|
|
smtp_socket:send(encoder.toBase64(MY_EMAIL).."\r\n")
|
2015-03-13 20:39:14 +01:00
|
|
|
elseif(count == 3) then
|
|
|
|
count = count + 1
|
2019-12-31 00:53:54 +01:00
|
|
|
smtp_socket:send(encoder.toBase64(EMAIL_PASSWORD).."\r\n")
|
2015-03-13 20:39:14 +01:00
|
|
|
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
|
|
|
|
smtp_socket:send("DATA\r\n")
|
|
|
|
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" ..
|
|
|
|
email_body,"\r\n.\r\n","")
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2015-03-13 20:39:14 +01:00
|
|
|
smtp_socket:send(message.."\r\n.\r\n")
|
|
|
|
elseif(count==8) then
|
|
|
|
count = count+1
|
2019-12-31 00:53:54 +01:00
|
|
|
timer:stop()
|
2015-03-13 20:39:14 +01:00
|
|
|
smtp_socket:send("QUIT\r\n")
|
|
|
|
else
|
|
|
|
smtp_socket:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
-- The connected() function is executed when the SMTP socket is connected to the SMTP server.
|
2015-03-14 02:51:19 +01:00
|
|
|
-- This function will create a timer to call the do_next function which will send the SMTP commands
|
2019-02-17 19:26:29 +01:00
|
|
|
-- in sequence, one by one, every 5000 seconds.
|
2015-03-14 02:51:19 +01:00
|
|
|
-- You can change the time to be smaller if that works for you, I used 5000ms just because.
|
2019-12-31 00:53:54 +01:00
|
|
|
local function connected()
|
|
|
|
timer = tmr.create()
|
|
|
|
timer:alarm(5000, tmr.ALARM_AUTO, do_next)
|
2015-03-13 20:39:14 +01:00
|
|
|
end
|
|
|
|
|
2015-03-14 02:51:19 +01:00
|
|
|
-- @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
|
2019-12-31 00:53:54 +01:00
|
|
|
local function send_email(subject,body)
|
2015-03-16 10:17:19 +01:00
|
|
|
count = 0
|
2015-03-13 20:39:14 +01:00
|
|
|
email_subject = subject
|
|
|
|
email_body = body
|
2015-03-16 10:17:19 +01:00
|
|
|
smtp_socket = net.createConnection(net.TCP,0)
|
2015-03-13 20:39:14 +01:00
|
|
|
smtp_socket:on("connection",connected)
|
|
|
|
smtp_socket:on("receive",display)
|
|
|
|
smtp_socket:connect(SMTP_PORT,SMTP_SERVER)
|
|
|
|
end
|
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
do
|
|
|
|
-- Send an email
|
|
|
|
send_email(
|
|
|
|
"ESP8266",
|
|
|
|
[[Hi,
|
|
|
|
How are your IoT projects coming along?
|
|
|
|
Best Wishes,
|
|
|
|
ESP8266]]
|
|
|
|
)
|
|
|
|
end
|