将 PDF 文件附加到电子邮件 - Swift

Attach a PDF file to email - Swift

我想发送带有 PDF 附件的电子邮件。 我创建了 PDF 文件,然后我做了以下我认为是错误的:

// 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)

在发送邮件之前,我可以看到附件chart.pdf,但是当我发送邮件时,发送的邮件没有附件,这是因为我没有正确附加文件。

您将错误的 mimeType 传递给了 addAttachmentData()。使用 application/pdf 而不是 pdf.

我们可以将 PDF 文件附加到电子邮件中并以编程方式发送

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)
    }

第一个,导入:

import MessageUI

并实施 de Email Delegate,例如

public class MyViewController : UIViewController, MFMailComposeViewControllerDelegate { ...

如果你有文件或Data类型,你可以使用这个功能:

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@whosebug.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")

}

compose:

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