Attach a PDF file to email - Swift

Xin Lok picture Xin Lok · May 24, 2015 · Viewed 10.8k times · Source

I want to send email with a PDF attachment. I created PDF file, then I did the following which is wrong I believe:

// locate folder containing pdf file            
let documentsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String

let pdfFileName = documentsPath.stringByAppendingPathComponent("chart.pdf")
let fileData = NSData(contentsOfFile: pdfFileName)
mc.addAttachmentData(fileData, mimeType: "pdf", fileName: chart)

Before sending the email, I can see attached chart.pdf, but when I sent the email, it was sent without attachment and this is because I didn't attached correctly the file.

Answer

NightFury picture NightFury · May 24, 2015

You are passing wrong mimeType to addAttachmentData(). Use application/pdf instead of pdf.