A standard interface for managing, editing, and sending an email message.
SDK
- iOS 3.0+
Framework
- Message
UI
Declaration
class MFMailComposeViewController : UINavigation Controller
Overview
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.
The mail composition interface

Important
You must not modify the view hierarchy presented by this view controller. You can, however, customize the appearance of the interface using the UIAppearance
protocol.
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 can
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 can
method returns false
.
Checking the availability of mail services
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
return
}
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 like 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 mail
property. The delegate is responsible for dismissing the composition interface later.
Configuring and presenting the composition interface
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["address@example.com"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello from California!", isHTML: false)
// Present the view controller modally.
self.presentViewController(composeVC, animated: true, completion: nil)
Important
After presenting a mail compose view controller, the system ignores any attempts to modify the email using the methods of this class. The user can still edit the content of the email, but your app cannot. Therefore, always configure the fields of your email before presenting the view controller.
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 mail
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.
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.dismissViewControllerAnimated(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.