In swift 3, you can use this clean code:
@IBAction func sendMail(_ sender: Any) { print(MFMailComposeViewController.canSendMail()) if MFMailComposeViewController.canSendMail() { let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients([" test@test.com "]) mail.setMessageBody("<p>This is test Mail!</p>", isHTML: true) present(mail, animated: true) } else { let email = " test@test.com " if let url = URL(string: "mailto:\(email)") { UIApplication.shared.open(url) } } } func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { controller.dismiss(animated: true) switch result { case .cancelled: print("Mail cancelled") case .saved: print("Mail saved") case .sent: self.allertInfo(_title: "Mail Info", _message: "Mail is sent successfuly", _actionTitle: "OK") print("Mail sent") case .failed: self.allertInfo(_title: "Mail Info", _message: "Mail isn't sent.", _actionTitle: "OK") print("Mail sent failure: \(error?.localizedDescription)") default: break } } func allertInfo(_title:String, _message:String, _actionTitle:String) { let alert = UIAlertController(title: _title, message: _message, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: _actionTitle, style: UIAlertActionStyle.default, handler: nil)) self.present(alert, animated: true, completion: nil) }
source share