| 1 |
|
| 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 |
|
| 31 |
server = "localhost", |
| 32 |
port = 2525 |
| 33 |
} |
| 34 |
|
| 35 |
print(r, e) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
source = smtp.message{ |
| 40 |
headers = { |
| 41 |
|
| 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 |
|
| 51 |
|
| 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 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 89 |
|
| 90 |
server = "localhost", |
| 91 |
port = 2525 |
| 92 |
} |
| 93 |
|
| 94 |
print(r, e) |
| 95 |
|
| 96 |
|