-- LUA from https://nodemcu-build.com/ (SSL True, module crypto, file,gpio, -- http, net, node, tmr, uart, websocket, wifi, tls) -- for access to local wifi: local SSID = "xxxx" local SSID_PASSWORD = "xxxxxxxxxxxxxxxxxx" -- email provider data: local email_subject = "" local email_body = "" local count = 0 local MY_EMAIL = "mail@xxxx.de" local EMAIL_PASSWORD = "xxxxxxxxx" local mail_to = "mail@xxxx.de" -- The SMTP server and port of the email provider: local SMTP_SERVER = "smtp.xxx.com" local SMTP_PORT = "465" local smtp_socket = nil -- will be used as socket to email server -- Connect to local wifi: wifi.setmode(wifi.STATION) wifi.sta.config(SSID,SSID_PASSWORD) wifi.sta.connect() -- wait with 1second timer until connection is ready: tmr.alarm(1, 1000, tmr.ALARM_AUTO, function() if wifi.sta.getip() == nil then print("Wifi not jet ready...") else tmr.stop(1) print("Wifi ready, IP is "..wifi.sta.getip()) -- Send the email send_email( "ESP8266", "ESP8266: " .. tmr.time()/60 .."min Uptime") -- never reached, deepsleep at the end of do_next() end end) -- The do_next() function is used to send the SMTP commands to the SMTP server -- in the required sequence after connection to the smtp server 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(crypto.toBase64(MY_EMAIL).."\r\n") elseif(count == 3) then count = count + 1 smtp_socket:send(crypto.toBase64(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 -- no single . line in message allowed, so string.gsub( ) deletes such lines 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 smtp_socket:send("QUIT\r\n") elseif(count == 9) then smtp_socket:close() print("email send..") print("Socket Closed, goto sleep") tmr.stop(0) node.dsleep(0) --deepsleep bis Reset or CH_PD Button pressed return else end end -- callback functions for socket events "connection" und "receive" -- do_next function wird alle 0.5s aufgerufen um nŠchstes smtp kommando zu senden function connected(sck) tmr.alarm(0,500,tmr.ALARM_AUTO,do_next) print("3") end -- display_received function prints die vom smtp server empfangenen Antworten function display_received(sck,response) print(response) if(string.match(response,"%d+") == 221) then gpio.mode(4, gpio.OUTPUT) gpio.write(4,gpio.LOW) else gpio.write(4,gpio.HIGH) end end function send_email(subject,body) count = 0 email_subject = subject email_body = body --smtp_socket = tls.createConnection() smtp_socket = net.createConnection(net.TCP,1) -- creates a TCP Client for secure connection print("1") smtp_socket:on("connection",connected) -- callbach function for "connection" event smtp_socket:on("receive",display_received) -- callback function for "receive" event smtp_socket:dns(SMTP_SERVER,function(conn,ip) print("dns ".. ip) end) smtp_socket:connect(SMTP_PORT,SMTP_SERVER) -- Verbindung zum smtp server aufbauen print("2") end