send_email_smtp.lua 4.6 KB

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