Skip to content

Instantly share code, notes, and snippets.

@Danukeru
Created April 1, 2017 16:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Danukeru/c6700d04237503424b878bbc315fb594 to your computer and use it in GitHub Desktop.
Save Danukeru/c6700d04237503424b878bbc315fb594 to your computer and use it in GitHub Desktop.
A simple LUA script to send an e-mail with an attachment.
-- requires an SMTP server to send emails
local smtp = require("socket.smtp")
local ltn12 = require("ltn12")
local mime = require("mime")
from = "<me@my.email.com>"
rcpt = {
"<somebob@testemail.com>"
}
-- the multipart message definition
mesgt = {
headers = {
to = "Bob <somebob@testemail.com>",
subject = "Sending attachment over e-mail test"
},
body = {
-- the message content
[1] = {
body = "Sending you a nice file! :D"
},
-- the file attachment
[2] = {
headers = {
["content-type"] = 'attachment; name="testfile.bin"',
["content-disposition"] = 'attachment; filename="testfile.bin"',
["content-description"] = 'test binary file',
["content-transfer-encoding"] = "BASE64"
},
body = ltn12.source.chain(
ltn12.source.file( io.open( "/path/to/testfile.bin", "rb")),
ltn12.filter.chain(
mime.encode("base64"),
mime.wrap()
)
)
}
}
}
-- send the message
r, e = smtp.send{
from = from,
rcpt = rcpt,
source = smtp.message(mesgt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment