Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- load the smtp support and its friends
- local smtp = require("socket.smtp")
- local mime = require("mime")
- local ltn12 = require("ltn12")
- -- 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 = {
- ["content-type"] = 'multipart/mixed; charset=utf-8',
- -- Remember that headers are *ignored* by smtp.send.
- from = "章文 <zwgb2002@163.com>",
- to = "zhangwen <zhangwen@ct108.com>",
- 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 will probably appear 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] = {
- headers = {
- ["content-type"] = 'text/plain; charset=utf-8'
- },
- body = mime.eol(0, [[
- Lines in a message body should always end with CRLF.
- The smtp module will *NOT* perform translation. However, the
- send function *DOES* perform SMTP stuffing, whereas the message
- function does *NOT*. 测试
- ]])
- },
- -- second part: headers describe content to be a png image,
- -- sent under the base64 transfer content encoding.
- -- notice that nothing happens until the message is actually sent.
- -- small chunks are loaded into memory right before transmission 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"
- }
- }
- io.write("Enter password: ")
- local pass = io.read()
- -- finally send it
- r, e = smtp.send{
- from = "<zwgb2002@163.com>",
- rcpt = "<zhangwen@ct108.com>",
- source = source,
- server = "smtp.163.com",
- user = "zwgb2002",
- password = pass
- }
- if not r then
- print(e)
- else
- print("send ok!")
- end
Add Comment
Please, Sign In to add comment