42

In a normal UIViewController in Swift, I use this code to send a mail.

let mailComposeViewController = configuredMailComposeViewController()

mailComposeViewController.navigationItem.leftBarButtonItem?.style = .plain
mailComposeViewController.navigationItem.rightBarButtonItem?.style = .plain
mailComposeViewController.navigationBar.tintColor = UIColor.white

if MFMailComposeViewController.canSendMail() {
    self.present(mailComposeViewController, animated: true, completion: nil)
} else {
    self.showSendMailErrorAlert()
}

How can I achieve the same in SwiftUI?

Do I need to use UIViewControllerRepresentable?

Matteo Pacini
  • 17,507
  • 3
  • 58
  • 70
Khant Thu Linn
  • 5,035
  • 5
  • 42
  • 111

8 Answers8

63

@Matteo's answer is good but it needs to use the presentation environment variable. I have updated it here and it addresses all of the concerns in the comments.

import SwiftUI
import UIKit
import MessageUI

struct MailView: UIViewControllerRepresentable {

    @Environment(\.presentationMode) var presentation
    @Binding var result: Result<MFMailComposeResult, Error>?

    class Coordinator: NSObject, MFMailComposeViewControllerDelegate {

        @Binding var presentation: PresentationMode
        @Binding var result: Result<MFMailComposeResult, Error>?

        init(presentation: Binding<PresentationMode>,
             result: Binding<Result<MFMailComposeResult, Error>?>) {
            _presentation = presentation
            _result = result
        }

        func mailComposeController(_ controller: MFMailComposeViewController,
                                   didFinishWith result: MFMailComposeResult,
                                   error: Error?) {
            defer {
                $presentation.wrappedValue.dismiss()
            }
            guard error == nil else {
                self.result = .failure(error!)
                return
            }
            self.result = .success(result)
        }
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator(presentation: presentation,
                           result: $result)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<MailView>) -> MFMailComposeViewController {
        let vc = MFMailComposeViewController()
        vc.mailComposeDelegate = context.coordinator
        return vc
    }

    func updateUIViewController(_ uiViewController: MFMailComposeViewController,
                                context: UIViewControllerRepresentableContext<MailView>) {

    }
}

Usage:

import SwiftUI
import MessageUI

struct ContentView: View {

   @State var result: Result<MFMailComposeResult, Error>? = nil
   @State var isShowingMailView = false

    var body: some View {
        Button(action: {
            self.isShowingMailView.toggle()
        }) {
            Text("Tap Me")
        }
        .disabled(!MFMailComposeViewController.canSendMail())
        .sheet(isPresented: $isShowingMailView) {
            MailView(result: self.$result)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Hobbes the Tige
  • 3,375
  • 2
  • 20
  • 21
  • 2
    THANK YOU for tying everything together so nicely -- it works well! If one wants to pre-populate a To: and CC: address, along with a subject line, body text, and some attached files, where do those parameters go in the code above, please? – ConfusionTowers Dec 22 '19 at 23:32
  • 1
    @ConfusionTowers Right after `let vc = MFMailComposeViewController()` is where you do any of your accustomed configuration. – Alex Curylo Dec 26 '19 at 21:46
  • `.disabled(!MFMailComposeViewController.canSendMail())` isn't working. This was causing crashes on my app when people don't have mail set up. Otherwise, good solution. – sfung3 Apr 19 '20 at 02:36
  • Worth a comment that I spent a few hours attempting to make this solution work. (As of SwiftUI 2.0 / Xcode 12) the accepted answer was the solution that works in my particular case - using `@Binding var isShowing: Bool` and not `@Environment(\.presentationMode) var presentation`. – andrewbuilder Aug 23 '20 at 15:50
  • `vc.setSubject("foo")` doesn't seem to work. Any ideas? – fankibiber Sep 18 '20 at 22:18
  • 1
    If anyone has the problem that the Save draft/Delete draft popup appears laggy and the keyboard hides laggy it helped for me to add .edgesIgnoringSafeArea(.bottom) to the MailView sheet to resolve this problem. – sp4c38 Oct 27 '20 at 14:01
  • It works but does have a glitch as when data are populated to the user and we can not swipe down to dismiss the view like the UIKit implementation does. – Roland Lariotte Dec 22 '20 at 13:46
56

As you mentioned, you need to port the component to SwiftUI via UIViewControllerRepresentable.

Here's a simple implementation:

struct MailView: UIViewControllerRepresentable {

    @Binding var isShowing: Bool
    @Binding var result: Result<MFMailComposeResult, Error>?

    class Coordinator: NSObject, MFMailComposeViewControllerDelegate {

        @Binding var isShowing: Bool
        @Binding var result: Result<MFMailComposeResult, Error>?

        init(isShowing: Binding<Bool>,
             result: Binding<Result<MFMailComposeResult, Error>?>) {
            _isShowing = isShowing
            _result = result
        }

        func mailComposeController(_ controller: MFMailComposeViewController,
                                   didFinishWith result: MFMailComposeResult,
                                   error: Error?) {
            defer {
                isShowing = false
            }
            guard error == nil else {
                self.result = .failure(error!)
                return
            }
            self.result = .success(result)
        }
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator(isShowing: $isShowing,
                           result: $result)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<MailView>) -> MFMailComposeViewController {
        let vc = MFMailComposeViewController()
        vc.mailComposeDelegate = context.coordinator
        return vc
    }

    func updateUIViewController(_ uiViewController: MFMailComposeViewController,
                                context: UIViewControllerRepresentableContext<MailView>) {

    }
}

Usage:

struct ContentView: View {

    @State var result: Result<MFMailComposeResult, Error>? = nil
    @State var isShowingMailView = false

    var body: some View {

        VStack {
            if MFMailComposeViewController.canSendMail() {
                Button("Show mail view") {
                    self.isShowingMailView.toggle()
                }
            } else {
                Text("Can't send emails from this device")
            }
            if result != nil {
                Text("Result: \(String(describing: result))")
                    .lineLimit(nil)
            }
        }
        .sheet(isPresented: $isShowingMailView) {
            MailView(isShowing: self.$isShowingMailView, result: self.$result)
        }

    }

}

(Tested on iPhone 7 Plus running iOS 13 - works like a charm)

Updated for Xcode 11.4

Ralf Ebert
  • 1,802
  • 16
  • 26
Matteo Pacini
  • 17,507
  • 3
  • 58
  • 70
  • 1
    wow..it look so different from normal iOS app development with swift. Thanks. it is working – Khant Thu Linn Jun 27 '19 at 09:02
  • But if you try twice, it doesn't work. It seems to have a memory leak. – Florent Morin Jun 29 '19 at 20:58
  • @FlorentMorin `result` is non-nil after the first call, hence it won't display again - see `self.isShowingMailView && result == nil` – Matteo Pacini Jun 29 '19 at 21:10
  • 1
    OK. I suggest an alternative here, without host mail controller by SwiftUI... https://gist.github.com/florentmorin/4be7ca70c973c29cbeebbed4e2ef20ba – Florent Morin Jun 29 '19 at 22:02
  • I updated my answer to include a basic transition, so it looks like the view controller is being presented from the bottom. – Matteo Pacini Jun 30 '19 at 04:45
  • Any update on how to present this in a proper modal aka sheet? I still can only present it once. The ZStack solution is even glitchier and looks pretty bad, because it doesn't stack the view in the iOS 13 modal stack. – hoshy Sep 12 '19 at 16:46
  • Please see my answer below. I modified the code above to work with presentation environment variable. – Hobbes the Tige Nov 04 '19 at 12:08
  • 1
    If anyone has the problem that the Save draft/Delete draft popup appears laggy and the keyboard hides laggy it helped for me to add `.edgesIgnoringSafeArea(.bottom)` to the MailView sheet to resolve this problem. – sp4c38 Oct 27 '20 at 14:00
  • It works but does have a glitch as when data are populated to the user and we can not swipe down to dismiss the view like the UIKit implementation does. – Roland Lariotte Dec 22 '20 at 13:47
  • This is still pretty buggy, the message window randomly turns black. I see similar issues with MFMessageComposeViewController – bze12 Jan 11 '21 at 02:55
  • The dark mode does not wok properly and the cancel Button is invisible (white-in-white) – iKK May 12 '21 at 07:36
23

Answers are correct Hobbes the Tige & Matteo

From the comments, if you need to show an alert if no email is set up on the button or tap gesture

@State var isShowingMailView = false
@State var alertNoMail = false
@State var result: Result<MFMailComposeResult, Error>? = nil

HStack {
                Image(systemName: "envelope.circle").imageScale(.large)
                Text("Contact")
            }.onTapGesture {
                MFMailComposeViewController.canSendMail() ? self.isShowingMailView.toggle() : self.alertNoMail.toggle()
            }
                //            .disabled(!MFMailComposeViewController.canSendMail())
                .sheet(isPresented: $isShowingMailView) {
                    MailView(result: self.$result)
            }
            .alert(isPresented: self.$alertNoMail) {
                Alert(title: Text("NO MAIL SETUP"))
            }

To pre-populate To, Body ... also I add system sound same as Apple email sending sound

Parameters: recipients & messageBody can be injected when you init. MailView

import AVFoundation
import Foundation
import MessageUI
import SwiftUI
import UIKit

struct MailView: UIViewControllerRepresentable {
    @Environment(\.presentationMode) var presentation
    @Binding var result: Result<MFMailComposeResult, Error>?
    var recipients = [String]()
    var messageBody = ""

    class Coordinator: NSObject, MFMailComposeViewControllerDelegate {
        @Binding var presentation: PresentationMode
        @Binding var result: Result<MFMailComposeResult, Error>?

        init(presentation: Binding<PresentationMode>,
             result: Binding<Result<MFMailComposeResult, Error>?>)
        {
            _presentation = presentation
            _result = result
        }

        func mailComposeController(_: MFMailComposeViewController,
                                   didFinishWith result: MFMailComposeResult,
                                   error: Error?)
        {
            defer {
                $presentation.wrappedValue.dismiss()
            }
            guard error == nil else {
                self.result = .failure(error!)
                return
            }
            self.result = .success(result)
            
            if result == .sent {
            AudioServicesPlayAlertSound(SystemSoundID(1001))
            }
        }
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator(presentation: presentation,
                           result: $result)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<MailView>) -> MFMailComposeViewController {
        let vc = MFMailComposeViewController()
        vc.setToRecipients(recipients)
        vc.setMessageBody(messageBody, isHTML: true)
        vc.mailComposeDelegate = context.coordinator
        return vc
    }

    func updateUIViewController(_: MFMailComposeViewController,
                                context _: UIViewControllerRepresentableContext<MailView>) {}
}
zdravko zdravkin
  • 1,032
  • 11
  • 11
7

Well, I have an old code that I used in SwiftUI in this way. The static function belongs to this class basically stays in my Utilities.swift file. But for demonstration purposes, I moved that in here.

Also to retain the delegate and works correctly, I have used this one as a singleton pattern.

Step 1: Create an Email Helper class

import Foundation
import MessageUI

class EmailHelper: NSObject, MFMailComposeViewControllerDelegate {
    public static let shared = EmailHelper()
    private override init() {
        //
    }
    
    func sendEmail(subject:String, body:String, to:String){
        if !MFMailComposeViewController.canSendMail() {
            // Utilities.showErrorBanner(title: "No mail account found", subtitle: "Please setup a mail account")
            return //EXIT
        }
        
        let picker = MFMailComposeViewController()
        
        picker.setSubject(subject)
        picker.setMessageBody(body, isHTML: true)
        picker.setToRecipients([to])
        picker.mailComposeDelegate = self
        
        EmailHelper.getRootViewController()?.present(picker, animated: true, completion: nil)
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        EmailHelper.getRootViewController()?.dismiss(animated: true, completion: nil)
    }
    
    static func getRootViewController() -> UIViewController? {
        (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController

         // OR If you use SwiftUI 2.0 based WindowGroup try this one
         // UIApplication.shared.windows.first?.rootViewController
    }
}

Step 2: Just call this way in SwiftUI class

Button(action: {
   EmailHelper.shared.sendEmail(subject: "Anything...", body: "", to: "")
 }) {
     Text("Send Email")
 }

I am using this is in my SwiftUI based project.

Mahmud Ahsan
  • 1,313
  • 14
  • 15
  • Wow this is the best. So simple to use. Worked perfectly. Just remove Utilities.showErrorBanner. – thegathering Oct 28 '20 at 12:18
  • Unfortunately, this solution crashes the app when you attempt to open the mail app twice in a row. This error is shown ' [PPT] Error creating the CFMessagePort needed to communicate with PPT' – Roland Lariotte Dec 21 '20 at 22:33
5

Yeeee @Hobbes the Tige answer is good but...

Let's make it even better! What if user doesn't have Mail app (like I don't). You can handle it by trying out other mail apps.

if MFMailComposeViewController.canSendMail() {
   self.showMailView.toggle()
} else if let emailUrl = Utils.createEmailUrl(subject: "Yo, sup?", body: "hot dog") {
   UIApplication.shared.open(emailUrl)
} else {
   self.alertNoMail.toggle()
}

createEmailUrl

static func createEmailUrl(subject: String, body: String) -> URL? {
        let to = YOUR_EMAIL
        let subjectEncoded = subject.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
        let bodyEncoded = body.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!

        let gmailUrl = URL(string: "googlegmail://co?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
        let outlookUrl = URL(string: "ms-outlook://compose?to=\(to)&subject=\(subjectEncoded)")
        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
    }

Info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlegmail</string>
    <string>ms-outlook</string>
    <string>readdle-spark</string>
    <string>ymail</string>
</array>
Stephen Lee
  • 209
  • 3
  • 10
  • Nice addition! You are correct not everyone uses the Apple Mail app! What's interesting is that I do not have the Apple Mail app installed yet the MFMailComposeViewController.canSendMail() still returns true. – Dom Sep 08 '20 at 19:14
  • Hmm, weird. If you deleted the mail app, it shouldn't have returned true. – Stephen Lee Sep 08 '20 at 21:55
  • 4
    this is a complete copy of https://stackoverflow.com/a/55765362/6898849. please leave a link to another answer if you refer to it – ramzesenok Sep 14 '20 at 20:49
5

I also improved @Hobbes answer to easily configure parameters like, subject, recipients.

Checkout this gist

Even too lazy to checkout gist, then what about a SPM?

You can now easily copy paste this gift across different projects.

Usage;

import SwiftUI
import MessagesUI
// import SwiftUIEKtensions // via SPM

@State private var result: Result<MFMailComposeResult, Error>? = nil
@State private var isShowingMailView = false

var body: some View {
    Form {
        Button(action: {
            if MFMailComposeViewController.canSendMail() {
                self.isShowingMailView.toggle()
            } else {
                print("Can't send emails from this device")
            }
            if result != nil {
                print("Result: \(String(describing: result))")
            }
        }) {
            HStack {
                Image(systemName: "envelope")
                Text("Contact Us")
            }
        }
        // .disabled(!MFMailComposeViewController.canSendMail())
    }
    .sheet(isPresented: $isShowingMailView) {
        MailView(result: $result) { composer in
            composer.setSubject("Secret")
            composer.setToRecipients(["fancy@mail.com"])
        }
    }
}
Enes Karaosman
  • 1,011
  • 14
  • 20
3

I upgraded and simplified @Mahmud Assan's answer for the new SwiftUI Lifecycle.

import Foundation
import MessageUI

class EmailService: NSObject, MFMailComposeViewControllerDelegate {
public static let shared = EmailService()

func sendEmail(subject:String, body:String, to:String, completion: @escaping (Bool) -> Void){
 if MFMailComposeViewController.canSendMail(){
    let picker = MFMailComposeViewController()
    picker.setSubject(subject)
    picker.setMessageBody(body, isHTML: true)
    picker.setToRecipients([to])
    picker.mailComposeDelegate = self
    
   UIApplication.shared.windows.first?.rootViewController?.present(picker,  animated: true, completion: nil)
}
  completion(MFMailComposeViewController.canSendMail())
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true, completion: nil)
     }
}

Usage:

Button(action: {
            EmailService.shared.sendEmail(subject: "hello", body: "this is body", to: "asd@gmail.com") { (isWorked) in
                if !isWorked{ //if mail couldn't be presented
                    // do action
                }
            }
        }, label: {
            Text("Send Email")
        })
batuhankrbb
  • 112
  • 5
2

I've created a github repository for it. just add it to your project and use it like this:

struct ContentView: View {

@State var showMailSheet = false

var body: some View {
    NavigationView {
        Button(action: {
            self.showMailSheet.toggle()
        }) {
            Text("compose")
        }
    }
    .sheet(isPresented: self.$showMailSheet) {
        MailView(isShowing: self.$showMailSheet,
                 resultHandler: {
                    value in
                    switch value {
                    case .success(let result):
                        switch result {
                        case .cancelled:
                            print("cancelled")
                        case .failed:
                            print("failed")
                        case .saved:
                            print("saved")
                        default:
                            print("sent")
                        }
                    case .failure(let error):
                        print("error: \(error.localizedDescription)")
                    }
        },
                 subject: "test Subjet",
                 toRecipients: ["recipient@test.com"],
                 ccRecipients: ["cc@test.com"],
                 bccRecipients: ["bcc@test.com"],
                 messageBody: "works like a charm!",
                 isHtml: false)
        .safe()
        
    }

  }
}

safe() modifier checks if MFMailComposeViewController.canSendMail() is false, it automatically dismesses the modal and tries to open a mailto link.

Mohammad Rahchamani
  • 4,406
  • 1
  • 22
  • 35