Debian Codegraph @

texk/web2c/luatexdir/luasocket/test/testmesg.lua

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