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)
|
|
|
|
|
|
|
|
-- 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"
|
2001-09-12 20:16:31 +02:00
|
|
|
-- default server used to send e-mails
|
2003-10-21 03:12:23 +02:00
|
|
|
SERVER = "localhost"
|
|
|
|
|
2004-02-04 15:29:11 +01:00
|
|
|
function stuff()
|
2004-03-16 07:42:53 +01:00
|
|
|
return ltn12.filter.cycle(dot, 2)
|
2004-02-04 15:29:11 +01:00
|
|
|
end
|
|
|
|
|
2004-03-18 08:01:14 +01:00
|
|
|
local function skip(a, b, c)
|
|
|
|
return b, c
|
2001-09-12 20:16:31 +02:00
|
|
|
end
|
|
|
|
|
2004-03-18 08:01:14 +01:00
|
|
|
function psend(control, mailt)
|
|
|
|
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))
|
|
|
|
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))
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
2004-03-18 08:01:14 +01:00
|
|
|
socket.try(control:check("2.."))
|
|
|
|
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-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-18 08:01:14 +01:00
|
|
|
local function sendmessage(mesgt)
|
|
|
|
-- send headers
|
|
|
|
if mesgt.headers then
|
|
|
|
for i,v in pairs(mesgt.headers) do
|
|
|
|
coroutine.yield(i .. ':' .. v .. "\r\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- deal with multipart
|
|
|
|
if type(mesgt.body) == "table" then
|
|
|
|
local bd = newboundary()
|
|
|
|
-- define boundary and finish headers
|
|
|
|
coroutine.yield('mime-version: 1.0\r\n')
|
|
|
|
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")
|
|
|
|
sendmessage(m)
|
|
|
|
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
|
|
|
|
-- deal with a source
|
|
|
|
elseif type(mesgt.body) == "function" then
|
|
|
|
-- finish headers
|
|
|
|
coroutine.yield("\r\n")
|
2003-10-21 03:12:23 +02:00
|
|
|
while true do
|
2004-03-18 08:01:14 +01:00
|
|
|
local chunk, err = mesgt.body()
|
|
|
|
if err then return nil, err
|
|
|
|
elseif chunk then coroutine.yield(chunk)
|
|
|
|
else break end
|
2003-10-21 03:12:23 +02:00
|
|
|
end
|
2004-03-18 08:01:14 +01:00
|
|
|
-- deal with a simple string
|
|
|
|
else
|
|
|
|
-- finish headers
|
|
|
|
coroutine.yield("\r\n")
|
|
|
|
coroutine.yield(mesgt.body)
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-18 08:01:14 +01:00
|
|
|
function message(mesgt)
|
|
|
|
local co = coroutine.create(function() sendmessage(mesgt) end)
|
|
|
|
return function() return skip(coroutine.resume(co)) end
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2004-03-18 08:01:14 +01:00
|
|
|
function send(mailt)
|
|
|
|
local control, err = socket.tp.connect(mailt.server or SERVER,
|
|
|
|
mailt.port or PORT)
|
|
|
|
if not control then return nil, err end
|
|
|
|
local status, err = pcall(psend, control, mailt)
|
|
|
|
control:close()
|
|
|
|
if status then return true
|
|
|
|
else return nil, err end
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2003-10-21 03:12:23 +02:00
|
|
|
return smtp
|