read_email_imap.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ---
  2. -- Working Example: https://www.youtube.com/watch?v=PDxTR_KJLhc
  3. -- @author Miguel (AllAboutEE.com)
  4. -- @description This example will read the first email in your inbox using IMAP and
  5. -- display it through serial. The email server must provided unecrypted access. The code
  6. -- was tested with an AOL and Time Warner cable email accounts (GMail and other services who do
  7. -- not support no SSL access will not work).
  8. local imap = require("imap")
  9. local IMAP_USERNAME = "email@domain.com"
  10. local IMAP_PASSWORD = "password"
  11. -- find out your unencrypted imap server and port
  12. -- from your email provided i.e. google "[my email service] imap settings" for example
  13. local IMAP_SERVER = "imap.service.com"
  14. local IMAP_PORT = "143"
  15. local IMAP_TAG = "t1" -- You do not need to change this
  16. local IMAP_DEBUG = true -- change to true if you would like to see the entire conversation between
  17. -- the ESP8266 and IMAP server
  18. local SSID = "ssid"
  19. local SSID_PASSWORD = "password"
  20. local count = 0 -- we will send several IMAP commands/requests, this variable helps keep track of which one to send
  21. local imap_socket, timer
  22. ---
  23. -- @name setup
  24. -- @description A call back function used to begin reading email
  25. -- upon sucessfull connection to the IMAP server
  26. local function setup(sck)
  27. -- Set the email user name and password, IMAP tag, and if debugging output is needed
  28. imap.config(IMAP_USERNAME,
  29. IMAP_PASSWORD,
  30. IMAP_TAG,
  31. IMAP_DEBUG)
  32. imap.login(sck)
  33. end
  34. local subject = ""
  35. local from = ""
  36. local body = ""
  37. ---
  38. -- @name do_next
  39. -- @description A call back function for a timer alarm used to check if the previous
  40. -- IMAP command reply has been processed. If the IMAP reply has been processed
  41. -- this function will call the next IMAP command function necessary to read the email
  42. local function do_next()
  43. -- Check if the IMAP reply was processed
  44. if(imap.response_processed() == true) then
  45. -- The IMAP reply was processed
  46. if (count == 0) then
  47. -- After logging in we need to select the email folder from which we wish to read
  48. -- in this case the INBOX folder
  49. imap.examine(imap_socket,"INBOX")
  50. count = count + 1
  51. elseif (count == 1) then
  52. -- After examining/selecting the INBOX folder we can begin to retrieve emails.
  53. -- Retrieve the SUBJECT of the first/newest email
  54. imap.fetch_header(imap_socket,imap.get_most_recent_num(),"SUBJECT")
  55. count = count + 1
  56. elseif (count == 2) then
  57. subject = imap.get_header() -- store the SUBJECT response in subject
  58. -- Retrieve the FROM of the first/newest email
  59. imap.fetch_header(imap_socket,imap.get_most_recent_num(),"FROM")
  60. count = count + 1
  61. elseif (count == 3) then
  62. from = imap.get_header() -- store the FROM response in from
  63. -- Retrieve the BODY of the first/newest email
  64. imap.fetch_body_plain_text(imap_socket,imap.get_most_recent_num())
  65. count = count + 1
  66. elseif (count == 4) then
  67. body = imap.get_body() -- store the BODY response in body
  68. imap.logout(imap_socket) -- Logout of the email account
  69. count = count + 1
  70. else
  71. -- display the email contents
  72. -- create patterns to strip away IMAP protocol text from actual message
  73. local pattern1 = "%*.*}\n" -- to remove "* n command (BODY[n] {n}"
  74. local pattern2 = "%)\n.+" -- to remove ") t1 OK command completed"
  75. from = string.gsub(from,pattern1,"")
  76. from = string.gsub(from,pattern2,"")
  77. print(from)
  78. subject = string.gsub(subject,pattern1,"")
  79. subject = string.gsub(subject,pattern2,"")
  80. print(subject)
  81. body = string.gsub(body,pattern1,"")
  82. body = string.gsub(body,pattern2,"")
  83. print("Message: " .. body)
  84. timer:stop() -- Stop the timer alarm
  85. imap_socket:close() -- close the IMAP socket
  86. collectgarbage() -- clean up
  87. end
  88. end
  89. end
  90. do
  91. -- configure the ESP8266 as a station
  92. wifi.setmode(wifi.STATION)
  93. wifi.sta.config(SSID,SSID_PASSWORD)
  94. wifi.sta.autoconnect(1)
  95. -- create an unencrypted connection
  96. imap_socket = net.createConnection(net.TCP,0)
  97. imap_socket:on("connection",setup) -- call setup() upon connection
  98. imap_socket:connect(IMAP_PORT,IMAP_SERVER) -- connect to the IMAP server
  99. -- A timer alarm is sued to check if an IMAP reply has been processed
  100. timer = tmr.create()
  101. timer:alarm(1000, tmr.ALARM_AUTO, do_next)
  102. end