2015-03-11 14:32:00 +01:00
|
|
|
---
|
2015-03-16 10:17:19 +01:00
|
|
|
-- Working Example: https://www.youtube.com/watch?v=PDxTR_KJLhc
|
2015-03-11 14:32:00 +01:00
|
|
|
-- @author Miguel (AllAboutEE.com)
|
2019-02-17 19:26:29 +01:00
|
|
|
-- @description This example will read the first email in your inbox using IMAP and
|
2015-03-12 17:27:01 +01:00
|
|
|
-- display it through serial. The email server must provided unecrypted access. The code
|
2015-03-14 02:51:19 +01:00
|
|
|
-- was tested with an AOL and Time Warner cable email accounts (GMail and other services who do
|
2015-03-12 17:27:01 +01:00
|
|
|
-- not support no SSL access will not work).
|
2015-03-11 14:32:00 +01:00
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
local imap = require("imap")
|
2015-03-11 14:32:00 +01:00
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
local IMAP_USERNAME = "email@domain.com"
|
2015-03-12 17:27:01 +01:00
|
|
|
local IMAP_PASSWORD = "password"
|
2015-03-12 07:16:02 +01:00
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
-- find out your unencrypted imap server and port
|
|
|
|
-- from your email provided i.e. google "[my email service] imap settings" for example
|
|
|
|
local IMAP_SERVER = "imap.service.com"
|
2015-03-11 14:32:00 +01:00
|
|
|
local IMAP_PORT = "143"
|
2015-03-12 17:27:01 +01:00
|
|
|
|
|
|
|
local IMAP_TAG = "t1" -- You do not need to change this
|
2015-03-16 10:17:19 +01:00
|
|
|
local IMAP_DEBUG = true -- change to true if you would like to see the entire conversation between
|
2015-03-12 17:27:01 +01:00
|
|
|
-- the ESP8266 and IMAP server
|
2015-03-11 14:32:00 +01:00
|
|
|
|
|
|
|
local SSID = "ssid"
|
2015-03-12 17:27:01 +01:00
|
|
|
local SSID_PASSWORD = "password"
|
2015-03-12 07:16:02 +01:00
|
|
|
|
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
local count = 0 -- we will send several IMAP commands/requests, this variable helps keep track of which one to send
|
2019-12-31 00:53:54 +01:00
|
|
|
local imap_socket, timer
|
2015-03-11 14:32:00 +01:00
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
---
|
|
|
|
-- @name setup
|
2019-02-17 19:26:29 +01:00
|
|
|
-- @description A call back function used to begin reading email
|
2015-03-12 17:27:01 +01:00
|
|
|
-- upon sucessfull connection to the IMAP server
|
2019-12-31 00:53:54 +01:00
|
|
|
local function setup(sck)
|
2015-03-12 17:27:01 +01:00
|
|
|
-- Set the email user name and password, IMAP tag, and if debugging output is needed
|
2015-03-12 07:16:02 +01:00
|
|
|
imap.config(IMAP_USERNAME,
|
|
|
|
IMAP_PASSWORD,
|
|
|
|
IMAP_TAG,
|
2015-03-12 17:27:01 +01:00
|
|
|
IMAP_DEBUG)
|
2015-03-11 14:32:00 +01:00
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
imap.login(sck)
|
2015-03-11 14:32:00 +01:00
|
|
|
end
|
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
local subject = ""
|
|
|
|
local from = ""
|
2019-12-31 00:53:54 +01:00
|
|
|
local body = ""
|
2015-03-12 07:16:02 +01:00
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
---
|
|
|
|
-- @name do_next
|
|
|
|
-- @description A call back function for a timer alarm used to check if the previous
|
|
|
|
-- IMAP command reply has been processed. If the IMAP reply has been processed
|
|
|
|
-- this function will call the next IMAP command function necessary to read the email
|
2019-12-31 00:53:54 +01:00
|
|
|
local function do_next()
|
2015-03-12 07:16:02 +01:00
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
-- Check if the IMAP reply was processed
|
|
|
|
if(imap.response_processed() == true) then
|
|
|
|
|
|
|
|
-- The IMAP reply was processed
|
2015-03-12 07:16:02 +01:00
|
|
|
|
|
|
|
if (count == 0) then
|
2015-03-12 17:27:01 +01:00
|
|
|
-- After logging in we need to select the email folder from which we wish to read
|
|
|
|
-- in this case the INBOX folder
|
2015-03-12 07:16:02 +01:00
|
|
|
imap.examine(imap_socket,"INBOX")
|
|
|
|
count = count + 1
|
|
|
|
elseif (count == 1) then
|
2015-03-12 17:27:01 +01:00
|
|
|
-- After examining/selecting the INBOX folder we can begin to retrieve emails.
|
2019-12-31 00:53:54 +01:00
|
|
|
-- Retrieve the SUBJECT of the first/newest email
|
|
|
|
imap.fetch_header(imap_socket,imap.get_most_recent_num(),"SUBJECT")
|
2015-03-12 07:16:02 +01:00
|
|
|
count = count + 1
|
|
|
|
elseif (count == 2) then
|
2015-03-12 17:27:01 +01:00
|
|
|
subject = imap.get_header() -- store the SUBJECT response in subject
|
2019-12-31 00:53:54 +01:00
|
|
|
-- Retrieve the FROM of the first/newest email
|
|
|
|
imap.fetch_header(imap_socket,imap.get_most_recent_num(),"FROM")
|
2015-03-12 07:16:02 +01:00
|
|
|
count = count + 1
|
|
|
|
elseif (count == 3) then
|
2015-03-12 17:27:01 +01:00
|
|
|
from = imap.get_header() -- store the FROM response in from
|
2019-12-31 00:53:54 +01:00
|
|
|
-- Retrieve the BODY of the first/newest email
|
|
|
|
imap.fetch_body_plain_text(imap_socket,imap.get_most_recent_num())
|
2015-03-12 07:16:02 +01:00
|
|
|
count = count + 1
|
|
|
|
elseif (count == 4) then
|
2015-03-12 17:27:01 +01:00
|
|
|
body = imap.get_body() -- store the BODY response in body
|
|
|
|
imap.logout(imap_socket) -- Logout of the email account
|
2015-03-12 07:16:02 +01:00
|
|
|
count = count + 1
|
2019-02-17 19:26:29 +01:00
|
|
|
else
|
2015-03-12 17:27:01 +01:00
|
|
|
-- display the email contents
|
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
-- create patterns to strip away IMAP protocol text from actual message
|
|
|
|
local pattern1 = "%*.*}\n" -- to remove "* n command (BODY[n] {n}"
|
|
|
|
local pattern2 = "%)\n.+" -- to remove ") t1 OK command completed"
|
2015-03-12 17:27:01 +01:00
|
|
|
|
|
|
|
from = string.gsub(from,pattern1,"")
|
|
|
|
from = string.gsub(from,pattern2,"")
|
|
|
|
print(from)
|
|
|
|
|
|
|
|
subject = string.gsub(subject,pattern1,"")
|
|
|
|
subject = string.gsub(subject,pattern2,"")
|
|
|
|
print(subject)
|
|
|
|
|
|
|
|
body = string.gsub(body,pattern1,"")
|
|
|
|
body = string.gsub(body,pattern2,"")
|
|
|
|
print("Message: " .. body)
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
timer:stop() -- Stop the timer alarm
|
2015-03-12 17:27:01 +01:00
|
|
|
imap_socket:close() -- close the IMAP socket
|
|
|
|
collectgarbage() -- clean up
|
2015-03-12 07:16:02 +01:00
|
|
|
end
|
|
|
|
end
|
2015-03-11 14:32:00 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
do
|
|
|
|
-- configure the ESP8266 as a station
|
|
|
|
wifi.setmode(wifi.STATION)
|
|
|
|
wifi.sta.config(SSID,SSID_PASSWORD)
|
|
|
|
wifi.sta.autoconnect(1)
|
|
|
|
|
|
|
|
-- create an unencrypted connection
|
|
|
|
imap_socket = net.createConnection(net.TCP,0)
|
|
|
|
imap_socket:on("connection",setup) -- call setup() upon connection
|
|
|
|
imap_socket:connect(IMAP_PORT,IMAP_SERVER) -- connect to the IMAP server
|
|
|
|
|
|
|
|
-- A timer alarm is sued to check if an IMAP reply has been processed
|
|
|
|
timer = tmr.create()
|
|
|
|
timer:alarm(1000, tmr.ALARM_AUTO, do_next)
|
|
|
|
end
|