send_email_smtp.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ---
  2. -- Working Example: https://www.youtube.com/watch?v=CcRbFIJ8aeU
  3. -- @description a basic SMTP email example. You must use an account which can provide unencrypted authenticated access.
  4. -- This example was tested with an AOL and Time Warner email accounts. GMail does not offer unecrypted authenticated access.
  5. -- To obtain your email's SMTP server and port simply Google it e.g. [my email domain] SMTP settings
  6. -- 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
  7. -- To Learn more about SMTP email visit:
  8. -- SMTP Commands Reference - http://www.samlogic.net/articles/smtp-commands-reference.htm
  9. -- See "SMTP transport example" in this page http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
  10. -- @author Miguel
  11. require("base64")
  12. -- The email and password from the account you want to send emails from
  13. local MY_EMAIL = "esp8266@domain.com"
  14. local EMAIL_PASSWORD = "123456"
  15. -- The SMTP server and port of your email provider.
  16. -- If you don't know it google [my email provider] SMTP settings
  17. local SMTP_SERVER = "smtp.server.com"
  18. local SMTP_PORT = "587"
  19. -- The account you want to send email to
  20. local mail_to = "to_email@domain.com"
  21. -- Your access point's SSID and password
  22. local SSID = "ssid"
  23. local SSID_PASSWORD = "password"
  24. -- configure ESP as a station
  25. wifi.setmode(wifi.STATION)
  26. wifi.sta.config(SSID,SSID_PASSWORD)
  27. wifi.sta.autoconnect(1)
  28. -- These are global variables. Don't change their values
  29. -- they will be changed in the functions below
  30. local email_subject = ""
  31. local email_body = ""
  32. local count = 0
  33. local smtp_socket = nil -- will be used as socket to email server
  34. -- The display() function will be used to print the SMTP server's response
  35. function display(sck,response)
  36. print(response)
  37. end
  38. -- The do_next() function is used to send the SMTP commands to the SMTP server in the required sequence.
  39. -- I was going to use socket callbacks but the code would not run callbacks after the first 3.
  40. function do_next()
  41. if(count == 0)then
  42. count = count+1
  43. local IP_ADDRESS = wifi.sta.getip()
  44. smtp_socket:send("HELO "..IP_ADDRESS.."\r\n")
  45. elseif(count==1) then
  46. count = count+1
  47. smtp_socket:send("AUTH LOGIN\r\n")
  48. elseif(count == 2) then
  49. count = count + 1
  50. smtp_socket:send(base64.enc(MY_EMAIL).."\r\n")
  51. elseif(count == 3) then
  52. count = count + 1
  53. smtp_socket:send(base64.enc(EMAIL_PASSWORD).."\r\n")
  54. elseif(count==4) then
  55. count = count+1
  56. smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n")
  57. elseif(count==5) then
  58. count = count+1
  59. smtp_socket:send("RCPT TO:<" .. mail_to ..">\r\n")
  60. elseif(count==6) then
  61. count = count+1
  62. smtp_socket:send("DATA\r\n")
  63. elseif(count==7) then
  64. count = count+1
  65. local message = string.gsub(
  66. "From: \"".. MY_EMAIL .."\"<"..MY_EMAIL..">\r\n" ..
  67. "To: \"".. mail_to .. "\"<".. mail_to..">\r\n"..
  68. "Subject: ".. email_subject .. "\r\n\r\n" ..
  69. email_body,"\r\n.\r\n","")
  70. smtp_socket:send(message.."\r\n.\r\n")
  71. elseif(count==8) then
  72. count = count+1
  73. tmr.stop(0)
  74. smtp_socket:send("QUIT\r\n")
  75. else
  76. smtp_socket:close()
  77. end
  78. end
  79. -- The connectted() function is executed when the SMTP socket is connected to the SMTP server.
  80. -- This function will create a timer to call the do_next function which will send the SMTP commands
  81. -- in sequence, one by one, every 5000 seconds.
  82. -- You can change the time to be smaller if that works for you, I used 5000ms just because.
  83. function connected(sck)
  84. tmr.alarm(0,5000,1,do_next)
  85. end
  86. -- @name send_email
  87. -- @description Will initiated a socket connection to the SMTP server and trigger the connected() function
  88. -- @param subject The email's subject
  89. -- @param body The email's body
  90. function send_email(subject,body)
  91. count = 0
  92. email_subject = subject
  93. email_body = body
  94. smtp_socket = net.createConnection(net.TCP,0)
  95. smtp_socket:on("connection",connected)
  96. smtp_socket:on("receive",display)
  97. smtp_socket:connect(SMTP_PORT,SMTP_SERVER)
  98. end
  99. -- Send an email
  100. send_email(
  101. "ESP8266",
  102. [[Hi,
  103. How are your IoT projects coming along?
  104. Best Wishes,
  105. ESP8266]])