A standard view controller, whose interface lets the user manage, edit, and send email messages.


class MFMailComposeViewController : UINavigationController


Use this view controller to display a standard email interface inside your app. Before presenting the interface, populate the fields with initial values for the subject, email recipients, body text, and attachments of the email. After presenting the interface, the user can edit your initial values before sending the email.

The composition interface does not guarantee the delivery of your email message; it only lets you construct the initial message and present it for user approval. The user may opt to cancel the composition interface, in which case the message and its contents are discarded. If the user opts to send the message, the message is queued in the user’s Mail app outbox. The Mail app is ultimately responsible for sending the message.

Figure 1

The mail composition interface

Screenshot of the email composition view in Mail, indicating the fields for recipients, subject, and body.

An alternate way to compose emails is to create and open a URL that uses the mailto scheme. URLs of that type are directed to the built-in Mail app, which uses your URL to configure a message. For information about the structure of mailto URLs, see Apple URL Scheme Reference.

Checking the Availability of the Composition Interface

Before presenting the mail compose view controller, always call the the canSendMail() method to see if the current device is configured to send email. If the user’s device is not set up for the delivery of email, you can notify the user or simply disable the email dispatch features in your application. You should not attempt to use this interface if the canSendMail() method returns false.

Listing 1

Checking the availability of mail services

if !MFMailComposeViewController.canSendMail() {
    print("Mail services are not available")

Configuring and Displaying the Composition Interface

After verifying that mail services are available, you can create and configure the mail composition view controller and then present it as you would any other view controller. Use the methods of this class to specify the subject, recipients, and message body of the email, including any attachments you want to send with the message. Listing 2 shows how to configure the composition interface and present it modally. Always assign a delegate to the mailComposeDelegate property, because the delegate is responsible for dismissing the composition interface later.

Listing 2

Configuring and presenting the composition interface

let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setMessageBody("Hello from California!", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)

The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController(_:didFinishWith:error:) method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in Listing 3. You can also use this method to check the result of the operation.

Listing 3

Dismissing the mail compose view controller

func mailComposeController(controller: MFMailComposeViewController,
                           didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    // Check the result or perform other tasks.
    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)

The user can delete a queued message before it is sent. Although the view controller reports the success or failure of the operation to its delegate, this class does not provide a way for you to verify whether the email was actually sent.

For more information on how to present and dismiss view controllers, see View Controller Programming Guide for iOS.


Responding to the View Controller Dismissal

var mailComposeDelegate: MFMailComposeViewControllerDelegate?

The mail composition view controller’s delegate.

protocol MFMailComposeViewControllerDelegate

An interface for responding to user interactions with a mail compose view controller.

Determining Mail Availability

class func canSendMail() -> Bool

Returns a Boolean that indicates whether the current device is able to send email.

Setting Mail Fields Programmatically

func setSubject(String)

Sets the initial text for the subject line of the email.

func setToRecipients([String]?)

Sets the initial recipients to include in the email’s “To” field.

func setCcRecipients([String]?)

Sets the initial recipients to include in the email’s “Cc” field.

func setBccRecipients([String]?)

Sets the initial recipients to include in the email’s “Bcc” field.

func setMessageBody(String, isHTML: Bool)

Sets the initial body text to include in the email.

func addAttachmentData(Data, mimeType: String, fileName: String)

Adds the specified data as an attachment to the message.

func setPreferredSendingEmailAddress(String)

Sets the preferred email address to use in the From field, if such an address is available.

Responding to Errors

struct MFMailComposeError

Mail composition errors.

let MFMailComposeErrorDomain: String

The domain used for error objects that are associated with the mail composition interface.

enum MFMailComposeError.Code

Error codes for NSError objects that are associated with the mail composition interface.