testmesg.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. -- load the smtp support and its friends
  2. local smtp = require("socket.smtp")
  3. local mime = require("mime")
  4. local ltn12 = require("ltn12")
  5. function filter(s)
  6. if s then io.write(s) end
  7. return s
  8. end
  9. source = smtp.message {
  10. headers = { ['content-type'] = 'multipart/alternative' },
  11. body = {
  12. [1] = {
  13. headers = { ['Content-type'] = 'text/html' },
  14. body = "<html> <body> Hi, <b>there</b>...</body> </html>"
  15. },
  16. [2] = {
  17. headers = { ['content-type'] = 'text/plain' },
  18. body = "Hi, there..."
  19. }
  20. }
  21. }
  22. r, e = smtp.send{
  23. rcpt = {"<[email protected]>",
  24. "<[email protected]>" },
  25. from = "<[email protected]>",
  26. source = ltn12.source.chain(source, filter),
  27. --server = "mail.cs.princeton.edu"
  28. server = "localhost",
  29. port = 2525
  30. }
  31. print(r, e)
  32. -- creates a source to send a message with two parts. The first part is
  33. -- plain text, the second part is a PNG image, encoded as base64.
  34. source = smtp.message{
  35. headers = {
  36. -- Remember that headers are *ignored* by smtp.send.
  37. from = "Sicrano <[email protected]>",
  38. to = "Fulano <[email protected]>",
  39. subject = "Here is a message with attachments"
  40. },
  41. body = {
  42. preamble = "If your client doesn't understand attachments, \r\n" ..
  43. "it will still display the preamble and the epilogue.\r\n" ..
  44. "Preamble might show up even in a MIME enabled client.",
  45. -- first part: No headers means plain text, us-ascii.
  46. -- The mime.eol low-level filter normalizes end-of-line markers.
  47. [1] = {
  48. body = mime.eol(0, [[
  49. Lines in a message body should always end with CRLF.
  50. The smtp module will *NOT* perform translation. It will
  51. perform necessary stuffing, though.
  52. ]])
  53. },
  54. -- second part: Headers describe content the to be an image,
  55. -- sent under the base64 transfer content encoding.
  56. -- Notice that nothing happens until the message is sent. Small
  57. -- chunks are loaded into memory and translation happens on the fly.
  58. [2] = {
  59. headers = {
  60. ["ConTenT-tYpE"] = 'image/png; name="luasocket.png"',
  61. ["content-disposition"] = 'attachment; filename="luasocket.png"',
  62. ["content-description"] = 'our logo',
  63. ["content-transfer-encoding"] = "BASE64"
  64. },
  65. body = ltn12.source.chain(
  66. ltn12.source.file(io.open("luasocket.png", "rb")),
  67. ltn12.filter.chain(
  68. mime.encode("base64"),
  69. mime.wrap()
  70. )
  71. )
  72. },
  73. epilogue = "This might also show up, but after the attachments"
  74. }
  75. }
  76. r, e = smtp.send{
  77. rcpt = {"<[email protected]>",
  78. "<[email protected]>" },
  79. from = "<[email protected]>",
  80. source = ltn12.source.chain(source, filter),
  81. --server = "mail.cs.princeton.edu",
  82. --port = 25
  83. server = "localhost",
  84. port = 2525
  85. }
  86. print(r, e)