Send e-mail with attachment in Lua

Asked

Viewed 131 times

2

I’m trying to send an e-mail behind the socket. I can send an email with an attachment, but I also want to add text to the email and I can’t. Example of the code:

local smtp = require("socket.smtp")
local mime = require("mime")
local ltn12 = require("ltn12")
rcpt = {
    "[email protected]"
    }

mesgt = {
  headers = {
    ["content-type"] = 'text/html; charset="ISO-8859-1"',
    ["content-transfer-encoding"] = 'quoted-printable',
    to = "exepmo <"[email protected]">",
    ["content-type"] = 'image/png; name="image.png"',
    ["content-disposition"] = 'attachment; filename="image.png"',
    ["content-description"] = 'a beautiful image',
    ["content-transfer-encoding"] = "BASE64"
    subject = sujet
  },

   body = "texto texto texto",
      [1] = { 

      body = ltn12.source.chain(
            ltn12.source.file(io.open("image.png", "rb")),
            ltn12.filter.chain(
                mime.encode("base64"),
                mime.wrap()
            )
        )

        }
      }
   r, e = smtp.send{
      from = from,
      rcpt = rcpt, 
      source = smtp.message(mesgt),
      port = 21,    
      server = servidordemail
  }

The code works if you send either the text or the attachment. If you want to send both does not work for me.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

2

To documentation which you used in the previous question shows how you should do but you are doing it in a different way. Relevant part:

  headers = {
     -- Remember that headers are *ignored* by smtp.send. 
     from = "Sicrano de Oliveira <[email protected]>",
     to = "Fulano da Silva <[email protected]>",
     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] = { 
      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"
  }

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.