aboutsummaryrefslogtreecommitdiff
path: root/doc/smtp.html
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 06:24:00 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 06:24:00 +0000
commit58096449c6044b7aade5cd41cfd71c6bec1d273d (patch)
tree1814ffebe89c4c2556d84f97f66db37a7e8b4554 /doc/smtp.html
parent9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (diff)
downloadluasocket-58096449c6044b7aade5cd41cfd71c6bec1d273d.tar.gz
luasocket-58096449c6044b7aade5cd41cfd71c6bec1d273d.tar.bz2
luasocket-58096449c6044b7aade5cd41cfd71c6bec1d273d.zip
Manual is almost done. HTTP is missing.
Implemented new distribution scheme. Select is now purely C. HTTP reimplemented seems faster dunno why. LTN12 functions that coroutines fail gracefully.
Diffstat (limited to 'doc/smtp.html')
-rw-r--r--doc/smtp.html235
1 files changed, 194 insertions, 41 deletions
diff --git a/doc/smtp.html b/doc/smtp.html
index 0862de0..b0ae634 100644
--- a/doc/smtp.html
+++ b/doc/smtp.html
@@ -35,15 +35,22 @@
35 35
36<h2 id=smtp>SMTP</h2> 36<h2 id=smtp>SMTP</h2>
37 37
38<p> 38<p> The <tt>smtp.lua</tt> module provides functionality to send e-mail
39The <tt>smtp.lua</tt> module provides functionality to send e-mail
40messages. The implementation conforms to the Simple Mail Transfer Protocol, 39messages. The implementation conforms to the Simple Mail Transfer Protocol,
41<a href="http://www.cs.princeton.edu/~diego/rfc/rfc2821.txt">RFC 2821</a>. 40<a href="http://www.cs.princeton.edu/~diego/rfc/rfc2821.txt">RFC 2821</a>.
42The other RFC of interest in this implementation is 41Another RFC of interest is <a
43<a href="http://www.cs.princeton.edu/~diego/rfc/rfc2822.txt">RFC 2822</a>, 42href="http://www.cs.princeton.edu/~diego/rfc/rfc2822.txt">RFC 2822</a>,
44which governs the Internet Message Format. 43which governs the Internet Message Format.
44Multipart messages (those that contain attatchments) are part
45of the MIME standard, but described mainly
46in <a href="http://www.cs.princeton.edu/~diego/rfc/rfc2046.txt">RFC
472046</a>
45 48
46</p> 49<p> In the description below, good understanding of <a
50href="http://lua-users.org/wiki/FiltersSourcesAndSinks"> LTN012, Filters
51sources and sinks</a> and the <a href=mime.html>MIME</a> module is
52assumed. In fact, the SMTP module was the main reason for their
53creation. </p>
47 54
48<p> 55<p>
49MIME headers are represented as a Lua table in the form: 56MIME headers are represented as a Lua table in the form:
@@ -78,29 +85,56 @@ Note: MIME headers are independent of order. Therefore, there is no problem
78in representing them in a Lua table. 85in representing them in a Lua table.
79</p> 86</p>
80 87
81<!-- mail +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 88<p>
89The following constants can be set to control the default behaviour of
90the SMTP module:
91</p>
82 92
83<p class=name id=mail> 93<ul>
84socket.smtp.<b>mail{</b><br> 94<li> <tt>DOMAIN</tt>: domain used to greet the server;
95<li> <tt>PORT</tt>: default port used for the connection;
96<li> <tt>SERVER</tt>: default server used for the connection;
97<li> <tt>TIMEOUT</tt>: default timeout for all I/O operations;
98<li> <tt>ZONE</tt>: default time zone.
99</ul>
100
101<!-- send +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
102
103<p class=name id=send>
104smtp.<b>send{</b><br>
85&nbsp;&nbsp;from = <i>string</i>,<br> 105&nbsp;&nbsp;from = <i>string</i>,<br>
86&nbsp;&nbsp;rcpt = <i>string</i> or <i>string-table</i>,<br> 106&nbsp;&nbsp;rcpt = <i>string</i> or <i>string-table</i>,<br>
87&nbsp;&nbsp;body = <i>string</i>,<br> 107&nbsp;&nbsp;source = <i>LTN12 source</i>,<br>
88&nbsp;&nbsp;headers = <i>headers-table</i>,<br> 108&nbsp;&nbsp;[server = <i>string</i>],<br>
89&nbsp;&nbsp;server = <i>string</i><br> 109&nbsp;&nbsp;[port = <i>string</i>]<br>
110&nbsp;&nbsp;[domain = <i>string</i>],<br>
111&nbsp;&nbsp;[step = <i>LTN12 pump step</i>],<br>
90<b>}</b> 112<b>}</b>
91</p> 113</p>
92 114
93<p class=description> 115<p class=description>
94Sends a message to a recipient list. 116Sends a message to a recipient list. Since sending messages is not as
117simple as downloading an URL from a FTP or HTTP server, this function
118doesn't have a simple interface. However, see the
119<a href=#message><tt>message</tt></a> source factory for
120a very powerful way to define the message contents.
95</p> 121</p>
96 122
97<p class=parameters> 123<p class=parameters>
98<tt>Rcpt</tt> is a Lua table with one entry for each recipient, or a string 124The sender is given by the e-mail address in the <tt>from</tt> field.
125<tt>Rcpt</tt> is a Lua table with one entry for each recipient e-mail
126address, or a string
99in case there is just one recipient. 127in case there is just one recipient.
100The sender is given by the e-mail address <tt>from</tt>. 128The contents of the message are given by a LTN12 <tt>source</tt>. Several
101The message is composed by the optional MIME Headers <tt>headers</tt> 129arguments are optional:
102and text <tt>body</tt>. The message is sent using the server 130<ul>
103<tt>server</tt>. 131<li> <tt>server</tt>: Server to connect to. Defaults to "localhost";
132<li> <tt>port</tt>: Port to connect to. Defaults to 25;
133<li> <tt>domain</tt>: Domain name used to greet the server; Defaults to the
134local machine host name;
135<li> <tt>step</tt>: LTN12 pump step function used to pass data from the
136source to the server. Defaults to the LTN12 <tt>pump.step</tt> function.
137</ul>
104</p> 138</p>
105 139
106<p class=return> 140<p class=return>
@@ -109,6 +143,13 @@ If successful, the function returns 1. Otherwise, the function returns
109</p> 143</p>
110 144
111<p class=note> 145<p class=note>
146Note: SMTP servers are can be very picky with the format of e-mail
147addresses. To be safe, use only addresses of the form
148"<tt>&lt;fulano@tecgraf.puc-rio.br&gt;</tt>" in the <tt>from</tt> and
149<tt>rcpt</tt> arguments to the <tt>send</tt> function. In headers, e-mail
150addresses can take whatever form you like. </p>
151
152<p class=note>
112Big note: There is a good deal of misconception with the use of the 153Big note: There is a good deal of misconception with the use of the
113destination address field headers, i.e., the '<tt>To</tt>', '<tt>Cc</tt>', 154destination address field headers, i.e., the '<tt>To</tt>', '<tt>Cc</tt>',
114and, more importantly, the '<tt>Bcc</tt>' headers. Do <em>not</em> add a 155and, more importantly, the '<tt>Bcc</tt>' headers. Do <em>not</em> add a
@@ -117,11 +158,12 @@ exact opposite of what you expect.
117</p> 158</p>
118 159
119<p class=note> 160<p class=note>
120Only recipients specified in the recipient list will receive a copy of the 161Only recipients specified in the <tt>rcpt</tt> list will receive a copy of the
121message. Each recipient of an SMTP mail message receives a copy of the 162message. Each recipient of an SMTP mail message receives a copy of the
122message body along with the headers, and nothing more. The headers are 163message body along with the headers, and nothing more. The headers
123considered as part of the message. The list of recipients is <em>not</em> 164<em>are</em> part of the message and should be produced by the LTN12
124part of the message. 165<tt>source</tt> function. The <tt>rcpt</tt> list is <em>not</em>
166part of the message and will not be sent to anyone.
125</p> 167</p>
126 168
127<p class=note> 169<p class=note>
@@ -143,9 +185,9 @@ Copy") contains addresses of recipients of the message whose addresses are not t
143</ul> 185</ul>
144 186
145<p class=note> 187<p class=note>
146The LuaSocket <tt>mail</tt> function does not interpret the headers you 188The LuaSocket <tt>send</tt> function does not care or interpret the
147pass to, but it gives you full control over what is sent and to whom 189headers you send, but it gives you full control over what is sent and
148it is sent: 190to whom it is sent:
149</p> 191</p>
150<ul> 192<ul>
151<li> If someone is to receive the message, the e-mail address <em>has</em> 193<li> If someone is to receive the message, the e-mail address <em>has</em>
@@ -171,36 +213,147 @@ and
171</p> 213</p>
172 214
173<pre class=example> 215<pre class=example>
216-- load the smtp support
217local smtp = require("smtp")
218
174-- Connects to server "localhost" and sends a message to users 219-- Connects to server "localhost" and sends a message to users
175-- "fulano@tecgraf.puc-rio.br", "beltrano@tecgraf.puc-rio.br", 220-- "fulano@tecgraf.puc-rio.br", "beltrano@tecgraf.puc-rio.br",
176-- and "sicrano@tecgraf.puc-rio.br". 221-- and "sicrano@tecgraf.puc-rio.br".
177-- Note that "fulano" is the primary recipient, "beltrano" receives a 222-- Note that "fulano" is the primary recipient, "beltrano" receives a
178-- carbon copy and neither of them knows that "sicrano" received a blind 223-- carbon copy and neither of them knows that "sicrano" received a blind
179-- carbon copy of the message. 224-- carbon copy of the message.
180headers = { 225from = "&lt;luasocket@tecgraf.puc-rio.br&gt;"
181 to = "fulano@tecgraf.puc-rio.br",
182 cc = "beltrano@tecgraf.puc-rio.br",
183 subject = "LuaSocket test message"
184}
185
186from = "luasocket@tecgraf.puc-rio.br"
187 226
188rcpt = { 227rcpt = {
189 "fulano@tecgraf.puc-rio.br", 228 "&lt;fulano@tecgraf.puc-rio.br&gt;",
190 "beltrano@tecgraf.puc-rio.br", 229 "&lt;beltrano@tecgraf.puc-rio.br&gt;",
191 "sicrano@tecgraf.puc-rio.br" 230 "&lt;sicrano@tecgraf.puc-rio.br&gt;"
192} 231}
193 232
194body = "This is a test message. Please ignore." 233mesgt = {
195 234 headers = {
196server = "localhost" 235 to = "Fulano da Silva &lt;fulano@tecgraf.puc-rio.br&gt;",
236 cc = '"Beltrano F. Nunes" &lt;beltrano@tecgraf.puc-rio.br&gt;',
237 subject = "My first message"
238 }
239 body = "I hope this works. If it does, I can send you another 1000 copies."
240}
197 241
198r, e = socket.smtp.mail{ 242r, e = smtp.send{
199 from = from, 243 from = from,
200 rcpt = rcpt, 244 rcpt = rcpt,
201 headers = headers, 245 source = smtp.message(mesgt)
202 body = body, 246}
203 server = server 247</pre>
248
249<!-- message ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
250
251<p class=name id=message>
252smtp.<b>message(</b>mesgt<b>)</b>
253</p>
254
255<p class=description>
256Returns a LTN12 source that sends an SMTP message body, possibly multipart
257(arbitrarily deep).
258</p>
259
260<p class=parameters>
261The only parameter of the function is a table describing the message.
262<tt>Mesgt</tt> has the following form (notice the recursive structure):
263</p>
264
265<blockquote>
266<table summary="Mesgt table structure">
267<tr><td><tt>
268mesgt = {<br>
269&nbsp;&nbsp;headers = <i>header-table</i>,<br>
270&nbsp;&nbsp;body = <i>LTN12 source</i> or <i>string</i> or
271<i>multipart-mesgt</i><br>
272}<br>
273&nbsp;<br>
274multipart-mesgt = {<br>
275&nbsp;&nbsp;preamble = <i>string</i><br>
276&nbsp;&nbsp;[1] = <i>mesgt</i>,<br>
277&nbsp;&nbsp;[2] = <i>mesgt</i>,<br>
278&nbsp;&nbsp;...<br>
279&nbsp;&nbsp;[<i>n</i>] = <i>mesgt</i>,<br>
280&nbsp;&nbsp;epilogue = <i>string</i>,<br>
281}<br>
282</tt></td></tr>
283</table>
284</blockquote>
285
286<p class=parameters>
287For a simple message, all that is needed is a set of <tt>headers</tt>
288and the <tt>body</tt>. The message <tt>body</tt> can be given as a string
289or as a LTN12 source. For multipart messages, the body is a table that
290recursively defines each part as an independent message, plus a preamble
291and an epilogue.
292</p>
293
294<p class=return>
295The function returns an LTN12 source that produces the message contents as
296defined by <tt>mesgt</tt>. Hopefuly, the following example will make
297things clear. When in doubt, refer to the appropriate RFC as listed in the
298introduction. </p>
299
300<pre class=example>
301-- load the smtp support and its friends
302local smtp = require("smtp")
303local mime = require("mime")
304local ltn12 = require("ltn12")
305
306-- creates a source to send a message with two parts. The first part is
307-- plain text, the second part is a PNG image, encoded as base64.
308source = smtp.message{
309 headers = {
310 -- Remember that headers are *ignored* by smtp.send.
311 from = "Sicrano de Oliveira &lt;sicrano@tecgraf.puc-rio.br&gt;",
312 to = "Fulano da Silva &lt;fulano@tecgraf.puc-rio.br&gt;",
313 subject = "Here is a message with attachments"
314 },
315 body = {
316 preamble = "If your client doesn't understand attachments, \r\n" ..
317 "it will still display the preamble and the epilogue.\r\n",
318 "Preamble might show up even in a MIME enabled client.",
319 -- first part: no headers means plain text, us-ascii.
320 -- The mime.eol low-level filter normalizes end-of-line markers.
321 [1] = {
322 body = mime.eol(0, [[
323 Lines in a message body should always end with CRLF.
324 The smtp module will *NOT* perform translation. It will
325 perform necessary stuffing or '.' characters, though.
326 ]])
327 },
328 -- second part: headers describe content to be a png image,
329 -- sent under the base64 transfer content encoding.
330 -- notice that nothing happens until the message is actually sent.
331 -- small chunks are loaded into memory right before transmission and
332 -- translation happens on the fly.
333 [2] = {
334 headers = {
335 ["content-type"] = 'image/png; name="image.png"',
336 ["content-disposition"] = 'attachment; filename="image.png"',
337 ["content-description"] = 'a beautiful image',
338 ["content-transfer-encoding"] = "BASE64"
339 },
340 body = ltn12.source.chain(
341 ltn12.source.file(io.open("image.png", "rb")),
342 ltn12.filter.chain(
343 mime.encode("base64"),
344 mime.wrap()
345 )
346 )
347 },
348 epilogue = "This might also show up, but after the attachments"
349 }
350}
351
352-- finally send it
353r, e = smtp.send{
354 from = "&lt;sicrano@tecgraf.puc-rio.br&gt;",
355 rcpt = "&lt;fulano@tecgraf.puc-rio.br&gt;",
356 source = source,
204} 357}
205</pre> 358</pre>
206 359