title: SwiftUI: Send email using MFMailComposeViewController
body: <p>I am currently trying to implement a &quot;send email&quot; button in my SwiftUI app, using SwiftUI lifecycle and targeting iOS 14.</p>
  <p>I know there are quite some solutions presented online - here on stack overflow and elsewhere. However, I have-not been able to make anything work so far in simulator/on device.</p>
  <p>My current solution looks like this (based on this <a href="https://stackoverflow.com/a/65562703/14934149">question on stackoverflow</a>:</p>
  <pre><code>import SwiftUI
  import MessageUI
  import Foundation
  
  struct ContentView: View {
  
      class MailComposeViewController: UIViewController, MFMailComposeViewControllerDelegate {
      
          static let shared = MailComposeViewController()
      
          func sendEmail() {
              if MFMailComposeViewController.canSendMail() {
                  let mail = MFMailComposeViewController()
                  mail.mailComposeDelegate = self
                  mail.setToRecipients([&quot;test@test.com&quot;])
  
                  UIApplication.shared.windows.last?.rootViewController?.present(mail, animated: true, completion: nil)
              } else {
                  // Alert
              }
          }
      
          func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
              controller.dismiss(animated: true, completion: nil)
          }
      }
      
      var body: some View {
          Button(action: {
              MailComposeViewController.shared.sendEmail()
          }, label: {
              Text(&quot;Send&quot;)
          })
      }
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  </code></pre>
  <p>The simulator does show the button and doesn't give me any errors. However, upon clicking the button, nothing happens at all - same thing when testing on device.</p>
  <p>Any idea what might be wrong here?</p>
  <p>Thanks!</p>