nimMime icon indicating copy to clipboard operation
nimMime copied to clipboard

When I attach a PDF file, it is corrupted in the received message

Open SiegfriedEhret opened this issue 2 years ago • 4 comments

Hello,

Thanks a lot for this lib, it helps a lot.

I tried the code from the readme with a basic pdf file:

input.pdf

In the message I received, it ends up like this: input-1.pdf

Any idea about what is happening? (I have not yet tried another smtp provider, this is my next step => tried, same result).

Here is the full code I have:

import std/[smtp, os, strutils]
import mime

proc sendTestEmail*() =
  let smtpRelayHost = getEnv("SMTP_RELAY_HOST", "")
  let smtpRelayPort = parseInt(getEnv("SMTP_RELAY_PORT", ""))
  let smtpUsername = getEnv("SMTP_USERNAME", "")
  let smtpPassword = getEnv("SMTP_PASSWORD", "")
  let mailFrom = getEnv("MAIL_FROM", "")
  let sender = getEnv("SENDER", "")
  let recipient = getEnv("RECIPIENT", "")

  ## Compose your mime message
  var
    pdf = newAttachment(readFile("/Users/sehret/dev/input.pdf"),
        filename = "input.pdf")

  pdf.encodeQuotedPrintables()

  var email = newEmail("Hello friend", "Iñtërnâtiônàlizætiøn☃💩",
      sender, @[recipient],
      attachments = @[pdf])

  ## Send it using smtp.nim
  var smtpConn = newSmtp(
    useSsl = true,
    debug = true
  )
  smtpConn.connect(smtpRelayHost, Port smtpRelayPort)
  smtpConn.auth(smtpUsername, smtpPassword)
  smtpConn.sendMail(mailFrom, @[recipient],
      $email.finalize())
  smtpConn.close()

Cheers!

SiegfriedEhret avatar Jul 08 '22 10:07 SiegfriedEhret

Hey @SiegfriedEhret, thank you for this issue!

Can you try to use:

pdf.encodeBase64()

instead of encodeQuotedPrintables() i wonder if this works. In theory, encodeQuotedPrintables should also work for this, but could produce larger attachments. i guess there is a bug in encodeQuotedPrintables but not checked yet.

enthus1ast avatar Jul 09 '22 00:07 enthus1ast

I found the issue, will make a release soon

enthus1ast avatar Jul 10 '22 13:07 enthus1ast

Thanks a lot!

It worked well with pdf.encodeBase64() :).

SiegfriedEhret avatar Jul 11 '22 05:07 SiegfriedEhret

TODO use the new (correct and fast) https://github.com/enthus1ast/nimQuotedPrintables

enthus1ast avatar Jul 11 '22 13:07 enthus1ast