2015-03-11 14:32:00 +01:00
|
|
|
---
|
|
|
|
-- @author Miguel (AllAboutEE.com)
|
2015-03-12 17:27:01 +01:00
|
|
|
-- @description This example will read the first email in your inbox using IMAP and
|
|
|
|
-- display it through serial. The email server must provided unecrypted access. The code
|
|
|
|
-- was tested with an AOL and Time Waraner cable email ccounts (GMail and other services who do
|
|
|
|
-- not support no SSL access will not work).
|
2015-03-11 14:32:00 +01:00
|
|
|
|
2015-03-12 07:16:02 +01:00
|
|
|
require("imap")
|
2015-03-11 14:32:00 +01:00
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
local IMAP_USERNAME = "email@domain.com"
|
|
|
|
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
|
|
|
|
local IMAP_DEBUG = false -- change to true if you would like to see the entire conversation between
|
|
|
|
-- 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
|
2015-03-11 14:32:00 +01:00
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
-- configure the ESP8266 as a station
|
2015-03-11 14:32:00 +01:00
|
|
|
wifi.setmode(wifi.STATION)
|
|
|
|
wifi.sta.config(SSID,SSID_PASSWORD)
|
|
|
|
wifi.sta.autoconnect(1)
|
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
-- create an unencrypted connection
|
2015-03-12 07:16:02 +01:00
|
|
|
local imap_socket = net.createConnection(net.TCP,0)
|
2015-03-11 14:32:00 +01:00
|
|
|
|
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
---
|
|
|
|
-- @name setup
|
|
|
|
-- @description A call back function used to begin reading email
|
|
|
|
-- upon sucessfull connection to the IMAP server
|
2015-03-12 07:16:02 +01:00
|
|
|
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
|
|
|
imap_socket:on("connection",setup) -- call setup() upon connection
|
|
|
|
imap_socket:connect(IMAP_PORT,IMAP_SERVER) -- connect to the IMAP server
|
|
|
|
|
|
|
|
local subject = ""
|
|
|
|
local from = ""
|
|
|
|
local message = ""
|
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
|
2015-03-12 07:16:02 +01:00
|
|
|
function do_next()
|
|
|
|
|
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.
|
|
|
|
imap.fetch_header(imap_socket,imap.get_most_recent_num(),"SUBJECT") -- Retrieve the SUBJECT of the first/newest email
|
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
|
|
|
|
imap.fetch_header(imap_socket,imap.get_most_recent_num(),"FROM") -- Retrieve the FROM of the first/newest email
|
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
|
|
|
|
imap.fetch_body_plain_text(imap_socket,imap.get_most_recent_num()) -- Retrieve the BODY of the first/newest email
|
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
|
|
|
|
else
|
2015-03-12 17:27:01 +01:00
|
|
|
-- display the email contents
|
|
|
|
|
|
|
|
-- create patterns to strip away IMAP protocl text from actual message
|
|
|
|
pattern1 = "(\*.+\}\r\n)" -- to remove "* n command (BODY[n] {n}"
|
|
|
|
pattern2 = "(\r\n.+)" -- to remove ") t1 OK command completed"
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
tmr.stop(0) -- Stop the timer alarm
|
|
|
|
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
|
|
|
|
|
2015-03-12 17:27:01 +01:00
|
|
|
-- A timer alarm is sued to check if an IMAP reply has been processed
|
2015-03-12 07:16:02 +01:00
|
|
|
tmr.alarm(0,1000,1, do_next)
|