Send Email Tutorial in iOS8 with Swift

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 tuturial is built in iOS 8.1 and Xcode 6.1.

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftEmailTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

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 all items by holding the Ctrl-key  and 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: AnyObject) {
    var picker = MFMailComposeViewController()
    picker.mailComposeDelegate = self
    picker.setMessageBody(body.text, isHTML: true)
    presentViewController(picker, animated: true, completion: nil)

The MFMailComposeViewController class provides a standard interface that manages the editing and sending an email message. Here 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!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
    dismissViewControllerAnimated(true, completion: nil)
// UITextFieldDelegate
// 2
func textFieldShouldReturn(textField: UITextField) -> Bool {
    return true
// UITextViewDelegate
// 3
func textView(textView: UITextView, shouldChangeTextInRange 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 IOS8SwiftEmailTutorial at the ioscreator repository on Github.