Mailed PDF Documents spit out garbage

I have a MacOS app that generates PDF document and adds them to an email. About 10% turns the PDF into a stream of characters like:

–a27mcmea8awd
Content-Type: application/pdf; name=“Receipt123.pdf”; name*=utf-8’‘Receipt123.pdf
Content-transfer-encoding: Base64
Content-disposition: attachment; filename=“Receipt123.pdf”; filename*=utf-8’'Receipt123.pdf

JVBERi0xLjUKJeLjz9MKMSAwIG9iaiA8PCAvVHlwZSAvQ2F0YWxvZyAvTGFuZyAoKSAvUGFnZXMg

Does anyone know why this happens? No errors are reported and the PDF file is not corrupte

Thaks
Robin

Looks good to me.
If you see this in the email app, the mime packaging failed.
e.g. instead of adding the PDF as mime part, the mime part shows as plain text.

100% of emails should look like this because attachments are always added as base64. The name is wrong, by the way. It should say name=“Receipt123.pdf”. name* concatenates the 2 parts of the attachment name.

How do you create and send your emails?

Var sendfile As FolderItem = SpecialFolder.Documents.Child("Bryndwr Receipts " +str(thisYear)).Child(fileName) //1.3.4

If sendfile <> Nil And sendfile.Exists Then

Var attachment As New EmailAttachment

attachment.ContentEncoding = “UTF8” //1.3.4

attachment.LoadFromFile(sendfile)
attachment.Name = sendfile.Name

If sendfile.Name.Right(4) = “.pdf” Then
attachment.MIMEType = “application/pdf”
Else
Msgbox("File extension is "+ sendfile.Name.Right(4)) //1.3.4
End If

mail.Attachments.Add(attachment)
Else
Msgbox(“No pdf file to send”) //1.3.4 warn about problem
End If

// setting these up earlier doesn’t work

mainWindow.mailSocket.Address = App.EmailServerAddress

mainWindow.mailSocket.Password = App.EmailUserPassword

mainWindow.mailSocket.Username = App.EmailUserName

// send the email
MainWindow.MailSocket.Messages.Append(mail)

MainWindow.MailSocket.SendMail

Why do you assign this?
Doesn’t make sense for pdf.

Setting the ContentEncoding is directly out of the examples.

I can’t even inspect the body of the email in the debugger. And BodyHtml is broken, too.

If anyone needs an alternative, please check the CURLEmailMBS class in MBS Xojo Plugins.

Dim mail As EmailMessage

var body as String = “This is your tax receipt for the year ending on March 31.”+chr(13)+chr(10)+chr(10) …

// populate the email message
mail = New EmailMessage
mail.FromAddress = “robin.harrington@snap.net.nz”
mail.Subject = “Bryndwr Chapel Donation Receipt”
mail.BodyPlainText = body

mail.Headers.AppendHeader(“X-Mailer”,“Bryndwr Donor System”)

// add recipients

var recepient as String = donor.emailAddress

mail.AddRecipient(recepient)

When posting code, please:

  1. add the code to your post
  2. select it all with the mouse
  3. click the </> button

makes it much more legible.

The odd thing about these failures is that they affect a small number of people but are repeatable.

But to try and send the same PDF file (but not to the same address), it does not fail. They are all gmail accounts but other gmail accounts are not affected. I haven’t been able to find any common factor as yet.

Weird

After lots of experiments, it seems that the problem is that a small number of clients have trouble decoding an Xojo attachment as a PDF.

Today accidentally found that it work properly if the email has a CC address.

Does that make sense? Not to me but it works,

Robin