2004-06-15 08:24:00 +02:00
|
|
|
-- load the smtp support and its friends
|
|
|
|
local smtp = require("smtp")
|
|
|
|
local mime = require("mime")
|
|
|
|
local ltn12 = require("ltn12")
|
2004-05-31 01:30:00 +02:00
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
-- 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" ..
|
2004-06-16 03:02:07 +02:00
|
|
|
"it will still display the preamble and the epilogue.\r\n" ..
|
2004-06-15 08:24:00 +02:00
|
|
|
"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.
|
|
|
|
]])
|
2004-03-19 07:14:56 +01:00
|
|
|
},
|
2004-06-15 08:24:00 +02:00
|
|
|
-- 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="image.png"',
|
|
|
|
["content-disposition"] = 'attachment; filename="image.png"',
|
|
|
|
["content-description"] = 'a beautiful image',
|
|
|
|
["content-transfer-encoding"] = "BASE64"
|
|
|
|
},
|
|
|
|
body = ltn12.source.chain(
|
|
|
|
ltn12.source.file(io.open("image.png", "rb")),
|
|
|
|
ltn12.filter.chain(
|
|
|
|
mime.encode("base64"),
|
|
|
|
mime.wrap()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
epilogue = "This might also show up, but after the attachments"
|
|
|
|
}
|
2004-03-19 07:14:56 +01:00
|
|
|
}
|
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
-- finally send it
|
|
|
|
r, e = smtp.send{
|
2004-03-21 08:50:15 +01:00
|
|
|
rcpt = "<diego@cs.princeton.edu>",
|
2004-03-19 07:14:56 +01:00
|
|
|
from = "<diego@cs.princeton.edu>",
|
2004-06-15 08:24:00 +02:00
|
|
|
source = source,
|
2004-05-26 06:58:32 +02:00
|
|
|
server = "mail.cs.princeton.edu"
|
2004-06-15 08:24:00 +02:00
|
|
|
}
|
2004-06-16 03:02:07 +02:00
|
|
|
|
|
|
|
print(r, e)
|