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 Reply

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