read_email_imap.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. 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. -- configure the ESP8266 as a station
  22. wifi.setmode(wifi.STATION)
  23. wifi.sta.config(SSID,SSID_PASSWORD)
  24. wifi.sta.autoconnect(1)
  25. -- create an unencrypted connection
  26. local imap_socket = net.createConnection(net.TCP,0)
  27. ---
  28. -- @name setup
  29. -- @description A call back function used to begin reading email
  30. -- upon sucessfull connection to the IMAP server
  31. function setup(sck)
  32. -- Set the email user name and password, IMAP tag, and if debugging output is needed
  33. imap.config(IMAP_USERNAME,
  34. IMAP_PASSWORD,
  35. IMAP_TAG,
  36. IMAP_DEBUG)
  37. imap.login(sck)
  38. end
  39. imap_socket:on("connection",setup) -- call setup() upon connection
  40. imap_socket:connect(IMAP_PORT,IMAP_SERVER) -- connect to the IMAP server
  41. local subject = ""
  42. local from = ""
  43. local message = ""
  44. ---
  45. -- @name do_next
  46. -- @description A call back function for a timer alarm used to check if the previous
  47. -- IMAP command reply has been processed. If the IMAP reply has been processed
  48. -- this function will call the next IMAP command function necessary to read the email
  49. function do_next()
  50. -- Check if the IMAP reply was processed
  51. if(imap.response_processed() == true) then
  52. -- The IMAP reply was processed
  53. if (count == 0) then
  54. -- After logging in we need to select the email folder from which we wish to read
  55. -- in this case the INBOX folder
  56. imap.examine(imap_socket,"INBOX")
  57. count = count + 1
  58. elseif (count == 1) then
  59. -- After examining/selecting the INBOX folder we can begin to retrieve emails.
  60. imap.fetch_header(imap_socket,imap.get_most_recent_num(),"SUBJECT") -- Retrieve the SUBJECT of the first/newest email
  61. count = count + 1
  62. elseif (count == 2) then
  63. subject = imap.get_header() -- store the SUBJECT response in subject
  64. imap.fetch_header(imap_socket,imap.get_most_recent_num(),"FROM") -- Retrieve the FROM of the first/newest email
  65. count = count + 1
  66. elseif (count == 3) then
  67. from = imap.get_header() -- store the FROM response in from
  68. imap.fetch_body_plain_text(imap_socket,imap.get_most_recent_num()) -- Retrieve the BODY of the first/newest email
  69. count = count + 1
  70. elseif (count == 4) then
  71. body = imap.get_body() -- store the BODY response in body
  72. imap.logout(imap_socket) -- Logout of the email account
  73. count = count + 1
  74. else
  75. -- display the email contents
  76. -- create patterns to strip away IMAP protocl text from actual message
  77. pattern1 = "(\*.+\}\r\n)" -- to remove "* n command (BODY[n] {n}"
  78. pattern2 = "(%)\r\n.+)" -- to remove ") t1 OK command completed"
  79. from = string.gsub(from,pattern1,"")
  80. from = string.gsub(from,pattern2,"")
  81. print(from)
  82. subject = string.gsub(subject,pattern1,"")
  83. subject = string.gsub(subject,pattern2,"")
  84. print(subject)
  85. body = string.gsub(body,pattern1,"")
  86. body = string.gsub(body,pattern2,"")
  87. print("Message: " .. body)
  88. tmr.stop(0) -- Stop the timer alarm
  89. imap_socket:close() -- close the IMAP socket
  90. collectgarbage() -- clean up
  91. end
  92. end
  93. end
  94. -- A timer alarm is sued to check if an IMAP reply has been processed
  95. tmr.alarm(0,1000,1, do_next)