2003-10-21 03:12:23 +02:00
|
|
|
-- make sure LuaSocket is loaded
|
|
|
|
if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end
|
|
|
|
-- get LuaSocket namespace
|
|
|
|
local socket = _G[LUASOCKET_LIBNAME]
|
|
|
|
if not socket then error('module requires LuaSocket') end
|
|
|
|
-- create smtp namespace inside LuaSocket namespace
|
2004-02-04 15:29:11 +01:00
|
|
|
local smtp = socket.smtp or {}
|
2003-10-21 03:12:23 +02:00
|
|
|
socket.smtp = smtp
|
|
|
|
-- make all module globals fall into smtp namespace
|
|
|
|
setmetatable(smtp, { __index = _G })
|
|
|
|
setfenv(1, smtp)
|
|
|
|
|
2004-03-19 06:04:03 +01:00
|
|
|
-- default server used to send e-mails
|
|
|
|
SERVER = "localhost"
|
2003-10-21 03:12:23 +02:00
|
|
|
-- default port
|
|
|
|
PORT = 25
|
2001-09-12 20:16:31 +02:00
|
|
|
-- domain used in HELO command and default sendmail
|
|
|
|
-- If we are under a CGI, try to get from environment
|
2003-10-21 03:12:23 +02:00
|
|
|
DOMAIN = os.getenv("SERVER_NAME") or "localhost"
|
2004-03-19 06:04:03 +01:00
|
|
|
-- default time zone (means we don't know)
|
|
|
|
ZONE = "-0000"
|
2003-10-21 03:12:23 +02:00
|
|
|
|
2004-03-19 06:04:03 +01:00
|
|
|
local function shift(a, b, c)
|
2004-03-18 08:01:14 +01:00
|
|
|
return b, c
|
2001-09-12 20:16:31 +02:00
|
|
|
end
|
|
|
|
|
2004-03-22 05:15:03 +01:00
|
|
|
-- high level stuffing filter
|
|
|
|
function stuff()
|
|
|
|
return ltn12.filter.cycle(dot, 2)
|
|
|
|
end
|
|
|
|
|
2004-03-19 06:04:03 +01:00
|
|
|
-- send message or throw an exception
|
2004-03-22 05:15:03 +01:00
|
|
|
local function send_p(control, mailt)
|
2004-03-19 06:04:03 +01:00
|
|
|
socket.try(control:check("2.."))
|
2004-03-18 08:01:14 +01:00
|
|
|
socket.try(control:command("EHLO", mailt.domain or DOMAIN))
|
|
|
|
socket.try(control:check("2.."))
|
|
|
|
socket.try(control:command("MAIL", "FROM:" .. mailt.from))
|
|
|
|
socket.try(control:check("2.."))
|
|
|
|
if type(mailt.rcpt) == "table" then
|
|
|
|
for i,v in ipairs(mailt.rcpt) do
|
|
|
|
socket.try(control:command("RCPT", "TO:" .. v))
|
2004-03-19 06:04:03 +01:00
|
|
|
socket.try(control:check("2.."))
|
2004-03-18 08:01:14 +01:00
|
|
|
end
|
2003-10-21 03:12:23 +02:00
|
|
|
else
|
2004-03-18 08:01:14 +01:00
|
|
|
socket.try(control:command("RCPT", "TO:" .. mailt.rcpt))
|
2004-03-19 06:04:03 +01:00
|
|
|
socket.try(control:check("2.."))
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
2004-03-18 08:01:14 +01:00
|
|
|
socket.try(control:command("DATA"))
|
|
|
|
socket.try(control:check("3.."))
|
|
|
|
socket.try(control:source(ltn12.source.chain(mailt.source, stuff())))
|
|
|
|
socket.try(control:send("\r\n.\r\n"))
|
|
|
|
socket.try(control:check("2.."))
|
|
|
|
socket.try(control:command("QUIT"))
|
|
|
|
socket.try(control:check("2.."))
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2004-03-19 06:04:03 +01:00
|
|
|
-- returns a hopefully unique mime boundary
|
2004-03-18 08:01:14 +01:00
|
|
|
local seqno = 0
|
|
|
|
local function newboundary()
|
|
|
|
seqno = seqno + 1
|
|
|
|
return string.format('%s%05d==%05u', os.date('%d%m%Y%H%M%S'),
|
|
|
|
math.random(0, 99999), seqno)
|
2001-09-12 20:16:31 +02:00
|
|
|
end
|
|
|
|
|
2004-03-22 05:15:03 +01:00
|
|
|
-- send_message forward declaration
|
|
|
|
local send_message
|
2004-03-19 06:04:03 +01:00
|
|
|
|
|
|
|
-- yield multipart message body from a multipart message table
|
2004-03-22 05:15:03 +01:00
|
|
|
local function send_multipart(mesgt)
|
2004-03-19 06:04:03 +01:00
|
|
|
local bd = newboundary()
|
|
|
|
-- define boundary and finish headers
|
|
|
|
coroutine.yield('content-type: multipart/mixed; boundary="' ..
|
|
|
|
bd .. '"\r\n\r\n')
|
|
|
|
-- send preamble
|
|
|
|
if mesgt.body.preamble then coroutine.yield(mesgt.body.preamble) end
|
|
|
|
-- send each part separated by a boundary
|
|
|
|
for i, m in ipairs(mesgt.body) do
|
|
|
|
coroutine.yield("\r\n--" .. bd .. "\r\n")
|
2004-03-22 05:15:03 +01:00
|
|
|
send_message(m)
|
2004-03-19 06:04:03 +01:00
|
|
|
end
|
|
|
|
-- send last boundary
|
|
|
|
coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n")
|
|
|
|
-- send epilogue
|
|
|
|
if mesgt.body.epilogue then coroutine.yield(mesgt.body.epilogue) end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- yield message body from a source
|
2004-03-22 05:15:03 +01:00
|
|
|
local function send_source(mesgt)
|
2004-03-19 06:04:03 +01:00
|
|
|
-- set content-type if user didn't override
|
|
|
|
if not mesgt.headers or not mesgt.headers["content-type"] then
|
2004-03-19 07:14:56 +01:00
|
|
|
coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n')
|
2004-03-19 06:04:03 +01:00
|
|
|
end
|
|
|
|
-- finish headers
|
|
|
|
coroutine.yield("\r\n")
|
|
|
|
-- send body from source
|
|
|
|
while true do
|
|
|
|
local chunk, err = mesgt.body()
|
|
|
|
if err then coroutine.yield(nil, err)
|
|
|
|
elseif chunk then coroutine.yield(chunk)
|
|
|
|
else break end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- yield message body from a string
|
2004-03-22 05:15:03 +01:00
|
|
|
local function send_string(mesgt)
|
2004-03-19 06:04:03 +01:00
|
|
|
-- set content-type if user didn't override
|
|
|
|
if not mesgt.headers or not mesgt.headers["content-type"] then
|
2004-03-19 07:14:56 +01:00
|
|
|
coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n')
|
2004-03-19 06:04:03 +01:00
|
|
|
end
|
|
|
|
-- finish headers
|
|
|
|
coroutine.yield("\r\n")
|
|
|
|
-- send body from string
|
|
|
|
coroutine.yield(mesgt.body)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
-- yield the headers one by one
|
2004-03-22 05:15:03 +01:00
|
|
|
local function send_headers(mesgt)
|
2004-03-18 08:01:14 +01:00
|
|
|
if mesgt.headers then
|
|
|
|
for i,v in pairs(mesgt.headers) do
|
|
|
|
coroutine.yield(i .. ':' .. v .. "\r\n")
|
|
|
|
end
|
|
|
|
end
|
2004-03-19 06:04:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- message source
|
2004-03-22 05:15:03 +01:00
|
|
|
function send_message(mesgt)
|
|
|
|
send_headers(mesgt)
|
|
|
|
if type(mesgt.body) == "table" then send_multipart(mesgt)
|
|
|
|
elseif type(mesgt.body) == "function" then send_source(mesgt)
|
|
|
|
else send_string(mesgt) end
|
2004-03-19 06:04:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- set defaul headers
|
2004-03-22 05:15:03 +01:00
|
|
|
local function adjust_headers(mesgt)
|
2004-03-19 06:04:03 +01:00
|
|
|
mesgt.headers = mesgt.headers or {}
|
|
|
|
mesgt.headers["mime-version"] = "1.0"
|
|
|
|
mesgt.headers["date"] = mesgt.headers["date"] or
|
2004-03-19 07:14:56 +01:00
|
|
|
os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE)
|
2004-03-19 06:04:03 +01:00
|
|
|
mesgt.headers["x-mailer"] = mesgt.headers["x-mailer"] or socket.version
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2004-03-18 08:01:14 +01:00
|
|
|
function message(mesgt)
|
2004-03-22 05:15:03 +01:00
|
|
|
adjust_headers(mesgt)
|
2004-03-19 06:04:03 +01:00
|
|
|
-- create and return message source
|
2004-03-22 05:15:03 +01:00
|
|
|
local co = coroutine.create(function() send_message(mesgt) end)
|
2004-03-19 06:04:03 +01:00
|
|
|
return function() return shift(coroutine.resume(co)) end
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2004-03-18 08:01:14 +01:00
|
|
|
function send(mailt)
|
2004-03-19 06:04:03 +01:00
|
|
|
local c, e = socket.tp.connect(mailt.server or SERVER, mailt.port or PORT)
|
|
|
|
if not c then return nil, e end
|
2004-03-22 05:15:03 +01:00
|
|
|
local s, e = pcall(send_p, c, mailt)
|
2004-03-19 06:04:03 +01:00
|
|
|
c:close()
|
|
|
|
if s then return true
|
|
|
|
else return nil, e end
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2003-10-21 03:12:23 +02:00
|
|
|
return smtp
|