diff options
Diffstat (limited to 'doc/smtp.html')
-rw-r--r-- | doc/smtp.html | 235 |
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 |
39 | The <tt>smtp.lua</tt> module provides functionality to send e-mail | ||
40 | messages. The implementation conforms to the Simple Mail Transfer Protocol, | 39 | messages. 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>. |
42 | The other RFC of interest in this implementation is | 41 | Another RFC of interest is <a |
43 | <a href="http://www.cs.princeton.edu/~diego/rfc/rfc2822.txt">RFC 2822</a>, | 42 | href="http://www.cs.princeton.edu/~diego/rfc/rfc2822.txt">RFC 2822</a>, |
44 | which governs the Internet Message Format. | 43 | which governs the Internet Message Format. |
44 | Multipart messages (those that contain attatchments) are part | ||
45 | of the MIME standard, but described mainly | ||
46 | in <a href="http://www.cs.princeton.edu/~diego/rfc/rfc2046.txt">RFC | ||
47 | 2046</a> | ||
45 | 48 | ||
46 | </p> | 49 | <p> In the description below, good understanding of <a |
50 | href="http://lua-users.org/wiki/FiltersSourcesAndSinks"> LTN012, Filters | ||
51 | sources and sinks</a> and the <a href=mime.html>MIME</a> module is | ||
52 | assumed. In fact, the SMTP module was the main reason for their | ||
53 | creation. </p> | ||
47 | 54 | ||
48 | <p> | 55 | <p> |
49 | MIME headers are represented as a Lua table in the form: | 56 | MIME 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 | |||
78 | in representing them in a Lua table. | 85 | in representing them in a Lua table. |
79 | </p> | 86 | </p> |
80 | 87 | ||
81 | <!-- mail +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 88 | <p> |
89 | The following constants can be set to control the default behaviour of | ||
90 | the SMTP module: | ||
91 | </p> | ||
82 | 92 | ||
83 | <p class=name id=mail> | 93 | <ul> |
84 | socket.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> | ||
104 | smtp.<b>send{</b><br> | ||
85 | from = <i>string</i>,<br> | 105 | from = <i>string</i>,<br> |
86 | rcpt = <i>string</i> or <i>string-table</i>,<br> | 106 | rcpt = <i>string</i> or <i>string-table</i>,<br> |
87 | body = <i>string</i>,<br> | 107 | source = <i>LTN12 source</i>,<br> |
88 | headers = <i>headers-table</i>,<br> | 108 | [server = <i>string</i>],<br> |
89 | server = <i>string</i><br> | 109 | [port = <i>string</i>]<br> |
110 | [domain = <i>string</i>],<br> | ||
111 | [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> |
94 | Sends a message to a recipient list. | 116 | Sends a message to a recipient list. Since sending messages is not as |
117 | simple as downloading an URL from a FTP or HTTP server, this function | ||
118 | doesn't have a simple interface. However, see the | ||
119 | <a href=#message><tt>message</tt></a> source factory for | ||
120 | a 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 | 124 | The 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 | ||
126 | address, or a string | ||
99 | in case there is just one recipient. | 127 | in case there is just one recipient. |
100 | The sender is given by the e-mail address <tt>from</tt>. | 128 | The contents of the message are given by a LTN12 <tt>source</tt>. Several |
101 | The message is composed by the optional MIME Headers <tt>headers</tt> | 129 | arguments are optional: |
102 | and 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 | ||
134 | local machine host name; | ||
135 | <li> <tt>step</tt>: LTN12 pump step function used to pass data from the | ||
136 | source 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> |
146 | Note: SMTP servers are can be very picky with the format of e-mail | ||
147 | addresses. To be safe, use only addresses of the form | ||
148 | "<tt><fulano@tecgraf.puc-rio.br></tt>" in the <tt>from</tt> and | ||
149 | <tt>rcpt</tt> arguments to the <tt>send</tt> function. In headers, e-mail | ||
150 | addresses can take whatever form you like. </p> | ||
151 | |||
152 | <p class=note> | ||
112 | Big note: There is a good deal of misconception with the use of the | 153 | Big note: There is a good deal of misconception with the use of the |
113 | destination address field headers, i.e., the '<tt>To</tt>', '<tt>Cc</tt>', | 154 | destination address field headers, i.e., the '<tt>To</tt>', '<tt>Cc</tt>', |
114 | and, more importantly, the '<tt>Bcc</tt>' headers. Do <em>not</em> add a | 155 | and, 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> |
120 | Only recipients specified in the recipient list will receive a copy of the | 161 | Only recipients specified in the <tt>rcpt</tt> list will receive a copy of the |
121 | message. Each recipient of an SMTP mail message receives a copy of the | 162 | message. Each recipient of an SMTP mail message receives a copy of the |
122 | message body along with the headers, and nothing more. The headers are | 163 | message body along with the headers, and nothing more. The headers |
123 | considered 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 |
124 | part of the message. | 165 | <tt>source</tt> function. The <tt>rcpt</tt> list is <em>not</em> |
166 | part 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> |
146 | The LuaSocket <tt>mail</tt> function does not interpret the headers you | 188 | The LuaSocket <tt>send</tt> function does not care or interpret the |
147 | pass to, but it gives you full control over what is sent and to whom | 189 | headers you send, but it gives you full control over what is sent and |
148 | it is sent: | 190 | to 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 | ||
217 | local 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. |
180 | headers = { | 225 | from = "<luasocket@tecgraf.puc-rio.br>" |
181 | to = "fulano@tecgraf.puc-rio.br", | ||
182 | cc = "beltrano@tecgraf.puc-rio.br", | ||
183 | subject = "LuaSocket test message" | ||
184 | } | ||
185 | |||
186 | from = "luasocket@tecgraf.puc-rio.br" | ||
187 | 226 | ||
188 | rcpt = { | 227 | rcpt = { |
189 | "fulano@tecgraf.puc-rio.br", | 228 | "<fulano@tecgraf.puc-rio.br>", |
190 | "beltrano@tecgraf.puc-rio.br", | 229 | "<beltrano@tecgraf.puc-rio.br>", |
191 | "sicrano@tecgraf.puc-rio.br" | 230 | "<sicrano@tecgraf.puc-rio.br>" |
192 | } | 231 | } |
193 | 232 | ||
194 | body = "This is a test message. Please ignore." | 233 | mesgt = { |
195 | 234 | headers = { | |
196 | server = "localhost" | 235 | to = "Fulano da Silva <fulano@tecgraf.puc-rio.br>", |
236 | cc = '"Beltrano F. Nunes" <beltrano@tecgraf.puc-rio.br>', | ||
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 | ||
198 | r, e = socket.smtp.mail{ | 242 | r, 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> | ||
252 | smtp.<b>message(</b>mesgt<b>)</b> | ||
253 | </p> | ||
254 | |||
255 | <p class=description> | ||
256 | Returns a LTN12 source that sends an SMTP message body, possibly multipart | ||
257 | (arbitrarily deep). | ||
258 | </p> | ||
259 | |||
260 | <p class=parameters> | ||
261 | The 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> | ||
268 | mesgt = {<br> | ||
269 | headers = <i>header-table</i>,<br> | ||
270 | body = <i>LTN12 source</i> or <i>string</i> or | ||
271 | <i>multipart-mesgt</i><br> | ||
272 | }<br> | ||
273 | <br> | ||
274 | multipart-mesgt = {<br> | ||
275 | preamble = <i>string</i><br> | ||
276 | [1] = <i>mesgt</i>,<br> | ||
277 | [2] = <i>mesgt</i>,<br> | ||
278 | ...<br> | ||
279 | [<i>n</i>] = <i>mesgt</i>,<br> | ||
280 | epilogue = <i>string</i>,<br> | ||
281 | }<br> | ||
282 | </tt></td></tr> | ||
283 | </table> | ||
284 | </blockquote> | ||
285 | |||
286 | <p class=parameters> | ||
287 | For a simple message, all that is needed is a set of <tt>headers</tt> | ||
288 | and the <tt>body</tt>. The message <tt>body</tt> can be given as a string | ||
289 | or as a LTN12 source. For multipart messages, the body is a table that | ||
290 | recursively defines each part as an independent message, plus a preamble | ||
291 | and an epilogue. | ||
292 | </p> | ||
293 | |||
294 | <p class=return> | ||
295 | The function returns an LTN12 source that produces the message contents as | ||
296 | defined by <tt>mesgt</tt>. Hopefuly, the following example will make | ||
297 | things clear. When in doubt, refer to the appropriate RFC as listed in the | ||
298 | introduction. </p> | ||
299 | |||
300 | <pre class=example> | ||
301 | -- load the smtp support and its friends | ||
302 | local smtp = require("smtp") | ||
303 | local mime = require("mime") | ||
304 | local 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. | ||
308 | source = smtp.message{ | ||
309 | headers = { | ||
310 | -- Remember that headers are *ignored* by smtp.send. | ||
311 | from = "Sicrano de Oliveira <sicrano@tecgraf.puc-rio.br>", | ||
312 | to = "Fulano da Silva <fulano@tecgraf.puc-rio.br>", | ||
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 | ||
353 | r, e = smtp.send{ | ||
354 | from = "<sicrano@tecgraf.puc-rio.br>", | ||
355 | rcpt = "<fulano@tecgraf.puc-rio.br>", | ||
356 | source = source, | ||
204 | } | 357 | } |
205 | </pre> | 358 | </pre> |
206 | 359 | ||