【问题标题】:NodeMCU SMTP EmailNodeMCU SMTP 电子邮件
【发布时间】:2016-10-23 15:32:24
【问题描述】:

我正在使用 NodeMCU(此处为文档 http://nodemcu.readthedocs.io/)尝试通过 SMTP 发送电子邮件。我从网上得到了这个脚本,一切似乎都正常,因为没有错误,但我没有在我的邮箱中看到任何电子邮件,所以一定是出了问题。不幸的是,使用 display 函数作为发送回调打印 nil。

我能够通过简单的 curl 发送 smtp 电子邮件,因此我知道 google 将通过命令行接受 smtp 请求并且这些设置是正确的。同样根据这个线程,他们正在做同样的事情,将原始字符串发送到 gmail 的 smtp 服务 (http://www.esp8266.com/viewtopic.php?f=24&t=1231&start=8)。

-- The email and password from the account you want to send emails from
MY_EMAIL = "REDACTED"
EMAIL_PASSWORD = "REDACTED"

-- The SMTP server and port of your email provider.
-- If you don't know it google [my email provider] SMTP settings
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = "465"

-- The account you want to send email to
mail_to = "REDACTED"

-- Your access point's SSID and password
SSID = "REDACTED"
SSID_PASSWORD = "REDACTED"

-- configure ESP as a station
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
wifi.sta.autoconnect(1)

email_subject = ""
email_body = ""
count = 0


local smtp_socket = nil -- will be used as socket to email server

-- The display() function will be used to print the SMTP server's response
function display(sck,response)
     print(response)
end

-- The do_next() function is used to send the SMTP commands to the SMTP server in the required sequence.
-- I was going to use socket callbacks but the code would not run callbacks after the first 3.
function do_next()
            if(count == 0)then
                count = count+1
                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("REDACTED".."\r\n")
            elseif(count == 3) then
                count = count + 1
                smtp_socket:send("REDACTED".."\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")
                 print("msg sent")
            else
               smtp_socket:close()
            end
            print(count)
end

-- The connectted() function is executed when the SMTP socket is connected to the SMTP server.
-- This function will create a timer to call the do_next function which will send the SMTP commands
-- in sequence, one by one, every 5000 seconds. 
-- You can change the time to be smaller if that works for you, I used 5000ms just because.
function connected(sck)
    tmr.alarm(0,5000,1,do_next)
end

-- @name send_email
-- @description Will initiated a socket connection to the SMTP server and trigger the connected() function
-- @param subject The email's subject
-- @param body The email's body
function send_email(subject,body)

     count = 0
     email_subject = subject
     email_body = body
     smtp_socket = net.createConnection(net.TCP,0)
     smtp_socket:on("connection",connected)
     smtp_socket:on("receive",display)
    smtp_socket:connect(SMTP_PORT, SMTP_SERVER)     
end
-- Send an email
send_email("ESP8266", "[[Hi, How are your IoT projects coming along? Best Wishes,ESP8266]]")

我可以回答有关此的问题。

【问题讨论】:

    标签: email lua smtp esp8266 nodemcu


    【解决方案1】:

    几点:

    我使用了你的脚本,一个最近的 NodeMCU 固件,将端口更改为 25,添加了 base64 编码用户/密码,并且能够成功地将电子邮件发送给我自己。

    2016 年 7 月 5 日更新

    包含 WiFi 初始化的完整脚本。

    MY_EMAIL = "myself@my-TLD.com"
    MY_EMAIL_B64 = "base64-encoded-email"
    EMAIL_PASSWORD_B64 = "base64-encoded-password"
    
    SMTP_SERVER = "my-ISPs-server"
    SMTP_PORT = 25
    
    mail_to = "myself@my-TLD.com"
    
    email_subject = ""
    email_body = ""
    count = 0
    
    smtp_socket = nil
    
    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(MY_EMAIL_B64.."\r\n")
      elseif(count == 3) then
        count = count + 1
        smtp_socket:send(EMAIL_PASSWORD_B64.."\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,3000,1,do_next)
    end
    
    function send_email(subject,body)
      count = 0
      email_subject = subject
      email_body = body
      smtp_socket = net.createConnection(net.TCP,0)
      smtp_socket:on("connection",connected)
      smtp_socket:on("receive",display)
      smtp_socket:connect(SMTP_PORT,SMTP_SERVER)
    end
    
    SSID = "my-SSID"
    SSID_PASSWORD = "my-WiFi-password"
    wifi.setmode(wifi.STATION)
    wifi.sta.config(SSID,SSID_PASSWORD)
    
    tmr.alarm(1, 1000, 1, function()
      if wifi.sta.getip()== nil then
        print("IP unavaiable, Waiting...")
      else
        tmr.stop(1)
        print("Config done, IP is "..wifi.sta.getip())
        send_email(
          "ESP8266",
          [[Hi,
          How are your IoT projects coming along?
          Best Wishes,
          ESP8266]])
      end
    end)
    

    【讨论】:

    • 还是不行。我将端口更改为 25 并确保遵循其他说明,它告诉我需要发出 starttls 命令。如果我这样做,它仍然拒绝工作。您是否做了其他任何事情来更改脚本以使其正常工作?您是否将连续发送调用更改为像您在 net:send 文档中显示的回调?
    • 已经有一段时间了,在此期间我丢弃了我的测试脚本,但我很快重新组装了它。我怀疑 Gmail 是否会接受端口 25 上的 StartTLS...事实上,我根本不知道 Google 是否接受未加密的连接。因此,您确实可能必须构建启用 SSL 的固件。此脚本(即 NodeMCU 示例脚本)中没有连续的 send 调用,因为它们相隔几秒钟。
    • 对不起,我有点困惑。您说使用端口 25 对您有用,但您不确定 gmail 是否会接受未加密的连接?如果你让它在端口 25 上工作,那不是 gmail 接受不安全的连接吗?
    • 我现在意识到混乱的根源是什么,抱歉。我使用SMTP_SERVER = "my-ISPs-server",这表明我使用Gmail 基础设施——我没有Google 邮件帐户。不过,我也许可以“借用”同事的帐户进行验证。
    • 我明白了,谢谢。明天我会用我的电子邮件@gmx.com 尝试这个,它使用 smtp 端口 25,所以希望它会起作用。
    猜你喜欢
    • 2011-07-30
    • 2018-10-28
    • 2019-10-11
    • 2012-03-09
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    相关资源
    最近更新 更多