aboutsummaryrefslogtreecommitdiff
path: root/src/smtp.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-11-22 08:33:29 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-11-22 08:33:29 +0000
commitd55a5826e81136a9ecf65c4cd407152a56684dc2 (patch)
tree109ad44c75cee890ad5e98583e12b15b5e65a18e /src/smtp.lua
parenta2b780bf7a78c66d54a248fa99b5fc862c12a127 (diff)
downloadluasocket-d55a5826e81136a9ecf65c4cd407152a56684dc2.tar.gz
luasocket-d55a5826e81136a9ecf65c4cd407152a56684dc2.tar.bz2
luasocket-d55a5826e81136a9ecf65c4cd407152a56684dc2.zip
Few tweaks in installation, some missing files, etc.
Diffstat (limited to 'src/smtp.lua')
-rw-r--r--src/smtp.lua42
1 files changed, 21 insertions, 21 deletions
diff --git a/src/smtp.lua b/src/smtp.lua
index 03b9e9b..72c4234 100644
--- a/src/smtp.lua
+++ b/src/smtp.lua
@@ -27,8 +27,8 @@ TIMEOUT = 60
27-- default server used to send e-mails 27-- default server used to send e-mails
28SERVER = "localhost" 28SERVER = "localhost"
29-- default port 29-- default port
30PORT = 25 30PORT = 25
31-- domain used in HELO command and default sendmail 31-- domain used in HELO command and default sendmail
32-- If we are under a CGI, try to get from environment 32-- If we are under a CGI, try to get from environment
33DOMAIN = os.getenv("SERVER_NAME") or "localhost" 33DOMAIN = os.getenv("SERVER_NAME") or "localhost"
34-- default time zone (means we don't know) 34-- default time zone (means we don't know)
@@ -43,12 +43,12 @@ function metat.__index:greet(domain)
43 self.try(self.tp:check("2..")) 43 self.try(self.tp:check("2.."))
44 self.try(self.tp:command("EHLO", domain or DOMAIN)) 44 self.try(self.tp:command("EHLO", domain or DOMAIN))
45 return socket.skip(1, self.try(self.tp:check("2.."))) 45 return socket.skip(1, self.try(self.tp:check("2..")))
46end 46end
47 47
48function metat.__index:mail(from) 48function metat.__index:mail(from)
49 self.try(self.tp:command("MAIL", "FROM:" .. from)) 49 self.try(self.tp:command("MAIL", "FROM:" .. from))
50 return self.try(self.tp:check("2..")) 50 return self.try(self.tp:check("2.."))
51end 51end
52 52
53function metat.__index:rcpt(to) 53function metat.__index:rcpt(to)
54 self.try(self.tp:command("RCPT", "TO:" .. to)) 54 self.try(self.tp:command("RCPT", "TO:" .. to))
@@ -99,7 +99,7 @@ function metat.__index:auth(user, password, ext)
99end 99end
100 100
101-- send message or throw an exception 101-- send message or throw an exception
102function metat.__index:send(mailt) 102function metat.__index:send(mailt)
103 self:mail(mailt.from) 103 self:mail(mailt.from)
104 if base.type(mailt.rcpt) == "table" then 104 if base.type(mailt.rcpt) == "table" then
105 for i,v in base.ipairs(mailt.rcpt) do 105 for i,v in base.ipairs(mailt.rcpt) do
@@ -112,14 +112,14 @@ function metat.__index:send(mailt)
112end 112end
113 113
114function open(server, port, create) 114function open(server, port, create)
115 local tp = socket.try(tp.connect(server or SERVER, port or PORT, 115 local tp = socket.try(tp.connect(server or SERVER, port or PORT,
116 create, TIMEOUT)) 116 create, TIMEOUT))
117 local s = base.setmetatable({tp = tp}, metat) 117 local s = base.setmetatable({tp = tp}, metat)
118 -- make sure tp is closed if we get an exception 118 -- make sure tp is closed if we get an exception
119 s.try = socket.newtry(function() 119 s.try = socket.newtry(function()
120 s:close() 120 s:close()
121 end) 121 end)
122 return s 122 return s
123end 123end
124 124
125--------------------------------------------------------------------------- 125---------------------------------------------------------------------------
@@ -142,7 +142,7 @@ local function send_headers(headers)
142 for i,v in base.pairs(headers) do 142 for i,v in base.pairs(headers) do
143 h = i .. ': ' .. v .. "\r\n" .. h 143 h = i .. ': ' .. v .. "\r\n" .. h
144 end 144 end
145 coroutine.yield(h) 145 coroutine.yield(h)
146end 146end
147 147
148-- yield multipart message body from a multipart message table 148-- yield multipart message body from a multipart message table
@@ -151,25 +151,25 @@ local function send_multipart(mesgt)
151 local bd = newboundary() 151 local bd = newboundary()
152 local headers = mesgt.headers or {} 152 local headers = mesgt.headers or {}
153 headers['content-type'] = headers['content-type'] or 'multipart/mixed' 153 headers['content-type'] = headers['content-type'] or 'multipart/mixed'
154 headers['content-type'] = headers['content-type'] .. 154 headers['content-type'] = headers['content-type'] ..
155 '; boundary="' .. bd .. '"' 155 '; boundary="' .. bd .. '"'
156 send_headers(headers) 156 send_headers(headers)
157 -- send preamble 157 -- send preamble
158 if mesgt.body.preamble then 158 if mesgt.body.preamble then
159 coroutine.yield(mesgt.body.preamble) 159 coroutine.yield(mesgt.body.preamble)
160 coroutine.yield("\r\n") 160 coroutine.yield("\r\n")
161 end 161 end
162 -- send each part separated by a boundary 162 -- send each part separated by a boundary
163 for i, m in base.ipairs(mesgt.body) do 163 for i, m in base.ipairs(mesgt.body) do
164 coroutine.yield("\r\n--" .. bd .. "\r\n") 164 coroutine.yield("\r\n--" .. bd .. "\r\n")
165 send_message(m) 165 send_message(m)
166 end 166 end
167 -- send last boundary 167 -- send last boundary
168 coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n") 168 coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n")
169 -- send epilogue 169 -- send epilogue
170 if mesgt.body.epilogue then 170 if mesgt.body.epilogue then
171 coroutine.yield(mesgt.body.epilogue) 171 coroutine.yield(mesgt.body.epilogue)
172 coroutine.yield("\r\n") 172 coroutine.yield("\r\n")
173 end 173 end
174end 174end
175 175
@@ -181,7 +181,7 @@ local function send_source(mesgt)
181 'text/plain; charset="iso-8859-1"' 181 'text/plain; charset="iso-8859-1"'
182 send_headers(headers) 182 send_headers(headers)
183 -- send body from source 183 -- send body from source
184 while true do 184 while true do
185 local chunk, err = mesgt.body() 185 local chunk, err = mesgt.body()
186 if err then coroutine.yield(nil, err) 186 if err then coroutine.yield(nil, err)
187 elseif chunk then coroutine.yield(chunk) 187 elseif chunk then coroutine.yield(chunk)
@@ -213,11 +213,11 @@ local function adjust_headers(mesgt)
213 for i,v in base.pairs(mesgt.headers or lower) do 213 for i,v in base.pairs(mesgt.headers or lower) do
214 lower[string.lower(i)] = v 214 lower[string.lower(i)] = v
215 end 215 end
216 lower["date"] = lower["date"] or 216 lower["date"] = lower["date"] or
217 os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE) 217 os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE)
218 lower["x-mailer"] = lower["x-mailer"] or socket._VERSION 218 lower["x-mailer"] = lower["x-mailer"] or socket._VERSION
219 -- this can't be overriden 219 -- this can't be overriden
220 lower["mime-version"] = "1.0" 220 lower["mime-version"] = "1.0"
221 mesgt.headers = lower 221 mesgt.headers = lower
222end 222end
223 223
@@ -225,7 +225,7 @@ function message(mesgt)
225 adjust_headers(mesgt) 225 adjust_headers(mesgt)
226 -- create and return message source 226 -- create and return message source
227 local co = coroutine.create(function() send_message(mesgt) end) 227 local co = coroutine.create(function() send_message(mesgt) end)
228 return function() 228 return function()
229 local ret, a, b = coroutine.resume(co) 229 local ret, a, b = coroutine.resume(co)
230 if ret then return a, b 230 if ret then return a, b
231 else return nil, a end 231 else return nil, a end