Send Mail with Attachment in iOS

if MFMailComposeViewController.canSendMail() {
    let data = prepareCSVData()
    
    let mailController = MFMailComposeViewController()
    mailController.mailComposeDelegate = self
    mailController.setSubject("Data Export")
    mailController.setMessageBody("Attached is a CSV file containing your data.", isHTML: false)
    
    // Add attachment
    if let data = data {
        mailController.addAttachmentData(data, mimeType: "text/comma-separated-values", fileName: "headaches.csv")
        
        presentViewController(mailController, animated: true, completion: nil)
    } else {
        NSLog("No data")
    }
}

 

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.