Swift – Send Email From App
Setting up an app to send an email from the user’s phone is simple. You can setup your app to open the mail app and fill out the To, CC, Subject and Mail Body fields with your own custom content. Then the user can send an email from the phone with all this filled out. This is handy if you wanted to have a method for feedback in your app, where by you can fill out the email and suject automatically, neat!
To set this up create a new single view application. Then go to the storyboard and add a “Send Email” button as follows.
Once added open up the assistant editor and connect it up to your ViewController code, create an action called sendEmail. Now to code it, go to the ViewController, in there add import MessageUI under import UIKit, this is used to prompt the user to send an email. Next up add MFMailComposeViewControllerDelegate to your ViewController, this gives us access to a function that is called once the user has either sent or discarded the email.
import MessageUI class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
Before we setup the sendEmail function there are a few functions we need to add that it needs to use. So lets add each one in as follows:
func configureMailController() -> MFMailComposeViewController { let mailComposerVC = MFMailComposeViewController() mailComposerVC.mailComposeDelegate = self mailComposerVC.setToRecipients(["andrew@seemuapps.com"]) mailComposerVC.setSubject("Hello") mailComposerVC.setMessageBody("How are you doing?", isHTML: false) return mailComposerVC }
This one sets up the mail view controller and returns an instance of it. It is here we set the recipiepnts of the email, the subject line, the message body and so on. A quick note on the setMessageBody is that you can set the isHTML flag as true, this will render your input string as HTML. E.g <b>Bold Text</b> as an input would result in Bold Text in an email if this was set to true. In our example we are leaving it set as false.
func showMailError() { let sendMailErrorAlert = UIAlertController(title: "Could not send email", message: "Your device could not send email", preferredStyle: .alert) let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil) sendMailErrorAlert.addAction(dismiss) self.present(sendMailErrorAlert, animated: true, completion: nil) }
This function shows an error to the user if we could not send an email. This could be due to a various range of issues such as:
- The app is running in a simulator which cannot send email’s
- The user has not set up their email account
- The user’s phone is locked down so our app cannot access their email
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { controller.dismiss(animated: true, completion: nil) }
Finally this function is used for the MFMailComposeViewControllerDelegate. Once the user completes the email (Either sending it or cancelling it), this will close down the mail app and return to our app. We can also get the results and if there are any errors from the arguments in this.
@IBAction func sendEmail(_ sender: Any) { let mailComposeViewController = configureMailController() if MFMailComposeViewController.canSendMail() { self.present(mailComposeViewController, animated: true, completion: nil) } else { showMailError() } }
Finally we can complete out sendEmail function. This will setup our mail controller, then if the device can send an email it will show it to the user, otherwise it will show the error to the user.