smtp.html 14 KB

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