Seems to be working.
This commit is contained in:
parent
2c160627e5
commit
e896454e6c
4 changed files with 111 additions and 69 deletions
138
src/smtp.lua
138
src/smtp.lua
|
@ -10,23 +10,27 @@ socket.smtp = smtp
|
|||
setmetatable(smtp, { __index = _G })
|
||||
setfenv(1, smtp)
|
||||
|
||||
-- default server used to send e-mails
|
||||
SERVER = "localhost"
|
||||
-- default port
|
||||
PORT = 25
|
||||
-- domain used in HELO command and default sendmail
|
||||
-- If we are under a CGI, try to get from environment
|
||||
DOMAIN = os.getenv("SERVER_NAME") or "localhost"
|
||||
-- default server used to send e-mails
|
||||
SERVER = "localhost"
|
||||
-- default time zone (means we don't know)
|
||||
ZONE = "-0000"
|
||||
|
||||
function stuff()
|
||||
return ltn12.filter.cycle(dot, 2)
|
||||
end
|
||||
|
||||
local function skip(a, b, c)
|
||||
local function shift(a, b, c)
|
||||
return b, c
|
||||
end
|
||||
|
||||
-- send message or throw an exception
|
||||
function psend(control, mailt)
|
||||
socket.try(control:check("2.."))
|
||||
socket.try(control:command("EHLO", mailt.domain or DOMAIN))
|
||||
socket.try(control:check("2.."))
|
||||
socket.try(control:command("MAIL", "FROM:" .. mailt.from))
|
||||
|
@ -34,11 +38,12 @@ function psend(control, mailt)
|
|||
if type(mailt.rcpt) == "table" then
|
||||
for i,v in ipairs(mailt.rcpt) do
|
||||
socket.try(control:command("RCPT", "TO:" .. v))
|
||||
socket.try(control:check("2.."))
|
||||
end
|
||||
else
|
||||
socket.try(control:command("RCPT", "TO:" .. mailt.rcpt))
|
||||
socket.try(control:check("2.."))
|
||||
end
|
||||
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())))
|
||||
|
@ -48,6 +53,7 @@ function psend(control, mailt)
|
|||
socket.try(control:check("2.."))
|
||||
end
|
||||
|
||||
-- returns a hopefully unique mime boundary
|
||||
local seqno = 0
|
||||
local function newboundary()
|
||||
seqno = seqno + 1
|
||||
|
@ -55,62 +61,98 @@ local function newboundary()
|
|||
math.random(0, 99999), seqno)
|
||||
end
|
||||
|
||||
local function sendmessage(mesgt)
|
||||
-- send headers
|
||||
-- sendmessage forward declaration
|
||||
local sendmessage
|
||||
|
||||
-- yield multipart message body from a multipart message table
|
||||
local function sendmultipart(mesgt)
|
||||
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")
|
||||
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
|
||||
end
|
||||
|
||||
-- yield message body from a source
|
||||
local function sendsource(mesgt)
|
||||
-- set content-type if user didn't override
|
||||
if not mesgt.headers or not mesgt.headers["content-type"] then
|
||||
coroutine.yield('content-type: text/plain; charset="iso-88591"\r\n')
|
||||
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
|
||||
local function sendstring(mesgt)
|
||||
-- set content-type if user didn't override
|
||||
if not mesgt.headers or not mesgt.headers["content-type"] then
|
||||
coroutine.yield('content-type: text/plain; charset="iso-88591"\r\n')
|
||||
end
|
||||
-- finish headers
|
||||
coroutine.yield("\r\n")
|
||||
-- send body from string
|
||||
coroutine.yield(mesgt.body)
|
||||
|
||||
end
|
||||
|
||||
-- yield the headers one by one
|
||||
local function sendheaders(mesgt)
|
||||
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")
|
||||
while true do
|
||||
local chunk, err = mesgt.body()
|
||||
if err then return nil, err
|
||||
elseif chunk then coroutine.yield(chunk)
|
||||
else break end
|
||||
end
|
||||
-- deal with a simple string
|
||||
else
|
||||
-- finish headers
|
||||
coroutine.yield("\r\n")
|
||||
coroutine.yield(mesgt.body)
|
||||
end
|
||||
end
|
||||
|
||||
-- message source
|
||||
function sendmessage(mesgt)
|
||||
sendheaders(mesgt)
|
||||
if type(mesgt.body) == "table" then sendmultipart(mesgt)
|
||||
elseif type(mesgt.body) == "function" then sendsource(mesgt)
|
||||
else sendstring(mesgt) end
|
||||
end
|
||||
|
||||
-- set defaul headers
|
||||
local function adjustheaders(mesgt)
|
||||
mesgt.headers = mesgt.headers or {}
|
||||
mesgt.headers["mime-version"] = "1.0"
|
||||
mesgt.headers["date"] = mesgt.headers["date"] or
|
||||
os.date("%a, %d %b %Y %H:%M:%S") .. (mesgt.zone or ZONE)
|
||||
mesgt.headers["x-mailer"] = mesgt.headers["x-mailer"] or socket.version
|
||||
end
|
||||
|
||||
function message(mesgt)
|
||||
adjustheaders(mesgt)
|
||||
-- create and return message source
|
||||
local co = coroutine.create(function() sendmessage(mesgt) end)
|
||||
return function() return skip(coroutine.resume(co)) end
|
||||
return function() return shift(coroutine.resume(co)) end
|
||||
end
|
||||
|
||||
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
|
||||
local c, e = socket.tp.connect(mailt.server or SERVER, mailt.port or PORT)
|
||||
if not c then return nil, e end
|
||||
local s, e = pcall(psend, c, mailt)
|
||||
c:close()
|
||||
if s then return true
|
||||
else return nil, e end
|
||||
end
|
||||
|
||||
return smtp
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue