diff --git a/lua_examples/email/send_email_smtp.lua b/lua_examples/email/send_email_smtp.lua new file mode 100644 index 00000000..637cd638 --- /dev/null +++ b/lua_examples/email/send_email_smtp.lua @@ -0,0 +1,96 @@ +require("base64") + +local MY_EMAIL = "esp8266@domain.com" +local EMAIL_PASSWORD = "123456" + +local SMTP_SERVER = "smtp.server.com" +local SMTP_PORT = "587" + +local mail_to = "to_email@domain.com" + +local SSID = "ssid" +local SSID_PASSWORD = "password" + +wifi.setmode(wifi.STATION) +wifi.sta.config(SSID,SSID_PASSWORD) +wifi.sta.autoconnect(1) + +local email_subject = "" +local email_body = "" +local receive_processed = false +local count = 0 + +local smtp_socket = net.createConnection(net.TCP,0) + +function display(sck,response) + print(response) +end + +function do_next() + if(count == 0)then + count = count+1 + local IP_ADDRESS = wifi.sta.getip() + smtp_socket:send("HELO "..IP_ADDRESS.."\r\n") + elseif(count==1) then + count = count+1 + smtp_socket:send("AUTH LOGIN\r\n") + elseif(count == 2) then + count = count + 1 + smtp_socket:send(base64.enc(MY_EMAIL).."\r\n") + elseif(count == 3) then + count = count + 1 + smtp_socket:send(base64.enc(EMAIL_PASSWORD).."\r\n") + elseif(count==4) then + count = count+1 + smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n") + elseif(count==5) then + count = count+1 + smtp_socket:send("RCPT TO:<" .. mail_to ..">\r\n") + elseif(count==6) then + count = count+1 + smtp_socket:send("DATA\r\n") + elseif(count==7) then + count = count+1 + local message = string.gsub( + "From: \"".. MY_EMAIL .."\"<"..MY_EMAIL..">\r\n" .. + "To: \"".. mail_to .. "\"<".. mail_to..">\r\n".. + "Subject: ".. email_subject .. "\r\n\r\n" .. + email_body,"\r\n.\r\n","") + + smtp_socket:send(message.."\r\n.\r\n") + elseif(count==8) then + count = count+1 + tmr.stop(0) + smtp_socket:send("QUIT\r\n") + else + smtp_socket:close() + end +end + +function connected(sck) + tmr.alarm(0,5000,1,do_next) +end + +function send_email(subject,body) + email_subject = subject + email_body = body + smtp_socket:on("connection",connected) + smtp_socket:on("receive",display) + smtp_socket:connect(SMTP_PORT,SMTP_SERVER) +end + +send_email( + "ESP8266", +[[Hi, +How are your IoT projects coming along? +Best Wishes, +ESP8266]]) + + + + + + + + + diff --git a/lua_modules/base64/base64.lua b/lua_modules/base64/base64.lua new file mode 100644 index 00000000..e3523a31 --- /dev/null +++ b/lua_modules/base64/base64.lua @@ -0,0 +1,41 @@ +-- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss +-- licensed under the terms of the LGPL2 + +local moduleName = ... +local M = {} +_G[moduleName] = M + +-- character table string +local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + +-- encoding +function M.enc(data) + return ((data:gsub('.', function(x) + local r,b='',x:byte() + for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end + return r; + end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x) + if (#x < 6) then return '' end + local c=0 + for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end + return b:sub(c+1,c+1) + end)..({ '', '==', '=' })[#data%3+1]) +end + +-- decoding +function M.dec(data) + data = string.gsub(data, '[^'..b..'=]', '') + return (data:gsub('.', function(x) + if (x == '=') then return '' end + local r,f='',(b:find(x)-1) + for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end + return r; + end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x) + if (#x ~= 8) then return '' end + local c=0 + for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(7-i) or 0) end + return string.char(c) + end)) +end + +return M