aboutsummaryrefslogtreecommitdiff
path: root/docs/smtp.html
diff options
context:
space:
mode:
authorCaleb Maclennan <caleb@alerque.com>2023-11-10 09:12:04 +0300
committerCaleb Maclennan <caleb@alerque.com>2023-11-10 09:12:04 +0300
commit5c4fc93d5f4137bf4c22ddf1a048c907a4a26727 (patch)
treea9a68e1f6a9c3bfe2b64fa1c3a4098865b7d3b5d /docs/smtp.html
parentccef3bc4e2aa6ee5b997a80aabb58f4ff0b0e98f (diff)
parent43a97b7f0053313b43906371dbdc226271e6c8ab (diff)
downloadluasocket-hjelmeland-patch-1.tar.gz
luasocket-hjelmeland-patch-1.tar.bz2
luasocket-hjelmeland-patch-1.zip
Merge branch 'master' into hjelmeland-patch-1hjelmeland-patch-1
Diffstat (limited to 'docs/smtp.html')
-rw-r--r--docs/smtp.html419
1 files changed, 419 insertions, 0 deletions
diff --git a/docs/smtp.html b/docs/smtp.html
new file mode 100644
index 0000000..787d0b1
--- /dev/null
+++ b/docs/smtp.html
@@ -0,0 +1,419 @@
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4
5<head>
6<meta name="description" content="LuaSocket: SMTP support">
7<meta name="keywords" content="Lua, LuaSocket, SMTP, E-Mail, MIME, Multipart,
8Library, Support">
9<title>LuaSocket: SMTP support</title>
10<link rel="stylesheet" href="reference.css" type="text/css">
11</head>
12
13<body>
14
15<!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
16
17<div class="header">
18<hr>
19<center>
20<table summary="LuaSocket logo">
21<tr><td align="center"><a href="http://www.lua.org">
22<img width="128" height="128" border="0" alt="LuaSocket" src="luasocket.png">
23</a></td></tr>
24<tr><td align="center" valign="top">Network support for the Lua language
25</td></tr>
26</table>
27<p class="bar">
28<a href="index.html">home</a> &middot;
29<a href="index.html#download">download</a> &middot;
30<a href="installation.html">installation</a> &middot;
31<a href="introduction.html">introduction</a> &middot;
32<a href="reference.html">reference</a>
33</p>
34</center>
35<hr>
36</div>
37
38<!-- smtp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
39
40<h2 id="smtp">SMTP</h2>
41
42<p> The <tt>smtp</tt> namespace provides functionality to send e-mail
43messages. The high-level API consists of two functions: one to
44define an e-mail message, and another to actually send the message.
45Although almost all users will find that these functions provide more than
46enough functionality, the underlying implementation allows for even more
47control (if you bother to read the code).
48</p>
49
50<p>The implementation conforms to the Simple Mail Transfer Protocol,
51<a href="http://www.ietf.org/rfc/rfc2821.txt">RFC 2821</a>.
52Another RFC of interest is <a
53href="http://www.ietf.org/rfc/rfc2822.txt">RFC 2822</a>,
54which governs the Internet Message Format.
55Multipart messages (those that contain attachments) are part
56of the MIME standard, but described mainly
57in <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a>.</p>
58
59<p> In the description below, good understanding of <a
60href="http://lua-users.org/wiki/FiltersSourcesAndSinks"> LTN012, Filters
61sources and sinks</a> and the <a href="mime.html">MIME</a> module is
62assumed. In fact, the SMTP module was the main reason for their
63creation. </p>
64
65<p>
66To obtain the <tt>smtp</tt> namespace, run:
67</p>
68
69<pre class="example">
70-- loads the SMTP module and everything it requires
71local smtp = require("socket.smtp")
72</pre>
73
74<p>
75MIME headers are represented as a Lua table in the form:
76</p>
77
78<blockquote>
79<table summary="MIME headers in Lua table">
80<tr><td><tt>
81headers = {<br>
82&nbsp;&nbsp;field-1-name = <i>field-1-value</i>,<br>
83&nbsp;&nbsp;field-2-name = <i>field-2-value</i>,<br>
84&nbsp;&nbsp;field-3-name = <i>field-3-value</i>,<br>
85&nbsp;&nbsp;...<br>
86&nbsp;&nbsp;field-n-name = <i>field-n-value</i><br>
87}
88</tt></td></tr>
89</table>
90</blockquote>
91
92<p>
93Field names are case insensitive (as specified by the standard) and all
94functions work with lowercase field names (but see
95<a href="socket.html#headers.canonic"><tt>socket.headers.canonic</tt></a>).
96Field values are left unmodified.
97</p>
98
99<p class="note">
100Note: MIME headers are independent of order. Therefore, there is no problem
101in representing them in a Lua table.
102</p>
103
104<p>
105The following constants can be set to control the default behavior of
106the SMTP module:
107</p>
108
109<ul>
110<li> <tt>DOMAIN</tt>: domain used to greet the server;</li>
111<li> <tt>PORT</tt>: default port used for the connection;</li>
112<li> <tt>SERVER</tt>: default server used for the connection;</li>
113<li> <tt>TIMEOUT</tt>: default timeout for all I/O operations;</li>
114<li> <tt>ZONE</tt>: default time zone.</li>
115</ul>
116
117<!-- message ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
118
119<p class="name" id="message">
120smtp.<b>message(</b>mesgt<b>)</b>
121</p>
122
123<p class="description">
124Returns a <em>simple</em>
125<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> source that sends an SMTP message body, possibly multipart (arbitrarily deep).
126</p>
127
128<p class="parameters">
129The only parameter of the function is a table describing the message.
130<tt>Mesgt</tt> has the following form (notice the recursive structure):
131</p>
132
133<blockquote>
134<table summary="Mesgt table structure">
135<tr><td><tt>
136mesgt = {<br>
137&nbsp;&nbsp;headers = <i>header-table</i>,<br>
138&nbsp;&nbsp;body = <i>LTN12 source</i> or <i>string</i> or
139<i>multipart-mesgt</i><br>
140}<br>
141&nbsp;<br>
142multipart-mesgt = {<br>
143&nbsp;&nbsp;[preamble = <i>string</i>,]<br>
144&nbsp;&nbsp;[1] = <i>mesgt</i>,<br>
145&nbsp;&nbsp;[2] = <i>mesgt</i>,<br>
146&nbsp;&nbsp;...<br>
147&nbsp;&nbsp;[<i>n</i>] = <i>mesgt</i>,<br>
148&nbsp;&nbsp;[epilogue = <i>string</i>,]<br>
149}<br>
150</tt></td></tr>
151</table>
152</blockquote>
153
154<p class="parameters">
155For a simple message, all that is needed is a set of <tt>headers</tt>
156and the <tt>body</tt>. The message <tt>body</tt> can be given as a string
157or as a <em>simple</em>
158<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
159source. For multipart messages, the body is a table that
160recursively defines each part as an independent message, plus an optional
161<tt>preamble</tt> and <tt>epilogue</tt>.
162</p>
163
164<p class="return">
165The function returns a <em>simple</em>
166<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
167source that produces the
168message contents as defined by <tt>mesgt</tt>, chunk by chunk.
169Hopefully, the following
170example will make things clear. When in doubt, refer to the appropriate RFC
171as listed in the introduction. </p>
172
173<pre class="example">
174-- load the smtp support and its friends
175local smtp = require("socket.smtp")
176local mime = require("mime")
177local ltn12 = require("ltn12")
178
179-- creates a source to send a message with two parts. The first part is
180-- plain text, the second part is a PNG image, encoded as base64.
181source = smtp.message{
182 headers = {
183 -- Remember that headers are *ignored* by smtp.send.
184 from = "Sicrano de Oliveira &lt;sicrano@example.com&gt;",
185 to = "Fulano da Silva &lt;fulano@example.com&gt;",
186 subject = "Here is a message with attachments"
187 },
188 body = {
189 preamble = "If your client doesn't understand attachments, \r\n" ..
190 "it will still display the preamble and the epilogue.\r\n" ..
191 "Preamble will probably appear even in a MIME enabled client.",
192 -- first part: no headers means plain text, us-ascii.
193 -- The mime.eol low-level filter normalizes end-of-line markers.
194 [1] = {
195 body = mime.eol(0, [[
196 Lines in a message body should always end with CRLF.
197 The smtp module will *NOT* perform translation. However, the
198 send function *DOES* perform SMTP stuffing, whereas the message
199 function does *NOT*.
200 ]])
201 },
202 -- second part: headers describe content to be a png image,
203 -- sent under the base64 transfer content encoding.
204 -- notice that nothing happens until the message is actually sent.
205 -- small chunks are loaded into memory right before transmission and
206 -- translation happens on the fly.
207 [2] = {
208 headers = {
209 ["content-type"] = 'image/png; name="image.png"',
210 ["content-disposition"] = 'attachment; filename="image.png"',
211 ["content-description"] = 'a beautiful image',
212 ["content-transfer-encoding"] = "BASE64"
213 },
214 body = ltn12.source.chain(
215 ltn12.source.file(io.open("image.png", "rb")),
216 ltn12.filter.chain(
217 mime.encode("base64"),
218 mime.wrap()
219 )
220 )
221 },
222 epilogue = "This might also show up, but after the attachments"
223 }
224}
225
226-- finally send it
227r, e = smtp.send{
228 from = "&lt;sicrano@example.com&gt;",
229 rcpt = "&lt;fulano@example.com&gt;",
230 source = source,
231}
232</pre>
233
234
235<!-- send +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
236
237<p class="name" id="send">
238smtp.<b>send{</b><br>
239&nbsp;&nbsp;from = <i>string</i>,<br>
240&nbsp;&nbsp;rcpt = <i>string</i> or <i>string-table</i>,<br>
241&nbsp;&nbsp;source = <i>LTN12 source</i>,<br>
242&nbsp;&nbsp;[user = <i>string</i>,]<br>
243&nbsp;&nbsp;[password = <i>string</i>,]<br>
244&nbsp;&nbsp;[server = <i>string</i>,]<br>
245&nbsp;&nbsp;[port = <i>number</i>,]<br>
246&nbsp;&nbsp;[domain = <i>string</i>,]<br>
247&nbsp;&nbsp;[step = <i>LTN12 pump step</i>,]<br>
248&nbsp;&nbsp;[create = <i>function</i>]<br>
249<b>}</b>
250</p>
251
252<p class="description">
253Sends a message to a recipient list. Since sending messages is not as
254simple as downloading an URL from a FTP or HTTP server, this function
255doesn't have a simple interface. However, see the
256<a href="#message"><tt>message</tt></a> source factory for
257a very powerful way to define the message contents.
258</p>
259
260
261<p class="parameters">
262The sender is given by the e-mail address in the <tt>from</tt> field.
263<tt>Rcpt</tt> is a Lua table with one entry for each recipient e-mail
264address, or a string
265in case there is just one recipient.
266The contents of the message are given by a <em>simple</em>
267<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
268<tt>source</tt>. Several arguments are optional:
269</p>
270<ul>
271<li> <tt>user</tt>, <tt>password</tt>: User and password for
272authentication. The function will attempt LOGIN and PLAIN authentication
273methods if supported by the server (both are unsafe);</li>
274<li> <tt>server</tt>: Server to connect to. Defaults to "localhost";</li>
275<li> <tt>port</tt>: Port to connect to. Defaults to 25;</li>
276<li> <tt>domain</tt>: Domain name used to greet the server; Defaults to the
277local machine host name;</li>
278<li> <tt>step</tt>:
279<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
280pump step function used to pass data from the
281source to the server. Defaults to the LTN12 <tt>pump.step</tt> function;</li>
282<li><tt>create</tt>: An optional function to be used instead of
283<a href="tcp.html#socket.tcp"><tt>socket.tcp</tt></a> when the communications socket is created.</li>
284</ul>
285
286<p class="return">
287If successful, the function returns 1. Otherwise, the function returns
288<b><tt>nil</tt></b> followed by an error message.
289</p>
290
291<p class="note">
292Note: SMTP servers can be very picky with the format of e-mail
293addresses. To be safe, use only addresses of the form
294"<tt>&lt;fulano@example.com&gt;</tt>" in the <tt>from</tt> and
295<tt>rcpt</tt> arguments to the <tt>send</tt> function. In headers, e-mail
296addresses can take whatever form you like. </p>
297
298<p class="note">
299Big note: There is a good deal of misconception with the use of the
300destination address field headers, i.e., the '<tt>To</tt>', '<tt>Cc</tt>',
301and, more importantly, the '<tt>Bcc</tt>' headers. Do <em>not</em> add a
302'<tt>Bcc</tt>' header to your messages because it will probably do the
303exact opposite of what you expect.
304</p>
305
306<p class="note">
307Only recipients specified in the <tt>rcpt</tt> list will receive a copy of the
308message. Each recipient of an SMTP mail message receives a copy of the
309message body along with the headers, and nothing more. The headers
310<em>are</em> part of the message and should be produced by the
311<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
312<tt>source</tt> function. The <tt>rcpt</tt> list is <em>not</em>
313part of the message and will not be sent to anyone.
314</p>
315
316<p class="note">
317<a href="http://www.ietf.org/rfc/rfc2822.txt">RFC 2822</a>
318has two <em>important and short</em> sections, "3.6.3. Destination address
319fields" and "5. Security considerations", explaining the proper
320use of these headers. Here is a summary of what it says:
321</p>
322
323<ul>
324<li> <tt>To</tt>: contains the address(es) of the primary recipient(s)
325of the message;</li>
326<li> <tt>Cc</tt>: (where the "Cc" means "Carbon Copy" in the sense of
327making a copy on a typewriter using carbon paper) contains the
328addresses of others who are to receive the message, though the
329content of the message may not be directed at them;</li>
330<li> <tt>Bcc</tt>: (where the "Bcc" means "Blind Carbon
331Copy") contains addresses of recipients of the message whose addresses are not
332to be revealed to other recipients of the message.</li>
333</ul>
334
335<p class="note">
336The LuaSocket <tt>send</tt> function does not care or interpret the
337headers you send, but it gives you full control over what is sent and
338to whom it is sent:
339</p>
340<ul>
341<li> If someone is to receive the message, the e-mail address <em>has</em>
342to be in the recipient list. This is the only parameter that controls who
343gets a copy of the message;</li>
344<li> If there are multiple recipients, none of them will automatically
345know that someone else got that message. That is, the default behavior is
346similar to the <tt>Bcc</tt> field of popular e-mail clients;</li>
347<li> It is up to you to add the <tt>To</tt> header with the list of primary
348recipients so that other recipients can see it;</li>
349<li> It is also up to you to add the <tt>Cc</tt> header with the
350list of additional recipients so that everyone else sees it;</li>
351<li> Adding a header <tt>Bcc</tt> is nonsense, unless it is
352empty. Otherwise, everyone receiving the message will see it and that is
353exactly what you <em>don't</em> want to happen!</li>
354</ul>
355
356<p class="note">
357I hope this clarifies the issue. Otherwise, please refer to
358<a href="http://www.ietf.org/rfc/rfc2821.txt">RFC 2821</a>
359and
360<a href="http://www.ietf.org/rfc/rfc2822.txt">RFC 2822</a>.
361</p>
362
363<pre class="example">
364-- load the smtp support
365local smtp = require("socket.smtp")
366
367-- Connects to server "localhost" and sends a message to users
368-- "fulano@example.com", "beltrano@example.com",
369-- and "sicrano@example.com".
370-- Note that "fulano" is the primary recipient, "beltrano" receives a
371-- carbon copy and neither of them knows that "sicrano" received a blind
372-- carbon copy of the message.
373from = "&lt;luasocket@example.com&gt;"
374
375rcpt = {
376 "&lt;fulano@example.com&gt;",
377 "&lt;beltrano@example.com&gt;",
378 "&lt;sicrano@example.com&gt;"
379}
380
381mesgt = {
382 headers = {
383 to = "Fulano da Silva &lt;fulano@example.com&gt;",
384 cc = '"Beltrano F. Nunes" &lt;beltrano@example.com&gt;',
385 subject = "My first message"
386 },
387 body = "I hope this works. If it does, I can send you another 1000 copies."
388}
389
390r, e = smtp.send{
391 from = from,
392 rcpt = rcpt,
393 source = smtp.message(mesgt)
394}
395</pre>
396
397<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
398
399<div class="footer">
400<hr>
401<center>
402<p class="bar">
403<a href="index.html">home</a> &middot;
404<a href="index.html#down">download</a> &middot;
405<a href="installation.html">installation</a> &middot;
406<a href="introduction.html">introduction</a> &middot;
407<a href="reference.html">reference</a>
408</p>
409<p>
410<small>
411Last modified by Diego Nehab on <br>
412Thu Apr 20 00:25:51 EDT 2006
413</small>
414</p>
415</center>
416</div>
417
418</body>
419</html>