Guest User

Untitled

a guest
Sep 26th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  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.  
  6. -- creates a source to send a message with two parts. The first part is
  7. -- plain text, the second part is a PNG image, encoded as base64.
  8. source = smtp.message{
  9. headers = {
  10. ["content-type"] = 'multipart/mixed; charset=utf-8',
  11. -- Remember that headers are *ignored* by smtp.send.
  12. from = "章文 <zwgb2002@163.com>",
  13. to = "zhangwen <zhangwen@ct108.com>",
  14. subject = "自动发送 Here is a message with attachments"
  15. },
  16. body = {
  17. preamble = "If your client doesn't understand attachments, \r\n" ..
  18. "it will still display the preamble and the epilogue.\r\n" ..
  19. "Preamble will probably appear even in a MIME enabled client.",
  20. -- first part: no headers means plain text, us-ascii.
  21. -- The mime.eol low-level filter normalizes end-of-line markers.
  22. [1] = {
  23. headers = {
  24. ["content-type"] = 'text/plain; charset=utf-8'
  25. },
  26. body = mime.eol(0, [[
  27. Lines in a message body should always end with CRLF.
  28. The smtp module will *NOT* perform translation. However, the
  29. send function *DOES* perform SMTP stuffing, whereas the message
  30. function does *NOT*. 测试
  31. ]])
  32. },
  33. -- second part: headers describe content to be a png image,
  34. -- sent under the base64 transfer content encoding.
  35. -- notice that nothing happens until the message is actually sent.
  36. -- small chunks are loaded into memory right before transmission and
  37. -- translation happens on the fly.
  38. [2] = {
  39. headers = {
  40. ["content-type"] = 'image/png; name="image.png"',
  41. ["content-disposition"] = 'attachment; filename="image.png"',
  42. ["content-description"] = 'a beautiful image',
  43. ["content-transfer-encoding"] = "BASE64"
  44. },
  45. body = ltn12.source.chain(
  46. ltn12.source.file(io.open("image.png", "rb")),
  47. ltn12.filter.chain(
  48. mime.encode("base64"),
  49. mime.wrap()
  50. )
  51. )
  52. },
  53. epilogue = "This might also show up, but after the attachments"
  54. }
  55. }
  56.  
  57. io.write("Enter password: ")
  58. local pass = io.read()
  59.  
  60. -- finally send it
  61. r, e = smtp.send{
  62. from = "<zwgb2002@163.com>",
  63. rcpt = "<zhangwen@ct108.com>",
  64. source = source,
  65. server = "smtp.163.com",
  66. user = "zwgb2002",
  67. password = pass
  68. }
  69.  
  70. if not r then
  71. print(e)
  72. else
  73. print("send ok!")
  74. end
Add Comment
Please, Sign In to add comment