How to open mail app from Swift
Asked Answered
S

18

183

Im working on a simple swift app where the user inputs an email address and presses a button which opens the mail app, with the entered address in the address bar. I know how to do this in Objective-C, but I'm having trouble getting it to work in Swift.

Schmooze answered 22/9, 2014 at 19:7 Comment(0)
L
322

You can use simple mailto: links in iOS to open the mail app.

let email = "[email protected]"
if let url = URL(string: "mailto:\(email)") {
  if #available(iOS 10.0, *) {
    UIApplication.shared.open(url)
  } else {
    UIApplication.shared.openURL(url)
  }    
}
Layoff answered 23/9, 2014 at 9:12 Comment(5)
Maybe worthwile to add that this does not work in the simulator, only on the device... See #26053315Kerakerala
now You need to add "!" in the second line, for the NSURL NSURL(string: "mailto:(email)")!Standpoint
why does it say this is only available on ios 10 or newer when the answer is clearly 3 yrs oldNobody
Swift 4/iOS 10+ example: UIApplication.shared.open(url, options: [:], completionHandler: nil) Passing an empty dictionary for options produces the same result as calling openURL did.Luteolin
You can also provide subject, body etc. See developer.apple.com/library/archive/featuredarticles/…Evasive
M
199

While other answers are all correct, you can never know if the iPhone/iPad that is running your application has the Apple's Mail app installed or not as it can be deleted by the user.

It is better to support multiple email clients. Following code handles the email sending in a more graceful way. The flow of the code is:

  • If Mail app is installed, open Mail's composer pre-filled with provided data
  • Otherwise, try opening the Gmail app, then Outlook, then Yahoo mail, then Spark, in this order
  • If none of those clients are installed, fallback to default mailto:.. that prompts the user to install Apple's Mail app.

Code is written in Swift 5:

    import MessageUI
    import UIKit

    class SendEmailViewController: UIViewController, MFMailComposeViewControllerDelegate {
        
        @IBAction func sendEmail(_ sender: UIButton) {
            // Modify following variables with your text / recipient
            let recipientEmail = "[email protected]"
            let subject = "Multi client email support"
            let body = "This code supports sending email via multiple different email apps on iOS! :)"
            
            // Show default mail composer
            if MFMailComposeViewController.canSendMail() {
                let mail = MFMailComposeViewController()
                mail.mailComposeDelegate = self
                mail.setToRecipients([recipientEmail])
                mail.setSubject(subject)
                mail.setMessageBody(body, isHTML: false)
                
                present(mail, animated: true)
            
            // Show third party email composer if default Mail app is not present
            } else if let emailUrl = createEmailUrl(to: recipientEmail, subject: subject, body: body) {
                UIApplication.shared.open(emailUrl)
            }
        }
        
        private func createEmailUrl(to: String, subject: String, body: String) -> URL? {
            let subjectEncoded = subject.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
            let bodyEncoded = body.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
            
            let gmailUrl = URL(string: "googlegmail://co?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
            let outlookUrl = URL(string: "ms-outlook://compose?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
            let yahooMail = URL(string: "ymail://mail/compose?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
            let sparkUrl = URL(string: "readdle-spark://compose?recipient=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
            let defaultUrl = URL(string: "mailto:\(to)?subject=\(subjectEncoded)&body=\(bodyEncoded)")
            
            if let gmailUrl = gmailUrl, UIApplication.shared.canOpenURL(gmailUrl) {
                return gmailUrl
            } else if let outlookUrl = outlookUrl, UIApplication.shared.canOpenURL(outlookUrl) {
                return outlookUrl
            } else if let yahooMail = yahooMail, UIApplication.shared.canOpenURL(yahooMail) {
                return yahooMail
            } else if let sparkUrl = sparkUrl, UIApplication.shared.canOpenURL(sparkUrl) {
                return sparkUrl
            }
            
            return defaultUrl
        }
        
        func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            controller.dismiss(animated: true)
        }
    }

You also have to add following code to Info.plist file that whitelists the URl query schemes that are used.

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlegmail</string>
    <string>ms-outlook</string>
    <string>readdle-spark</string>
    <string>ymail</string>
</array>
Morse answered 19/4, 2019 at 17:11 Comment(12)
Well done. This is the most complete answer and is easily extensible for other email client apps. IMHO, I don't think it's acceptable in late 2019 to just tell the person "sorry, you're out of luck" if they don't use the default Apple Mail app, as most other solutions suggest. This fixes that deficiency.Towers
Does this method work with HTML? I cannot get it display properly.Procto
@MatthewBradshaw you can support HTML for the default mail composer by setting isHTML in the above code to true. For other clients, it doesn't appear to be possible, for further reading see https://mcmap.net/q/54185/-mailto-link-with-html-bodyMorse
Thanks, this woks great. I modified it slightly to let user choose client of their preference (I am filtering them in advance with canOpenUrl). Btw body for Microsoft Outlook is working fine :-)Ealasaid
This is brilliant! Has anyone done this for SwiftUI?Normative
I think, it should be .urlQueryAllowed not .urlHostAllowedNation
So in iOS 14, the user can change the default app. But what would I do if I don't want the user to compose a new email, but to just make them check their emails to click on a confirmation link and also use the default app choice. If I go the mailto: route, then the proper default mail application is used, but it shows the compose view which would only be confusing in my case. If I use message: it works nicely for Mail.app but does not invoke the default mail app, unless it is coincidentally Apple Mail.Ellery
openURL() is deprecated btw, use this now: developer.apple.com/documentation/uikit/uiapplication/…Shanelleshaner
ymail does not seem to be working. Has the url scheme changed?Injun
How to add an attachment with openURL?Jaysonjaywalk
Can you give a usage example ?Pashm
I have created Swift package which alleviates apps enumeration and url building (to open an email app) - github.com/alex1704/EmailAppsErdda
A
63

I'm not sure if you want to switch to the mail app itself or just open and send an email. For the latter option linked to a button IBAction:

    import UIKit
    import MessageUI

    class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

    @IBAction func launchEmail(sender: AnyObject) {

    var emailTitle = "Feedback"
    var messageBody = "Feature request or bug report?"
    var toRecipents = ["[email protected]"]
    var mc: MFMailComposeViewController = MFMailComposeViewController()
    mc.mailComposeDelegate = self
    mc.setSubject(emailTitle)
    mc.setMessageBody(messageBody, isHTML: false)
    mc.setToRecipients(toRecipents)

    self.presentViewController(mc, animated: true, completion: nil)
    }

    func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
        switch result {
        case MFMailComposeResultCancelled:
            print("Mail cancelled")
        case MFMailComposeResultSaved:
            print("Mail saved")
        case MFMailComposeResultSent:
            print("Mail sent")
        case MFMailComposeResultFailed:
            print("Mail sent failure: \(error?.localizedDescription)")
        default:
            break
        }
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    }
Amylose answered 22/9, 2014 at 22:58 Comment(4)
I am having issues where the mailComposeController delegate function is not being called.Alive
Add "import MessageUI" to your imports and be sure to add the "MFMailComposeViewControllerDelegate" option to your class declaration like: class myClass: UIViewController, MFMailComposeViewControllerDelegate {Pulsation
MFMailComposeViewController() return nil for meJanelljanella
Also having issues: 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target. App crashes in some devices (iPhone 5, iPhone 6 and iPad Mini)Jacquerie
D
34

For Swift 4.2+ and iOS 9+

let appURL = URL(string: "mailto:[email protected]")!

if #available(iOS 10.0, *) {
    UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(appURL)
}

Replace [email protected] with your desired email address.

You can also include a subject field, a message, and multiple recipients in the To, Cc, and Bcc fields:

mailto:[email protected][email protected]&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!
Dichroite answered 16/7, 2018 at 21:33 Comment(1)
is it possible to predefine the subject of an email this way?Crotty
C
26

In Swift 3 you make sure to add import MessageUI and needs conform to the MFMailComposeViewControllerDelegate protocol.

func sendEmail() {
  if MFMailComposeViewController.canSendMail() {
    let mail = MFMailComposeViewController()
    mail.mailComposeDelegate = self
    mail.setToRecipients(["[email protected]"])
    mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)

    present(mail, animated: true)
  } else {
    // show failure alert
  }
}

Protocol:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
  controller.dismiss(animated: true)
}
Caras answered 15/12, 2016 at 9:20 Comment(0)
C
18

Swift 2, with availability check:

import MessageUI

if MFMailComposeViewController.canSendMail() {
    let mail = MFMailComposeViewController()
    mail.mailComposeDelegate = self
    mail.setToRecipients(["[email protected]"])
    mail.setSubject("Bla")
    mail.setMessageBody("<b>Blabla</b>", isHTML: true)
    presentViewController(mail, animated: true, completion: nil)
} else {
    print("Cannot send mail")
    // give feedback to the user
}


// MARK: - MFMailComposeViewControllerDelegate

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    switch result.rawValue {
    case MFMailComposeResultCancelled.rawValue:
        print("Cancelled")
    case MFMailComposeResultSaved.rawValue:
        print("Saved")
    case MFMailComposeResultSent.rawValue:
        print("Sent")
    case MFMailComposeResultFailed.rawValue:
        print("Error: \(error?.localizedDescription)")
    default:
        break
    }
    controller.dismissViewControllerAnimated(true, completion: nil)
}
Coriolanus answered 12/1, 2016 at 1:50 Comment(0)
L
17

Here how it looks for Swift 4:

import MessageUI

if MFMailComposeViewController.canSendMail() {
    let mail = MFMailComposeViewController()
    mail.mailComposeDelegate = self
    mail.setToRecipients(["[email protected]"])
    mail.setSubject("Bla")
    mail.setMessageBody("<b>Blabla</b>", isHTML: true)
    present(mail, animated: true, completion: nil)
} else {
    print("Cannot send mail")
    // give feedback to the user
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        switch result.rawValue {
        case MFMailComposeResult.cancelled.rawValue:
            print("Cancelled")
        case MFMailComposeResult.saved.rawValue:
            print("Saved")
        case MFMailComposeResult.sent.rawValue:
            print("Sent")
        case MFMailComposeResult.failed.rawValue:
            print("Error: \(String(describing: error?.localizedDescription))")
        default:
            break
        }
        controller.dismiss(animated: true, completion: nil)
    }
Lightness answered 5/11, 2017 at 7:51 Comment(0)
C
15

Here's an update for Swift 4 if you're simply looking to open up the mail client via a URL:

let email = "[email protected]"
if let url = URL(string: "mailto:\(email)") {
   UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

This worked perfectly fine for me :)

Christenechristening answered 27/12, 2017 at 12:6 Comment(0)
R
14

Updated answer from Stephen Groom for Swift 3

let email = "[email protected]"
let url = URL(string: "mailto:\(email)")
UIApplication.shared.openURL(url!)
Rameriz answered 16/11, 2016 at 14:28 Comment(0)
Z
10

This is a straight forward solution of 3 steps in Swift.

import MessageUI

Add to conform the Delegate

MFMailComposeViewControllerDelegate

And just create your method:

    func sendEmail() {
    if MFMailComposeViewController.canSendMail() {
        let mail = MFMailComposeViewController()
        mail.mailComposeDelegate = self
        mail.setToRecipients(["[email protected]"])
        mail.setSubject("Support App")
        mail.setMessageBody("<p>Send us your issue!</p>", isHTML: true)
        presentViewController(mail, animated: true, completion: nil)
    } else {
        // show failure alert
    }
}

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    controller.dismissViewControllerAnimated(true, completion: nil)
}
Zeniazenith answered 7/9, 2016 at 15:48 Comment(0)
C
9

You should try sending with built-in mail composer, and if that fails, try with share:

func contactUs() {

    let email = "[email protected]" // insert your email here
    let subject = "your subject goes here"
    let bodyText = "your body text goes here"

    // https://developer.apple.com/documentation/messageui/mfmailcomposeviewcontroller
    if MFMailComposeViewController.canSendMail() {

        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self as? MFMailComposeViewControllerDelegate

        mailComposerVC.setToRecipients([email])
        mailComposerVC.setSubject(subject)
        mailComposerVC.setMessageBody(bodyText, isHTML: false)

        self.present(mailComposerVC, animated: true, completion: nil)

    } else {
        print("Device not configured to send emails, trying with share ...")

        let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        if let emailURL = URL(string: coded!) {
            if #available(iOS 10.0, *) {
                if UIApplication.shared.canOpenURL(emailURL) {
                    UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
                        if !result {
                            print("Unable to send email.")
                        }
                    })
                }
            }
            else {
                UIApplication.shared.openURL(emailURL as URL)
            }
        }
    }
}
Collocutor answered 19/1, 2019 at 12:29 Comment(2)
error: "This app is not allowed to query for scheme mailto"Smuggle
@KhushaliOS use real device instead simulatorStupefacient
I
9

For Swift 4.2 and above

let supportEmail = "[email protected]"
if let emailURL = URL(string: "mailto:\(supportEmail)"), UIApplication.shared.canOpenURL(emailURL)
{
    UIApplication.shared.open(emailURL, options: [:], completionHandler: nil)
}

Give the user to choose many mail options(like iCloud, google, yahoo, Outlook.com - if no mail is pre-configured in his phone) to send email.

Involucre answered 16/5, 2019 at 9:28 Comment(2)
In my case, with iOS 13, when calling UIApplication.shared.open, the OS would always show a dialog offering to install Mail.app (oh, and canOpenURL for "mailto" is always true, too), even if there are other mail apps. So this is definitely not working out.Egin
still works for iOS 16, swift 5+Transformer
Y
6

In the view controller from where you want your mail-app to open on the tap.

  • At the top of the file do, import MessageUI.
  • Put this function inside your Controller.

    func showMailComposer(){
    
      guard MFMailComposeViewController.canSendMail() else {
           return
      }
      let composer = MFMailComposeViewController()
      composer.mailComposeDelegate = self
      composer.setToRecipients(["[email protected]"]) // email id of the recipient
      composer.setSubject("testing!!!")
      composer.setMessageBody("this is a test mail.", isHTML: false)
      present(composer, animated: true, completion: nil)
     }
    
  • Extend your View Controller and conform to the MFMailComposeViewControllerDelegate.

  • Put this method and handle the failure, sending of your mails.

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
      if let _ = error {
          controller.dismiss(animated: true, completion: nil)
          return
      }
      controller.dismiss(animated: true, completion: nil)
    }
    
Yellowknife answered 5/11, 2019 at 20:32 Comment(0)
M
5
@IBAction func launchEmail(sender: AnyObject) {
 if if MFMailComposeViewController.canSendMail() {
   var emailTitle = "Feedback"
   var messageBody = "Feature request or bug report?"
   var toRecipents = ["[email protected]"]
   var mc: MFMailComposeViewController = MFMailComposeViewController()
   mc.mailComposeDelegate = self
   mc.setSubject(emailTitle)
   mc.setMessageBody(messageBody, isHTML: false)
   mc.setToRecipients(toRecipents)

   self.present(mc, animated: true, completion: nil)
 } else {
   // show failure alert
 }
}

func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
    switch result {
    case .cancelled:
        print("Mail cancelled")
    case .saved:
        print("Mail saved")
    case .sent:
        print("Mail sent")
    case .failed:
        print("Mail sent failure: \(error?.localizedDescription)")
    default:
        break
    }
    self.dismiss(animated: true, completion: nil)
}

Note that not all users have their device configure to send emails, which is why we need to check the result of canSendMail() before trying to send. Note also that you need to catch the didFinishWith callback in order to dismiss the mail window.

Mantle answered 5/6, 2017 at 14:8 Comment(0)
R
2

In my case I was just trying to open the mail app, without creating an email draft.

In case someone is stumbling upon this question and is actually trying to do the same thing here is the code I used:

private var openMailAppButton: some View { 
        Button {
            if let emailUrl = mailAppUrl() {
                UIApplication.shared.open(emailUrl)
            }
        } label: {
            Text("OPEN MAIL APP")
        }
    }

private func mailAppUrl() -> URL? {
        let gmailUrl = URL(string: "googlegmail://")
        let outlookUrl = URL(string: "ms-outlook://")
        let yahooMail = URL(string: "ymail://")
        let sparkUrl = URL(string: "readdle-spark://")
        let defaultUrl = URL(string: "message://")
        
        if let defaultUrl = defaultUrl, UIApplication.shared.canOpenURL(defaultUrl) {
            return defaultUrl
        } else if let gmailUrl = gmailUrl, UIApplication.shared.canOpenURL(gmailUrl) {
            return gmailUrl
        } else if let outlookUrl = outlookUrl, UIApplication.shared.canOpenURL(outlookUrl) {
            return outlookUrl
        } else if let yahooMail = yahooMail, UIApplication.shared.canOpenURL(yahooMail) {
            return yahooMail
        } else if let sparkUrl = sparkUrl, UIApplication.shared.canOpenURL(sparkUrl) {
            return sparkUrl
        }
        
        return defaultUrl
    }
Reparation answered 6/7, 2022 at 20:44 Comment(0)
C
1

For those of us still lagging behind on Swift 2.3 here is Gordon's answer in our syntax:

let email = "[email protected]"
if let url = NSURL(string: "mailto:\(email)") {
   UIApplication.sharedApplication().openURL(url)
}
Cowling answered 3/4, 2017 at 18:10 Comment(0)
D
-1

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult,error: Swift.Error?) {

    controller.dismiss(animated: true, completion: nil)
}
Deth answered 14/9, 2022 at 12:36 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Tanishatanitansy
T
-2

all answer is great but for me i like this more

extension ContentUsVC : MFMailComposeViewControllerDelegate {
    func sendEmail(emile:String) {
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients([emile])
            present(mail, animated: true)
        } else {
            UIPasteboard.general.string = emile
            self.view.makeToast(StaticString.copiedToClipboard[languageString],position: .top)
        }
        
    }
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
}
Thumbprint answered 12/3, 2023 at 18:59 Comment(3)
self.view.makeToast(StaticString.copiedToClipboard[languageString],position: .top) this from library Toast_Swift but you can build with yourself or show for user you copied the textThumbprint
Whenever possible, if the original asker does not ask for a library to do so and it is easy to perform an action without a library, I would not reccomend including an extra library. Also, if you must do so, please include the fact that you are using that library in your post, not in a comment below.Vacation
actually makeToast it is not from library this is a func that i built with my self and show a text with background on viewThumbprint

© 2022 - 2024 — McMap. All rights reserved.