MFMailComposeViewController
A standard view controller, whose interface lets the user manage, edit, and send email messages.
Declaration
@MainActor class MFMailComposeViewControllerOverview
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 person can edit your initial values before sending the email.
The composition interface doesn’t guarantee the delivery of your email message; it only lets you construct the initial message and present it for user approval. The person may opt to cancel the composition interface which discards the message and its contents. If the person opts to send the message, the message queues in the user’s Mail app outbox. The Mail app is ultimately responsible for sending the message.
[Image]
An alternate way to compose emails is to create and open a URL that uses the mailto scheme. URLs of that type go directly 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 canSendMail() method to see if the person configured the current device to send email. If the person’s device isn’t set up for the delivery of email, you can notify the person or disable the email dispatch features in your application. You shouldn’t attempt to use this interface if the canSendMail() method returns false.
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 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. The sample code below 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.
The mail compose view controller isn’t 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 sample code below. You can also use this method to check the result of the operation.
The user can delete a queued message before it’s sent. Although the view controller reports the success or failure of the operation to its delegate, this class doesn’t provide a way for you to verify if the email sent.
For more information on how to present and dismiss view controllers, see View Controller Programming Guide for iOS.
Topics
Responding to the view controller dismissal
Determining mail availability
Setting mail fields programmatically
setSubject(_:)setToRecipients(_:)setCcRecipients(_:)setBccRecipients(_:)setMessageBody(_:isHTML:)addAttachmentData(_:mimeType:fileName:)setPreferredSendingEmailAddress(_:)