Send Email iOS Tutorial

It is possible to compose and send an email from inside an app using the MFMailComposeViewController class. In this tutorial an email is composed with a body and a subject. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use IOS12SendEmailTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

Go to the Storyboard and drag a Text Field to the view. Select the Text Field and go to the Attribute Inspector, in the Text Field section change the placeholder text to "Subject". Next, drag a Text View below the Text Field. Select the Text View and go to the Attribute Inspector. In the Text View section delete the template text.Finally, add a Button to the view and place it below the Text View. Select it and give it s title of "Send Email" . Also, change the background color of the main view to light-gray. The Storyboard will look like this

Select the "Resolve Auto Layout Issues" button at the bottom-right(3rd button) and select Add Missing Constraints.

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Text Field to the ViewController class  and create the following Outlet.

Ctrl and drag from the Text View to the ViewController class  and create the following Outlet.

Ctrl and drag from the Button to the ViewController class  and create the following Action.

Go to the ViewController.swift file, first we need to import the MessageUI framework.

import MessageUI

The View Controller must conform to a couple of delegates change the class definition line into

class ViewController: UIViewController, MFMailComposeViewControllerDelegate, UITextFieldDelegate, UITextViewDelegate {

Change the viewDidLoad method to

override func viewDidLoad() {
    subject.delegate = self
    body.delegate = self

The delegates of the TextField and TextView now points to the View Controller. Next, implement the sendMail method

@IBAction func sendMail(_ sender: Any) {
    let picker = MFMailComposeViewController()
    picker.mailComposeDelegate = self
    if let subjectText = subject.text {
    picker.setMessageBody(body.text, isHTML: true)
    present(picker, animated: true, completion: nil)

The MFMailComposeViewController class provides a standard interface that manages the editing and sending an email message. The View Controller is presented including the subject and body's text. Finally we need to implement some delegate methods.

// MFMailComposeViewControllerDelegate

// 1
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
     dismiss(animated: true, completion: nil)

// UITextFieldDelegate
// 2
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    return true
// UITextViewDelegate
// 3
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    body.text = textView.text
    if text == "\n" {
        return false
    return true
  1. Tells the delegate that the user wants to dismiss the mail composition view.

  2. In the textFieldShouldReturn method we resign The FirstResponder so the control is given back to the ViewController.

  3. When the enter key is pressed. the keyboard will hide.

Build and Run, the project. Enter some subject and body text.

Press the Send Email button. The MFMailCompose ViewController will be displayed containing the subject and body's text.

You can download the source code of the IOS12SendEmailTutorial at the ioscreator repository on Github.