2001-03-27 21:25:11 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
-- SMTP support for the Lua language.
|
2002-07-08 23:01:45 +02:00
|
|
|
-- LuaSocket 1.5 toolkit
|
2001-03-27 21:25:11 +02:00
|
|
|
-- Author: Diego Nehab
|
2001-09-12 20:16:31 +02:00
|
|
|
-- Conforming to: RFC 821, LTN7
|
|
|
|
-- RCS ID: $Id$
|
2001-03-27 21:25:11 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
2001-09-12 20:16:31 +02:00
|
|
|
local Public, Private = {}, {}
|
2003-03-20 01:24:44 +01:00
|
|
|
socket.smtp = Public
|
2001-09-12 20:16:31 +02:00
|
|
|
|
2001-03-27 21:25:11 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Program constants
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- timeout in secconds before we give up waiting
|
2001-09-12 20:16:31 +02:00
|
|
|
Public.TIMEOUT = 180
|
2001-03-27 21:25:11 +02:00
|
|
|
-- port used for connection
|
2001-09-12 20:16:31 +02:00
|
|
|
Public.PORT = 25
|
|
|
|
-- domain used in HELO command and default sendmail
|
|
|
|
-- If we are under a CGI, try to get from environment
|
2002-12-03 00:34:41 +01:00
|
|
|
Public.DOMAIN = os.getenv("SERVER_NAME") or "localhost"
|
2001-09-12 20:16:31 +02:00
|
|
|
-- default server used to send e-mails
|
|
|
|
Public.SERVER = "localhost"
|
2001-03-27 21:25:11 +02:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
-- Tries to send data through socket. Closes socket on error.
|
2001-03-27 21:25:11 +02:00
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
2001-09-12 20:16:31 +02:00
|
|
|
-- data: string to be sent
|
2001-03-27 21:25:11 +02:00
|
|
|
-- Returns
|
|
|
|
-- err: message in case of error, nil if successfull
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Private.try_send(sock, data)
|
|
|
|
local err = sock:send(data)
|
2001-03-27 21:25:11 +02:00
|
|
|
if err then sock:close() end
|
|
|
|
return err
|
|
|
|
end
|
|
|
|
|
2001-09-12 20:16:31 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Tries to get a pattern from the server and closes socket on error
|
|
|
|
-- sock: socket opened to the server
|
|
|
|
-- ...: pattern to receive
|
|
|
|
-- Returns
|
|
|
|
-- ...: received pattern
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.try_receive(...)
|
|
|
|
local sock = arg[1]
|
2003-03-20 01:24:44 +01:00
|
|
|
local data, err = sock.receive(unpack(arg))
|
2001-09-12 20:16:31 +02:00
|
|
|
if err then sock:close() end
|
|
|
|
return data, err
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends a command to the server (closes sock on error)
|
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
|
|
|
-- command: command to be sent
|
|
|
|
-- param: command parameters if any
|
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.send_command(sock, command, param)
|
|
|
|
local line
|
|
|
|
if param then line = command .. " " .. param .. "\r\n"
|
|
|
|
else line = command .. "\r\n" end
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.try_send(sock, line)
|
2001-09-12 20:16:31 +02:00
|
|
|
end
|
|
|
|
|
2001-03-27 21:25:11 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Gets command reply, (accepts multiple-line replies)
|
|
|
|
-- Input
|
2001-09-12 20:16:31 +02:00
|
|
|
-- control: control openion socket
|
2001-03-27 21:25:11 +02:00
|
|
|
-- Returns
|
|
|
|
-- answer: whole server reply, nil if error
|
|
|
|
-- code: reply status code or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Private.get_answer(control)
|
|
|
|
local code, lastcode, sep, _
|
2002-07-08 23:01:45 +02:00
|
|
|
local line, err = Private.try_receive(control)
|
2001-03-27 21:25:11 +02:00
|
|
|
local answer = line
|
|
|
|
if err then return nil, err end
|
2003-03-20 01:24:44 +01:00
|
|
|
_,_, code, sep = string.find(line, "^(%d%d%d)(.)")
|
2001-03-27 21:25:11 +02:00
|
|
|
if not code or not sep then return nil, answer end
|
|
|
|
if sep == "-" then -- answer is multiline
|
|
|
|
repeat
|
2002-07-08 23:01:45 +02:00
|
|
|
line, err = Private.try_receive(control)
|
2001-03-27 21:25:11 +02:00
|
|
|
if err then return nil, err end
|
2003-03-20 01:24:44 +01:00
|
|
|
_,_, lastcode, sep = string.find(line, "^(%d%d%d)(.)")
|
2001-03-27 21:25:11 +02:00
|
|
|
answer = answer .. "\n" .. line
|
|
|
|
until code == lastcode and sep == " " -- answer ends with same code
|
|
|
|
end
|
|
|
|
return answer, tonumber(code)
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
-- Checks if a message reply code is correct. Closes control openion
|
2001-03-27 21:25:11 +02:00
|
|
|
-- if not.
|
|
|
|
-- Input
|
2001-09-12 20:16:31 +02:00
|
|
|
-- control: control openion socket
|
2001-03-27 21:25:11 +02:00
|
|
|
-- success: table with successfull reply status code
|
|
|
|
-- Returns
|
|
|
|
-- code: reply code or nil in case of error
|
|
|
|
-- answer: complete server answer or system error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Private.check_answer(control, success)
|
2002-07-08 23:01:45 +02:00
|
|
|
local answer, code = Private.get_answer(control)
|
2001-09-12 20:16:31 +02:00
|
|
|
if not answer then return nil, code end
|
2001-03-27 21:25:11 +02:00
|
|
|
if type(success) ~= "table" then success = {success} end
|
2003-03-20 01:24:44 +01:00
|
|
|
for i = 1, table.getn(success) do
|
2001-03-27 21:25:11 +02:00
|
|
|
if code == success[i] then
|
|
|
|
return code, answer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
control:close()
|
|
|
|
return nil, answer
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends initial client greeting
|
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
|
|
|
-- Returns
|
|
|
|
-- code: server code if ok, nil if error
|
|
|
|
-- answer: complete server reply
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Private.send_helo(sock)
|
2002-07-08 23:01:45 +02:00
|
|
|
local err = Private.send_command(sock, "HELO", Public.DOMAIN)
|
2001-03-27 21:25:11 +02:00
|
|
|
if err then return nil, err end
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.check_answer(sock, 250)
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
-- Sends openion termination command
|
2001-03-27 21:25:11 +02:00
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
|
|
|
-- Returns
|
|
|
|
-- code: server status code, nil if error
|
|
|
|
-- answer: complete server reply or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Private.send_quit(sock)
|
2002-07-08 23:01:45 +02:00
|
|
|
local err = Private.send_command(sock, "QUIT")
|
2001-03-27 21:25:11 +02:00
|
|
|
if err then return nil, err end
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, answer = Private.check_answer(sock, 221)
|
2001-03-27 21:25:11 +02:00
|
|
|
sock:close()
|
|
|
|
return code, answer
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends sender command
|
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
|
|
|
-- sender: e-mail of sender
|
|
|
|
-- Returns
|
|
|
|
-- code: server status code, nil if error
|
|
|
|
-- answer: complete server reply or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Private.send_mail(sock, sender)
|
2003-03-20 01:24:44 +01:00
|
|
|
local param = string.format("FROM:<%s>", sender or "")
|
2002-07-08 23:01:45 +02:00
|
|
|
local err = Private.send_command(sock, "MAIL", param)
|
2001-03-27 21:25:11 +02:00
|
|
|
if err then return nil, err end
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.check_answer(sock, 250)
|
2001-09-12 20:16:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends mime headers
|
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
|
|
|
-- headers: table with mime headers to be sent
|
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.send_headers(sock, headers)
|
|
|
|
local err
|
|
|
|
-- send request headers
|
|
|
|
for i, v in headers or {} do
|
2002-07-08 23:01:45 +02:00
|
|
|
err = Private.try_send(sock, i .. ": " .. v .. "\r\n")
|
2001-09-12 20:16:31 +02:00
|
|
|
if err then return err end
|
|
|
|
end
|
|
|
|
-- mark end of request headers
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.try_send(sock, "\r\n")
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends message mime headers and body
|
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
2001-09-12 20:16:31 +02:00
|
|
|
-- headers: table containing all mime headers to be sent
|
2001-03-27 21:25:11 +02:00
|
|
|
-- body: message body
|
|
|
|
-- Returns
|
|
|
|
-- code: server status code, nil if error
|
|
|
|
-- answer: complete server reply or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Private.send_data(sock, headers, body)
|
2002-07-08 23:01:45 +02:00
|
|
|
local err = Private.send_command(sock, "DATA")
|
2001-03-27 21:25:11 +02:00
|
|
|
if err then return nil, err end
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, answer = Private.check_answer(sock, 354)
|
2001-03-27 21:25:11 +02:00
|
|
|
if not code then return nil, answer end
|
|
|
|
-- avoid premature end in message body
|
2003-03-20 01:24:44 +01:00
|
|
|
body = string.gsub(body or "", "\n%.", "\n%.%.")
|
2001-03-27 21:25:11 +02:00
|
|
|
-- mark end of message body
|
2001-09-12 20:16:31 +02:00
|
|
|
body = body .. "\r\n.\r\n"
|
2002-07-08 23:01:45 +02:00
|
|
|
err = Private.send_headers(sock, headers)
|
2001-03-27 21:25:11 +02:00
|
|
|
if err then return nil, err end
|
2002-07-08 23:01:45 +02:00
|
|
|
err = Private.try_send(sock, body)
|
|
|
|
return Private.check_answer(sock, 250)
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends recipient list command
|
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
|
|
|
-- rcpt: lua table with recipient list
|
|
|
|
-- Returns
|
|
|
|
-- code: server status code, nil if error
|
|
|
|
-- answer: complete server reply
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Private.send_rcpt(sock, rcpt)
|
|
|
|
local err
|
|
|
|
local code, answer = nil, "No recipient specified"
|
2001-03-27 21:25:11 +02:00
|
|
|
if type(rcpt) ~= "table" then rcpt = {rcpt} end
|
2003-03-20 01:24:44 +01:00
|
|
|
for i = 1, table.getn(rcpt) do
|
|
|
|
err = Private.send_command(sock, "RCPT",
|
|
|
|
string.format("TO:<%s>", rcpt[i]))
|
2001-03-27 21:25:11 +02:00
|
|
|
if err then return nil, err end
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.check_answer(sock, {250, 251})
|
2001-03-27 21:25:11 +02:00
|
|
|
if not code then return code, answer end
|
|
|
|
end
|
|
|
|
return code, answer
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
-- Starts the connection and greets server
|
|
|
|
-- Input
|
|
|
|
-- parsed: parsed URL components
|
|
|
|
-- Returns
|
|
|
|
-- sock: socket connected to server
|
|
|
|
-- err: error message if any
|
2001-03-27 21:25:11 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Private.open(server)
|
2001-03-27 21:25:11 +02:00
|
|
|
local code, answer
|
2001-09-12 20:16:31 +02:00
|
|
|
-- default server
|
2002-07-08 23:01:45 +02:00
|
|
|
server = server or Public.SERVER
|
2001-09-12 20:16:31 +02:00
|
|
|
-- connect to server and make sure we won't hang
|
2003-03-20 01:24:44 +01:00
|
|
|
local sock, err = socket.connect(server, Public.PORT)
|
2001-03-27 21:25:11 +02:00
|
|
|
if not sock then return nil, err end
|
2002-07-08 23:01:45 +02:00
|
|
|
sock:timeout(Public.TIMEOUT)
|
2001-03-27 21:25:11 +02:00
|
|
|
-- initial server greeting
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.check_answer(sock, 220)
|
2001-03-27 21:25:11 +02:00
|
|
|
if not code then return nil, answer end
|
|
|
|
-- HELO
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.send_helo(sock)
|
2001-03-27 21:25:11 +02:00
|
|
|
if not code then return nil, answer end
|
|
|
|
return sock
|
|
|
|
end
|
|
|
|
|
2001-09-12 20:16:31 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends a message using an opened server
|
|
|
|
-- Input
|
|
|
|
-- sock: socket connected to server
|
|
|
|
-- message: a table with the following fields:
|
|
|
|
-- from: message sender's e-mail
|
|
|
|
-- rcpt: message recipient's e-mail
|
|
|
|
-- headers: message mime headers
|
|
|
|
-- body: messge body
|
|
|
|
-- Returns
|
|
|
|
-- code: server status code, nil if error
|
|
|
|
-- answer: complete server reply
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.send(sock, message)
|
2001-03-27 21:25:11 +02:00
|
|
|
local code, answer
|
|
|
|
-- MAIL
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.send_mail(sock, message.from)
|
2001-03-27 21:25:11 +02:00
|
|
|
if not code then return nil, answer end
|
|
|
|
-- RCPT
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.send_rcpt(sock, message.rcpt)
|
2001-03-27 21:25:11 +02:00
|
|
|
if not code then return nil, answer end
|
|
|
|
-- DATA
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.send_data(sock, message.headers, message.body)
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2001-09-12 20:16:31 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Closes connection with server
|
|
|
|
-- Input
|
|
|
|
-- sock: socket connected to server
|
|
|
|
-- Returns
|
|
|
|
-- code: server status code, nil if error
|
|
|
|
-- answer: complete server reply
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.close(sock)
|
2001-03-27 21:25:11 +02:00
|
|
|
-- QUIT
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.send_quit(sock)
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Main mail function
|
|
|
|
-- Input
|
2001-09-12 20:16:31 +02:00
|
|
|
-- message: a table with the following fields:
|
|
|
|
-- from: message sender
|
|
|
|
-- rcpt: table containing message recipients
|
|
|
|
-- headers: table containing mime headers
|
|
|
|
-- body: message body
|
|
|
|
-- server: smtp server to be used
|
2001-03-27 21:25:11 +02:00
|
|
|
-- Returns
|
|
|
|
-- nil if successfull, error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:31 +02:00
|
|
|
function Public.mail(message)
|
2002-07-08 23:01:45 +02:00
|
|
|
local sock, err = Private.open(message.server)
|
2001-03-27 21:25:11 +02:00
|
|
|
if not sock then return err end
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, answer = Private.send(sock, message)
|
2001-03-27 21:25:11 +02:00
|
|
|
if not code then return answer end
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.close(sock)
|
2001-03-27 21:25:11 +02:00
|
|
|
if code then return nil end
|
|
|
|
return answer
|
|
|
|
end
|