2004-05-28 08:16:43 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- SMTP client support for the Lua language.
|
|
|
|
-- LuaSocket toolkit.
|
|
|
|
-- Author: Diego Nehab
|
|
|
|
-- RCS ID: $Id$
|
|
|
|
-----------------------------------------------------------------------------
|
2004-05-28 09:47:41 +02:00
|
|
|
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-09-27 06:01:18 +02:00
|
|
|
-- Declare module and import dependencies
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2005-06-14 06:29:23 +02:00
|
|
|
local base = _G
|
2004-11-27 08:58:04 +01:00
|
|
|
local coroutine = require("coroutine")
|
|
|
|
local string = require("string")
|
|
|
|
local math = require("math")
|
|
|
|
local os = require("os")
|
2004-06-04 17:15:45 +02:00
|
|
|
local socket = require("socket")
|
2004-09-27 06:01:18 +02:00
|
|
|
local tp = require("socket.tp")
|
2004-06-04 17:15:45 +02:00
|
|
|
local ltn12 = require("ltn12")
|
2004-06-16 06:28:21 +02:00
|
|
|
local mime = require("mime")
|
2004-12-23 23:32:12 +01:00
|
|
|
module("socket.smtp")
|
2004-09-27 06:01:18 +02:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Program constants
|
|
|
|
-----------------------------------------------------------------------------
|
2004-06-15 08:24:00 +02:00
|
|
|
-- timeout for connection
|
|
|
|
TIMEOUT = 60
|
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
|
2005-11-22 09:33:29 +01:00
|
|
|
PORT = 25
|
|
|
|
-- domain used in HELO command and default sendmail
|
2001-09-12 20:16:31 +02:00
|
|
|
-- 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-05-25 07:27:44 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- Low level SMTP API
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local metat = { __index = {} }
|
|
|
|
|
|
|
|
function metat.__index:greet(domain)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:check("2.."))
|
|
|
|
self.try(self.tp:command("EHLO", domain or DOMAIN))
|
|
|
|
return socket.skip(1, self.try(self.tp:check("2..")))
|
2005-11-22 09:33:29 +01:00
|
|
|
end
|
2004-05-25 07:27:44 +02:00
|
|
|
|
|
|
|
function metat.__index:mail(from)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("MAIL", "FROM:" .. from))
|
|
|
|
return self.try(self.tp:check("2.."))
|
2005-11-22 09:33:29 +01:00
|
|
|
end
|
2004-05-25 07:27:44 +02:00
|
|
|
|
|
|
|
function metat.__index:rcpt(to)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("RCPT", "TO:" .. to))
|
|
|
|
return self.try(self.tp:check("2.."))
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
|
|
|
|
2004-05-26 06:58:32 +02:00
|
|
|
function metat.__index:data(src, step)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("DATA"))
|
|
|
|
self.try(self.tp:check("3.."))
|
|
|
|
self.try(self.tp:source(src, step))
|
|
|
|
self.try(self.tp:send("\r\n.\r\n"))
|
|
|
|
return self.try(self.tp:check("2.."))
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:quit()
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("QUIT"))
|
|
|
|
return self.try(self.tp:check("2.."))
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:close()
|
2004-06-18 23:41:44 +02:00
|
|
|
return self.tp:close()
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
|
|
|
|
2004-06-16 06:28:21 +02:00
|
|
|
function metat.__index:login(user, password)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("AUTH", "LOGIN"))
|
|
|
|
self.try(self.tp:check("3.."))
|
|
|
|
self.try(self.tp:command(mime.b64(user)))
|
|
|
|
self.try(self.tp:check("3.."))
|
|
|
|
self.try(self.tp:command(mime.b64(password)))
|
|
|
|
return self.try(self.tp:check("2.."))
|
2004-06-16 06:28:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:plain(user, password)
|
|
|
|
local auth = "PLAIN " .. mime.b64("\0" .. user .. "\0" .. password)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("AUTH", auth))
|
|
|
|
return self.try(self.tp:check("2.."))
|
2004-06-16 06:28:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:auth(user, password, ext)
|
|
|
|
if not user or not password then return 1 end
|
|
|
|
if string.find(ext, "AUTH[^\n]+LOGIN") then
|
|
|
|
return self:login(user, password)
|
|
|
|
elseif string.find(ext, "AUTH[^\n]+PLAIN") then
|
|
|
|
return self:plain(user, password)
|
|
|
|
else
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(nil, "authentication not supported")
|
2004-06-16 06:28:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-19 06:04:03 +01:00
|
|
|
-- send message or throw an exception
|
2005-11-22 09:33:29 +01:00
|
|
|
function metat.__index:send(mailt)
|
2004-05-25 07:27:44 +02:00
|
|
|
self:mail(mailt.from)
|
2004-11-27 08:58:04 +01:00
|
|
|
if base.type(mailt.rcpt) == "table" then
|
|
|
|
for i,v in base.ipairs(mailt.rcpt) do
|
2004-05-25 07:27:44 +02:00
|
|
|
self:rcpt(v)
|
2004-03-18 08:01:14 +01:00
|
|
|
end
|
2003-10-21 03:12:23 +02:00
|
|
|
else
|
2004-05-25 07:27:44 +02:00
|
|
|
self:rcpt(mailt.rcpt)
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
2004-06-17 08:23:13 +02:00
|
|
|
self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step)
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2005-08-12 07:56:32 +02:00
|
|
|
function open(server, port, create)
|
2005-11-22 09:33:29 +01:00
|
|
|
local tp = socket.try(tp.connect(server or SERVER, port or PORT,
|
2006-03-14 10:04:15 +01:00
|
|
|
TIMEOUT, create))
|
2004-11-27 08:58:04 +01:00
|
|
|
local s = base.setmetatable({tp = tp}, metat)
|
2004-06-18 10:02:09 +02:00
|
|
|
-- make sure tp is closed if we get an exception
|
2005-11-22 09:33:29 +01:00
|
|
|
s.try = socket.newtry(function()
|
2004-06-22 06:49:57 +02:00
|
|
|
s:close()
|
|
|
|
end)
|
2005-11-22 09:33:29 +01:00
|
|
|
return s
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
|
|
|
|
2007-03-12 05:08:40 +01:00
|
|
|
-- convert headers to lowercase
|
|
|
|
local function lower_headers(headers)
|
|
|
|
local lower = {}
|
|
|
|
for i,v in base.pairs(headers or lower) do
|
|
|
|
lower[string.lower(i)] = v
|
|
|
|
end
|
|
|
|
return lower
|
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- Multipart message source
|
|
|
|
-----------------------------------------------------------------------------
|
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
|
|
|
|
2005-08-23 07:53:14 +02:00
|
|
|
-- yield the headers all at once, it's faster
|
|
|
|
local function send_headers(headers)
|
|
|
|
local h = "\r\n"
|
|
|
|
for i,v in base.pairs(headers) do
|
|
|
|
h = i .. ': ' .. v .. "\r\n" .. h
|
|
|
|
end
|
2005-11-22 09:33:29 +01:00
|
|
|
coroutine.yield(h)
|
2005-08-23 07:53:14 +02:00
|
|
|
end
|
|
|
|
|
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)
|
2005-08-23 07:53:14 +02:00
|
|
|
-- make sure we have our boundary and send headers
|
2004-03-19 06:04:03 +01:00
|
|
|
local bd = newboundary()
|
2007-03-12 05:08:40 +01:00
|
|
|
local headers = lower_headers(mesgt.headers or {})
|
2005-08-23 07:53:14 +02:00
|
|
|
headers['content-type'] = headers['content-type'] or 'multipart/mixed'
|
2005-11-22 09:33:29 +01:00
|
|
|
headers['content-type'] = headers['content-type'] ..
|
2005-08-23 07:53:14 +02:00
|
|
|
'; boundary="' .. bd .. '"'
|
|
|
|
send_headers(headers)
|
2004-03-19 06:04:03 +01:00
|
|
|
-- send preamble
|
2005-11-22 09:33:29 +01:00
|
|
|
if mesgt.body.preamble then
|
|
|
|
coroutine.yield(mesgt.body.preamble)
|
|
|
|
coroutine.yield("\r\n")
|
2004-06-15 08:24:00 +02:00
|
|
|
end
|
2004-03-19 06:04:03 +01:00
|
|
|
-- send each part separated by a boundary
|
2004-11-27 08:58:04 +01:00
|
|
|
for i, m in base.ipairs(mesgt.body) do
|
2004-03-19 06:04:03 +01:00
|
|
|
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
|
2005-11-22 09:33:29 +01:00
|
|
|
-- send last boundary
|
2004-03-19 06:04:03 +01:00
|
|
|
coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n")
|
|
|
|
-- send epilogue
|
2005-11-22 09:33:29 +01:00
|
|
|
if mesgt.body.epilogue then
|
|
|
|
coroutine.yield(mesgt.body.epilogue)
|
|
|
|
coroutine.yield("\r\n")
|
2004-06-15 08:24:00 +02:00
|
|
|
end
|
2004-03-19 06:04:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- yield message body from a source
|
2004-03-22 05:15:03 +01:00
|
|
|
local function send_source(mesgt)
|
2005-08-23 07:53:14 +02:00
|
|
|
-- make sure we have a content-type
|
2007-03-12 05:08:40 +01:00
|
|
|
local headers = lower_headers(mesgt.headers or {})
|
2005-08-23 07:53:14 +02:00
|
|
|
headers['content-type'] = headers['content-type'] or
|
|
|
|
'text/plain; charset="iso-8859-1"'
|
|
|
|
send_headers(headers)
|
2004-03-19 06:04:03 +01:00
|
|
|
-- send body from source
|
2005-11-22 09:33:29 +01:00
|
|
|
while true do
|
2004-03-19 06:04:03 +01:00
|
|
|
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)
|
2005-08-23 07:53:14 +02:00
|
|
|
-- make sure we have a content-type
|
2007-03-12 05:08:40 +01:00
|
|
|
local headers = lower_headers(mesgt.headers or {})
|
2005-08-23 07:53:14 +02:00
|
|
|
headers['content-type'] = headers['content-type'] or
|
|
|
|
'text/plain; charset="iso-8859-1"'
|
|
|
|
send_headers(headers)
|
2004-03-19 06:04:03 +01:00
|
|
|
-- send body from string
|
|
|
|
coroutine.yield(mesgt.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- message source
|
2004-03-22 05:15:03 +01:00
|
|
|
function send_message(mesgt)
|
2004-11-27 08:58:04 +01:00
|
|
|
if base.type(mesgt.body) == "table" then send_multipart(mesgt)
|
|
|
|
elseif base.type(mesgt.body) == "function" then send_source(mesgt)
|
2004-03-22 05:15:03 +01:00
|
|
|
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)
|
2007-03-12 05:08:40 +01:00
|
|
|
local lower = lower_headers(mesgt.headers)
|
2005-11-22 09:33:29 +01:00
|
|
|
lower["date"] = lower["date"] or
|
2004-03-19 07:14:56 +01:00
|
|
|
os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE)
|
2005-06-14 06:29:23 +02:00
|
|
|
lower["x-mailer"] = lower["x-mailer"] or socket._VERSION
|
2004-06-04 17:15:45 +02:00
|
|
|
-- this can't be overriden
|
2005-11-22 09:33:29 +01:00
|
|
|
lower["mime-version"] = "1.0"
|
2007-03-12 05:08:40 +01:00
|
|
|
return lower
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2004-03-18 08:01:14 +01:00
|
|
|
function message(mesgt)
|
2007-03-12 05:08:40 +01:00
|
|
|
mesgt.headers = 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)
|
2005-11-22 09:33:29 +01:00
|
|
|
return function()
|
2004-06-15 08:24:00 +02:00
|
|
|
local ret, a, b = coroutine.resume(co)
|
|
|
|
if ret then return a, b
|
|
|
|
else return nil, a end
|
|
|
|
end
|
2001-03-27 21:25:11 +02:00
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- High level SMTP API
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
send = socket.protect(function(mailt)
|
2005-08-12 07:56:32 +02:00
|
|
|
local s = open(mailt.server, mailt.port, mailt.create)
|
2004-06-18 10:02:09 +02:00
|
|
|
local ext = s:greet(mailt.domain)
|
|
|
|
s:auth(mailt.user, mailt.password, ext)
|
|
|
|
s:send(mailt)
|
|
|
|
s:quit()
|
|
|
|
return s:close()
|
2004-05-25 07:27:44 +02:00
|
|
|
end)
|