Attach PDF to Email - Swift

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

// 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 an email, I see the attached chart.pdf , but when I sent the email, it was sent without attachment, and this is due to the fact that I did not install the file correctly.

+7
source share
3 answers

You are mistaken mimeType before addAttachmentData() . Use application/pdf instead of pdf .

+10
source

we can make a PDF file attachment by e-mail and send it programmatically

with Swift 2.2

 @IBAction func sendEmail(sender: UIButton) { //Check to see the device can send email. if( MFMailComposeViewController.canSendMail() ) { print("Can send email.") let mailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self //Set to recipients mailComposer.setToRecipients(["your email address heres"]) //Set the subject mailComposer.setSubject("email with document pdf") //set mail body mailComposer.setMessageBody("This is what they sound like.", isHTML: true) if let filePath = NSBundle.mainBundle().pathForResource("All_about_tax", ofType: "pdf") { print("File path loaded.") if let fileData = NSData(contentsOfFile: filePath) { print("File data loaded.") mailComposer.addAttachmentData(fileData, mimeType: "application/pdf", fileName: "All_about_tax.pdf") } } //this will compose and present mail to user self.presentViewController(mailComposer, animated: true, completion: nil) } else { print("email is not supported") } } func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { self.dismissViewControllerAnimated(true, completion: nil) } 
+4
source

First, import:

 import MessageUI 

And implement an email delegate for example

 public class MyViewController : UIViewController, MFMailComposeViewControllerDelegate { ... 

If you have a file or Data type, you can use this function:

 let filePath = NSBundle.mainBundle().pathForResource("chart", ofType: "pdf") let fileData = NSData(contentsOfFile: filePath) sendEmail(data:fileData) 

Swift 4

 func sendEmail(data:Data?){ if( MFMailComposeViewController.canSendMail() ) { let mailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self mailComposer.setToRecipients([" john@stackoverflow.com ", " mrmins@mydomain.com ", " anotheremail@email.com "]) mailComposer.setSubject("Cotización") mailComposer.setMessageBody("My body message", isHTML: true) if let fileData = data { mailComposer.addAttachmentData(fileData, mimeType: "application/pdf", fileName: "MyFileName.pdf") } self.present(mailComposer, animated: true, completion: nil) return } print("Email is not configured") } 

And compose :

 public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?){ self.dismiss(animated: true, completion: nil) print("sent!") } 
+1
source

Source: https://habr.com/ru/post/987817/


All Articles