sendmail=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