summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/luasocket/test/testmesg.lua
blob: 135a008d6644c9f3ea691a57d4f8df5c89a01cf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-- load the smtp support and its friends
local smtp = require("socket.smtp")
local mime = require("mime")
local ltn12 = require("ltn12")

function filter(s)
    if s then io.write(s) end
    return s
end

source = smtp.message {
    headers = { ['content-type'] = 'multipart/alternative' },
    body = {
        [1] = {
            headers = { ['Content-type'] = 'text/html' },
            body = "<html> <body> Hi, <b>there</b>...</body> </html>"
        },
        [2] = {
            headers = { ['content-type'] = 'text/plain' },
            body = "Hi, there..."
        }
    }
}

r, e = smtp.send{
    rcpt = {"<diego@tecgraf.puc-rio.br>",
            "<diego@princeton.edu>" },
    from = "<diego@princeton.edu>",
    source = ltn12.source.chain(source, filter),
    --server = "mail.cs.princeton.edu"
    server = "localhost",
    port = 2525
}

print(r, e)

-- creates a source to send a message with two parts. The first part is 
-- plain text, the second part is a PNG image, encoded as base64.
source = smtp.message{
  headers = {
     -- Remember that headers are *ignored* by smtp.send. 
     from = "Sicrano <sicrano@tecgraf.puc-rio.br>",
     to = "Fulano <fulano@tecgraf.puc-rio.br>",
     subject = "Here is a message with attachments"
  },
  body = {
    preamble = "If your client doesn't understand attachments, \r\n" ..
               "it will still display the preamble and the epilogue.\r\n" ..
               "Preamble might show up even in a MIME enabled client.",
    -- first part: No headers means plain text, us-ascii.
    -- The mime.eol low-level filter normalizes end-of-line markers.
    [1] = { 
      body = mime.eol(0, [[
        Lines in a message body should always end with CRLF. 
        The smtp module will *NOT* perform translation. It will
        perform necessary stuffing, though.
      ]])
    },
    -- second part: Headers describe content the to be an image, 
    -- sent under the base64 transfer content encoding.
    -- Notice that nothing happens until the message is sent. Small 
    -- chunks are loaded into memory and translation happens on the fly.
    [2] = { 
      headers = {
        ["ConTenT-tYpE"] = 'image/png; name="luasocket.png"',
        ["content-disposition"] = 'attachment; filename="luasocket.png"',
        ["content-description"] = 'our logo',
        ["content-transfer-encoding"] = "BASE64"
      },
      body = ltn12.source.chain(
        ltn12.source.file(io.open("luasocket.png", "rb")),
        ltn12.filter.chain(
          mime.encode("base64"),
          mime.wrap()
        )
      )
    },
    epilogue = "This might also show up, but after the attachments"
  }
}


r, e = smtp.send{
    rcpt = {"<diego@tecgraf.puc-rio.br>",
            "<diego@princeton.edu>" },
    from = "<diego@princeton.edu>",
    source = ltn12.source.chain(source, filter),
    --server = "mail.cs.princeton.edu",
    --port = 25
    server = "localhost",
    port = 2525
}

print(r, e)