As the title says... Chat on...

User avatar
By ThomasW
#7174 This works in lua but takes a lot of memory:
Code: Select allsendmail=function(from,to,subject,body,server)

   local handle_response = function(conn,response,step,state)
      -- print("handling state:" .. state)
      -- print(node.heap())
      -- print("expecting:" .. step[state].expected  .. " got:" .. response)
      -- if string.match(response,step[state].expected )
      if response:match(step[state].expected )
      then
         if step[state].request
         then
            conn:send(step[state].request .. "\r\n")
         else
            conn:close()
         end
         state = state + 1
      else
         conn:close()
      end
      return(state)
   end

   local step={}
   local smtp_server,smtp_port
   smtp_server,smtp_port = server:match("(.*):(.*)")

   local domain=from:match("@(.*)$")

   step[1]={expected="^220",request="HELO " .. domain}
   step[2]={expected="^250",request="MAIL FROM:<" .. from ..">"}
   step[3]={expected="^250",request="RCPT TO:<" .. to .. ">"} -- from
   step[4]={expected="^250",request="DATA"} -- to
   step[5]={expected="^354"} -- data
   step[5].request = string.format("TO:%s\r\nSUBJECT:%s\r\n\r\n%s\r\n.",to,subject,body)
   step[6]={expected="^250",request = "QUIT"} -- headers/message
   step[7]={expected="^221"}  -- quit

   local conn=net.createConnection(net.TCP, false)
   local state=0
   conn:on("receive", function(conn, payload)
      print("received:" .. payload)
      state=handle_response(conn,payload,step,state)
   end )
   conn:on("connection", function(conn)
      print("connected") state = 1
   end )
   conn:on("disconnection", function(conn)
      print("disconnected:" .. state)
      conn:close()
      conn=nil
      collectgarbage()
   end )
   conn:on("sent", function(conn)
      print("sent data")
   end )

   print("connecting to " .. smtp_server .. ":" .. smtp_port)
   conn:connect(smtp_port,smtp_server)
end

sendmail("from@xxx.yyy","to@xxx.yyy","subject","body","smtp.server.xxx:25")

Thomas
User avatar
By awball
#7249 Looks good! Would I save this as a .lua file then run it as a function? Actually I just need to find some time and to update the lua firmware and learn through trial and error. It's the best way to learn. It looks promising might need to set it up and test at work.

I think a major selling point of lua is you don't need to compile. Building the environment to make hello world happen is possibly one of the hardest parts.
User avatar
By raz123
#7329
ThomasW wrote:This works in lua but takes a lot of memory:
Code: Select allsendmail=function(from,to,subject,body,server)

   local handle_response = function(conn,response,step,state)
      -- print("handling state:" .. state)
      -- print(node.heap())
      -- print("expecting:" .. step[state].expected  .. " got:" .. response)
      -- if string.match(response,step[state].expected )
      if response:match(step[state].expected )
      then
         if step[state].request
         then
            conn:send(step[state].request .. "\r\n")
         else
            conn:close()
         end
         state = state + 1
      else
         conn:close()
      end
      return(state)
   end

   local step={}
   local smtp_server,smtp_port
   smtp_server,smtp_port = server:match("(.*):(.*)")

   local domain=from:match("@(.*)$")

   step[1]={expected="^220",request="HELO " .. domain}
   step[2]={expected="^250",request="MAIL FROM:<" .. from ..">"}
   step[3]={expected="^250",request="RCPT TO:<" .. to .. ">"} -- from
   step[4]={expected="^250",request="DATA"} -- to
   step[5]={expected="^354"} -- data
   step[5].request = string.format("TO:%s\r\nSUBJECT:%s\r\n\r\n%s\r\n.",to,subject,body)
   step[6]={expected="^250",request = "QUIT"} -- headers/message
   step[7]={expected="^221"}  -- quit

   local conn=net.createConnection(net.TCP, false)
   local state=0
   conn:on("receive", function(conn, payload)
      print("received:" .. payload)
      state=handle_response(conn,payload,step,state)
   end )
   conn:on("connection", function(conn)
      print("connected") state = 1
   end )
   conn:on("disconnection", function(conn)
      print("disconnected:" .. state)
      conn:close()
      conn=nil
      collectgarbage()
   end )
   conn:on("sent", function(conn)
      print("sent data")
   end )

   print("connecting to " .. smtp_server .. ":" .. smtp_port)
   conn:connect(smtp_port,smtp_server)
end

sendmail("from@xxx.yyy","to@xxx.yyy","subject","body","smtp.server.xxx:25")

Thomas


Could the email body be a file attachment from the ESP8266's flash? Could be useful if you'd want to transmit a .log file.