Open eMail from App with predefined text in iOS












6















Hello i want to open the eMail program from my App and the body should already be defined. I can open the eMail but don't know how to define the body of the eMail as a given Parameter to show a given standard text. Anyone can help? Heres the code i use to open Email:



//EMAIL
let email = "foo@bar.com"
let urlEMail = NSURL(string: "mailto:(email)")

if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}









share|improve this question

























  • Do some research on the mailto: URL scheme. You can provide "to" addresses, "cc" addresses, a subject, and the message body. But of course the best option is to do what the answers below suggest.

    – rmaddy
    Nov 13 '15 at 14:53
















6















Hello i want to open the eMail program from my App and the body should already be defined. I can open the eMail but don't know how to define the body of the eMail as a given Parameter to show a given standard text. Anyone can help? Heres the code i use to open Email:



//EMAIL
let email = "foo@bar.com"
let urlEMail = NSURL(string: "mailto:(email)")

if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}









share|improve this question

























  • Do some research on the mailto: URL scheme. You can provide "to" addresses, "cc" addresses, a subject, and the message body. But of course the best option is to do what the answers below suggest.

    – rmaddy
    Nov 13 '15 at 14:53














6












6








6


1






Hello i want to open the eMail program from my App and the body should already be defined. I can open the eMail but don't know how to define the body of the eMail as a given Parameter to show a given standard text. Anyone can help? Heres the code i use to open Email:



//EMAIL
let email = "foo@bar.com"
let urlEMail = NSURL(string: "mailto:(email)")

if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}









share|improve this question
















Hello i want to open the eMail program from my App and the body should already be defined. I can open the eMail but don't know how to define the body of the eMail as a given Parameter to show a given standard text. Anyone can help? Heres the code i use to open Email:



//EMAIL
let email = "foo@bar.com"
let urlEMail = NSURL(string: "mailto:(email)")

if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}






ios swift email url-scheme






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '15 at 16:45









Stefan

3,65981942




3,65981942










asked Nov 13 '15 at 14:15









JanScottJanScott

102110




102110













  • Do some research on the mailto: URL scheme. You can provide "to" addresses, "cc" addresses, a subject, and the message body. But of course the best option is to do what the answers below suggest.

    – rmaddy
    Nov 13 '15 at 14:53



















  • Do some research on the mailto: URL scheme. You can provide "to" addresses, "cc" addresses, a subject, and the message body. But of course the best option is to do what the answers below suggest.

    – rmaddy
    Nov 13 '15 at 14:53

















Do some research on the mailto: URL scheme. You can provide "to" addresses, "cc" addresses, a subject, and the message body. But of course the best option is to do what the answers below suggest.

– rmaddy
Nov 13 '15 at 14:53





Do some research on the mailto: URL scheme. You can provide "to" addresses, "cc" addresses, a subject, and the message body. But of course the best option is to do what the answers below suggest.

– rmaddy
Nov 13 '15 at 14:53












6 Answers
6






active

oldest

votes


















13














You can do it using MFMailComposeViewController:



import MessageUI

let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["email@email.com"])
mailComposerVC.setSubject("Subject")
mailComposerVC.setMessageBody("Body", isHTML: false)
self.presentViewController(mailComposerVC, animated: true, completion: nil)


Also, you need to implement mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate where you should dismiss MFMailComposeViewController






share|improve this answer





















  • 5





    Before you create your mailComposerVC it is not a bad idea to check if you actually can send an email using MFMailComposeViewController.canSendMail()

    – Au Ris
    May 2 '17 at 13:18



















18














If you'd like to open the built-in email app as opposed to showing the MFMailComposeViewController as has been mentioned by others, you could construct a mailto: link like this:



let subject = "My subject"
let body = "The awesome body of my email."
let encodedParams = "subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
let url = "mailto:foo@bar.com?(encodedParams)"

if let emailURL = NSURL(url) {
if UIApplication.sharedApplication().canOpenURL(emailURL) {
UIApplication.sharedApplication().openURL(emailURL)
}
}




Just to save anyone typing, for 2016 the syntax has changed slightly:



let subject = "Some subject"
let body = "Plenty of email body."
let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

if let emailURL:NSURL = NSURL(string: coded!)
{
if UIApplication.sharedApplication().canOpenURL(emailURL)
{
UIApplication.sharedApplication().openURL(emailURL)
}





share|improve this answer


























  • the actual correct answer to the question on the page! thanks man! :)

    – Fattie
    Jul 27 '16 at 18:36



















13














Swift 3 Version



let subject = "Some subject"
let body = "Plenty of email body."
let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

if let emailURL: NSURL = NSURL(string: coded!) {
if UIApplication.shared.canOpenURL(emailURL as URL) {
UIApplication.shared.openURL(emailURL as URL)
}
}





share|improve this answer

































    5














    Swift 4.0



        let email = "feedback@company.com"
    let subject = "subject"
    let bodyText = "Please provide information that will help us to serve you better"
    if MFMailComposeViewController.canSendMail() {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self
    mailComposerVC.setToRecipients([email])
    mailComposerVC.setSubject(subject)
    mailComposerVC.setMessageBody(bodyText, isHTML: true)
    self.present(mailComposerVC, animated: true, completion: nil)
    } else {
    let coded = "mailto:(email)?subject=(subject)&body=(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    if let emailURL = URL(string: coded!)
    {
    if UIApplication.shared.canOpenURL(emailURL)
    {
    UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
    if !result {
    // show some Toast or error alert
    //("Your device is not currently configured to send mail.")
    }
    })
    }
    }
    }





    share|improve this answer































      4














      Use the MFMailComposeViewController like this:





      1. Import the MessageUI



        import MessageUI



      2. Add the delegate to your class:



        class myClass: UIViewController, MFMailComposeViewControllerDelegate {}



      3. Configure the email preset you want to have



        let mail = MFMailComposeViewController()
        mail.mailComposeDelegate = self
        mail.setSubject("Subject")
        mail.setMessageBody("Body", isHTML: true)
        mail.setToRecipients(["my@email.com"])
        presentViewController(mail, animated: true, completion: nil)



      4. Put this method in your code:



        func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        dismissViewControllerAnimated(true, completion: nil)
        }



      There you go, works now.






      share|improve this answer



















      • 2





        do not forget check: if MFMailComposeViewController.canSendMail() {...

        – djdance
        Jan 5 '16 at 18:24



















      0














      I suggest to use Apple's way, which you can find in official documentation about MFMailComposeViewController. It opens a modal view controller with the email, which is dismissed after sending. Thus user stays in your app.






      share|improve this answer



















      • 1





        OP asked for Swift code, your reference displays Obj-C instead.

        – LinusGeffarth
        Nov 13 '15 at 14:57






      • 1





        The link leads to Apple's documentation, where are all information, which could Jan possibly need, not only specific parts (methods/properties). Apple has the sample code only in ObjC in this case, but it could be easily translated to Swift.

        – David Rysanek
        Nov 14 '15 at 10:14











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33694771%2fopen-email-from-app-with-predefined-text-in-ios%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      13














      You can do it using MFMailComposeViewController:



      import MessageUI

      let mailComposerVC = MFMailComposeViewController()
      mailComposerVC.mailComposeDelegate = self
      mailComposerVC.setToRecipients(["email@email.com"])
      mailComposerVC.setSubject("Subject")
      mailComposerVC.setMessageBody("Body", isHTML: false)
      self.presentViewController(mailComposerVC, animated: true, completion: nil)


      Also, you need to implement mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate where you should dismiss MFMailComposeViewController






      share|improve this answer





















      • 5





        Before you create your mailComposerVC it is not a bad idea to check if you actually can send an email using MFMailComposeViewController.canSendMail()

        – Au Ris
        May 2 '17 at 13:18
















      13














      You can do it using MFMailComposeViewController:



      import MessageUI

      let mailComposerVC = MFMailComposeViewController()
      mailComposerVC.mailComposeDelegate = self
      mailComposerVC.setToRecipients(["email@email.com"])
      mailComposerVC.setSubject("Subject")
      mailComposerVC.setMessageBody("Body", isHTML: false)
      self.presentViewController(mailComposerVC, animated: true, completion: nil)


      Also, you need to implement mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate where you should dismiss MFMailComposeViewController






      share|improve this answer





















      • 5





        Before you create your mailComposerVC it is not a bad idea to check if you actually can send an email using MFMailComposeViewController.canSendMail()

        – Au Ris
        May 2 '17 at 13:18














      13












      13








      13







      You can do it using MFMailComposeViewController:



      import MessageUI

      let mailComposerVC = MFMailComposeViewController()
      mailComposerVC.mailComposeDelegate = self
      mailComposerVC.setToRecipients(["email@email.com"])
      mailComposerVC.setSubject("Subject")
      mailComposerVC.setMessageBody("Body", isHTML: false)
      self.presentViewController(mailComposerVC, animated: true, completion: nil)


      Also, you need to implement mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate where you should dismiss MFMailComposeViewController






      share|improve this answer















      You can do it using MFMailComposeViewController:



      import MessageUI

      let mailComposerVC = MFMailComposeViewController()
      mailComposerVC.mailComposeDelegate = self
      mailComposerVC.setToRecipients(["email@email.com"])
      mailComposerVC.setSubject("Subject")
      mailComposerVC.setMessageBody("Body", isHTML: false)
      self.presentViewController(mailComposerVC, animated: true, completion: nil)


      Also, you need to implement mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate where you should dismiss MFMailComposeViewController







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 13 '18 at 12:05









      EderYif

      682820




      682820










      answered Nov 13 '15 at 14:22









      Michał KasprzykMichał Kasprzyk

      18917




      18917








      • 5





        Before you create your mailComposerVC it is not a bad idea to check if you actually can send an email using MFMailComposeViewController.canSendMail()

        – Au Ris
        May 2 '17 at 13:18














      • 5





        Before you create your mailComposerVC it is not a bad idea to check if you actually can send an email using MFMailComposeViewController.canSendMail()

        – Au Ris
        May 2 '17 at 13:18








      5




      5





      Before you create your mailComposerVC it is not a bad idea to check if you actually can send an email using MFMailComposeViewController.canSendMail()

      – Au Ris
      May 2 '17 at 13:18





      Before you create your mailComposerVC it is not a bad idea to check if you actually can send an email using MFMailComposeViewController.canSendMail()

      – Au Ris
      May 2 '17 at 13:18













      18














      If you'd like to open the built-in email app as opposed to showing the MFMailComposeViewController as has been mentioned by others, you could construct a mailto: link like this:



      let subject = "My subject"
      let body = "The awesome body of my email."
      let encodedParams = "subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
      let url = "mailto:foo@bar.com?(encodedParams)"

      if let emailURL = NSURL(url) {
      if UIApplication.sharedApplication().canOpenURL(emailURL) {
      UIApplication.sharedApplication().openURL(emailURL)
      }
      }




      Just to save anyone typing, for 2016 the syntax has changed slightly:



      let subject = "Some subject"
      let body = "Plenty of email body."
      let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

      if let emailURL:NSURL = NSURL(string: coded!)
      {
      if UIApplication.sharedApplication().canOpenURL(emailURL)
      {
      UIApplication.sharedApplication().openURL(emailURL)
      }





      share|improve this answer


























      • the actual correct answer to the question on the page! thanks man! :)

        – Fattie
        Jul 27 '16 at 18:36
















      18














      If you'd like to open the built-in email app as opposed to showing the MFMailComposeViewController as has been mentioned by others, you could construct a mailto: link like this:



      let subject = "My subject"
      let body = "The awesome body of my email."
      let encodedParams = "subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
      let url = "mailto:foo@bar.com?(encodedParams)"

      if let emailURL = NSURL(url) {
      if UIApplication.sharedApplication().canOpenURL(emailURL) {
      UIApplication.sharedApplication().openURL(emailURL)
      }
      }




      Just to save anyone typing, for 2016 the syntax has changed slightly:



      let subject = "Some subject"
      let body = "Plenty of email body."
      let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

      if let emailURL:NSURL = NSURL(string: coded!)
      {
      if UIApplication.sharedApplication().canOpenURL(emailURL)
      {
      UIApplication.sharedApplication().openURL(emailURL)
      }





      share|improve this answer


























      • the actual correct answer to the question on the page! thanks man! :)

        – Fattie
        Jul 27 '16 at 18:36














      18












      18








      18







      If you'd like to open the built-in email app as opposed to showing the MFMailComposeViewController as has been mentioned by others, you could construct a mailto: link like this:



      let subject = "My subject"
      let body = "The awesome body of my email."
      let encodedParams = "subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
      let url = "mailto:foo@bar.com?(encodedParams)"

      if let emailURL = NSURL(url) {
      if UIApplication.sharedApplication().canOpenURL(emailURL) {
      UIApplication.sharedApplication().openURL(emailURL)
      }
      }




      Just to save anyone typing, for 2016 the syntax has changed slightly:



      let subject = "Some subject"
      let body = "Plenty of email body."
      let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

      if let emailURL:NSURL = NSURL(string: coded!)
      {
      if UIApplication.sharedApplication().canOpenURL(emailURL)
      {
      UIApplication.sharedApplication().openURL(emailURL)
      }





      share|improve this answer















      If you'd like to open the built-in email app as opposed to showing the MFMailComposeViewController as has been mentioned by others, you could construct a mailto: link like this:



      let subject = "My subject"
      let body = "The awesome body of my email."
      let encodedParams = "subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
      let url = "mailto:foo@bar.com?(encodedParams)"

      if let emailURL = NSURL(url) {
      if UIApplication.sharedApplication().canOpenURL(emailURL) {
      UIApplication.sharedApplication().openURL(emailURL)
      }
      }




      Just to save anyone typing, for 2016 the syntax has changed slightly:



      let subject = "Some subject"
      let body = "Plenty of email body."
      let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

      if let emailURL:NSURL = NSURL(string: coded!)
      {
      if UIApplication.sharedApplication().canOpenURL(emailURL)
      {
      UIApplication.sharedApplication().openURL(emailURL)
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jul 27 '16 at 19:08









      Fattie

      20.2k31200439




      20.2k31200439










      answered Nov 13 '15 at 14:58









      mclaughlinjmclaughlinj

      4,64742537




      4,64742537













      • the actual correct answer to the question on the page! thanks man! :)

        – Fattie
        Jul 27 '16 at 18:36



















      • the actual correct answer to the question on the page! thanks man! :)

        – Fattie
        Jul 27 '16 at 18:36

















      the actual correct answer to the question on the page! thanks man! :)

      – Fattie
      Jul 27 '16 at 18:36





      the actual correct answer to the question on the page! thanks man! :)

      – Fattie
      Jul 27 '16 at 18:36











      13














      Swift 3 Version



      let subject = "Some subject"
      let body = "Plenty of email body."
      let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

      if let emailURL: NSURL = NSURL(string: coded!) {
      if UIApplication.shared.canOpenURL(emailURL as URL) {
      UIApplication.shared.openURL(emailURL as URL)
      }
      }





      share|improve this answer






























        13














        Swift 3 Version



        let subject = "Some subject"
        let body = "Plenty of email body."
        let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

        if let emailURL: NSURL = NSURL(string: coded!) {
        if UIApplication.shared.canOpenURL(emailURL as URL) {
        UIApplication.shared.openURL(emailURL as URL)
        }
        }





        share|improve this answer




























          13












          13








          13







          Swift 3 Version



          let subject = "Some subject"
          let body = "Plenty of email body."
          let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

          if let emailURL: NSURL = NSURL(string: coded!) {
          if UIApplication.shared.canOpenURL(emailURL as URL) {
          UIApplication.shared.openURL(emailURL as URL)
          }
          }





          share|improve this answer















          Swift 3 Version



          let subject = "Some subject"
          let body = "Plenty of email body."
          let coded = "mailto:blah@blah.com?subject=(subject)&body=(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

          if let emailURL: NSURL = NSURL(string: coded!) {
          if UIApplication.shared.canOpenURL(emailURL as URL) {
          UIApplication.shared.openURL(emailURL as URL)
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 24 '18 at 4:28

























          answered Nov 6 '16 at 2:15









          Matthew BarkerMatthew Barker

          371412




          371412























              5














              Swift 4.0



                  let email = "feedback@company.com"
              let subject = "subject"
              let bodyText = "Please provide information that will help us to serve you better"
              if MFMailComposeViewController.canSendMail() {
              let mailComposerVC = MFMailComposeViewController()
              mailComposerVC.mailComposeDelegate = self
              mailComposerVC.setToRecipients([email])
              mailComposerVC.setSubject(subject)
              mailComposerVC.setMessageBody(bodyText, isHTML: true)
              self.present(mailComposerVC, animated: true, completion: nil)
              } else {
              let coded = "mailto:(email)?subject=(subject)&body=(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
              if let emailURL = URL(string: coded!)
              {
              if UIApplication.shared.canOpenURL(emailURL)
              {
              UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
              if !result {
              // show some Toast or error alert
              //("Your device is not currently configured to send mail.")
              }
              })
              }
              }
              }





              share|improve this answer




























                5














                Swift 4.0



                    let email = "feedback@company.com"
                let subject = "subject"
                let bodyText = "Please provide information that will help us to serve you better"
                if MFMailComposeViewController.canSendMail() {
                let mailComposerVC = MFMailComposeViewController()
                mailComposerVC.mailComposeDelegate = self
                mailComposerVC.setToRecipients([email])
                mailComposerVC.setSubject(subject)
                mailComposerVC.setMessageBody(bodyText, isHTML: true)
                self.present(mailComposerVC, animated: true, completion: nil)
                } else {
                let coded = "mailto:(email)?subject=(subject)&body=(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                if let emailURL = URL(string: coded!)
                {
                if UIApplication.shared.canOpenURL(emailURL)
                {
                UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
                if !result {
                // show some Toast or error alert
                //("Your device is not currently configured to send mail.")
                }
                })
                }
                }
                }





                share|improve this answer


























                  5












                  5








                  5







                  Swift 4.0



                      let email = "feedback@company.com"
                  let subject = "subject"
                  let bodyText = "Please provide information that will help us to serve you better"
                  if MFMailComposeViewController.canSendMail() {
                  let mailComposerVC = MFMailComposeViewController()
                  mailComposerVC.mailComposeDelegate = self
                  mailComposerVC.setToRecipients([email])
                  mailComposerVC.setSubject(subject)
                  mailComposerVC.setMessageBody(bodyText, isHTML: true)
                  self.present(mailComposerVC, animated: true, completion: nil)
                  } else {
                  let coded = "mailto:(email)?subject=(subject)&body=(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                  if let emailURL = URL(string: coded!)
                  {
                  if UIApplication.shared.canOpenURL(emailURL)
                  {
                  UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
                  if !result {
                  // show some Toast or error alert
                  //("Your device is not currently configured to send mail.")
                  }
                  })
                  }
                  }
                  }





                  share|improve this answer













                  Swift 4.0



                      let email = "feedback@company.com"
                  let subject = "subject"
                  let bodyText = "Please provide information that will help us to serve you better"
                  if MFMailComposeViewController.canSendMail() {
                  let mailComposerVC = MFMailComposeViewController()
                  mailComposerVC.mailComposeDelegate = self
                  mailComposerVC.setToRecipients([email])
                  mailComposerVC.setSubject(subject)
                  mailComposerVC.setMessageBody(bodyText, isHTML: true)
                  self.present(mailComposerVC, animated: true, completion: nil)
                  } else {
                  let coded = "mailto:(email)?subject=(subject)&body=(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                  if let emailURL = URL(string: coded!)
                  {
                  if UIApplication.shared.canOpenURL(emailURL)
                  {
                  UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
                  if !result {
                  // show some Toast or error alert
                  //("Your device is not currently configured to send mail.")
                  }
                  })
                  }
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 24 '18 at 13:04









                  Manish NaharManish Nahar

                  294312




                  294312























                      4














                      Use the MFMailComposeViewController like this:





                      1. Import the MessageUI



                        import MessageUI



                      2. Add the delegate to your class:



                        class myClass: UIViewController, MFMailComposeViewControllerDelegate {}



                      3. Configure the email preset you want to have



                        let mail = MFMailComposeViewController()
                        mail.mailComposeDelegate = self
                        mail.setSubject("Subject")
                        mail.setMessageBody("Body", isHTML: true)
                        mail.setToRecipients(["my@email.com"])
                        presentViewController(mail, animated: true, completion: nil)



                      4. Put this method in your code:



                        func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
                        dismissViewControllerAnimated(true, completion: nil)
                        }



                      There you go, works now.






                      share|improve this answer



















                      • 2





                        do not forget check: if MFMailComposeViewController.canSendMail() {...

                        – djdance
                        Jan 5 '16 at 18:24
















                      4














                      Use the MFMailComposeViewController like this:





                      1. Import the MessageUI



                        import MessageUI



                      2. Add the delegate to your class:



                        class myClass: UIViewController, MFMailComposeViewControllerDelegate {}



                      3. Configure the email preset you want to have



                        let mail = MFMailComposeViewController()
                        mail.mailComposeDelegate = self
                        mail.setSubject("Subject")
                        mail.setMessageBody("Body", isHTML: true)
                        mail.setToRecipients(["my@email.com"])
                        presentViewController(mail, animated: true, completion: nil)



                      4. Put this method in your code:



                        func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
                        dismissViewControllerAnimated(true, completion: nil)
                        }



                      There you go, works now.






                      share|improve this answer



















                      • 2





                        do not forget check: if MFMailComposeViewController.canSendMail() {...

                        – djdance
                        Jan 5 '16 at 18:24














                      4












                      4








                      4







                      Use the MFMailComposeViewController like this:





                      1. Import the MessageUI



                        import MessageUI



                      2. Add the delegate to your class:



                        class myClass: UIViewController, MFMailComposeViewControllerDelegate {}



                      3. Configure the email preset you want to have



                        let mail = MFMailComposeViewController()
                        mail.mailComposeDelegate = self
                        mail.setSubject("Subject")
                        mail.setMessageBody("Body", isHTML: true)
                        mail.setToRecipients(["my@email.com"])
                        presentViewController(mail, animated: true, completion: nil)



                      4. Put this method in your code:



                        func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
                        dismissViewControllerAnimated(true, completion: nil)
                        }



                      There you go, works now.






                      share|improve this answer













                      Use the MFMailComposeViewController like this:





                      1. Import the MessageUI



                        import MessageUI



                      2. Add the delegate to your class:



                        class myClass: UIViewController, MFMailComposeViewControllerDelegate {}



                      3. Configure the email preset you want to have



                        let mail = MFMailComposeViewController()
                        mail.mailComposeDelegate = self
                        mail.setSubject("Subject")
                        mail.setMessageBody("Body", isHTML: true)
                        mail.setToRecipients(["my@email.com"])
                        presentViewController(mail, animated: true, completion: nil)



                      4. Put this method in your code:



                        func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
                        dismissViewControllerAnimated(true, completion: nil)
                        }



                      There you go, works now.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '15 at 14:27









                      LinusGeffarthLinusGeffarth

                      8,631145298




                      8,631145298








                      • 2





                        do not forget check: if MFMailComposeViewController.canSendMail() {...

                        – djdance
                        Jan 5 '16 at 18:24














                      • 2





                        do not forget check: if MFMailComposeViewController.canSendMail() {...

                        – djdance
                        Jan 5 '16 at 18:24








                      2




                      2





                      do not forget check: if MFMailComposeViewController.canSendMail() {...

                      – djdance
                      Jan 5 '16 at 18:24





                      do not forget check: if MFMailComposeViewController.canSendMail() {...

                      – djdance
                      Jan 5 '16 at 18:24











                      0














                      I suggest to use Apple's way, which you can find in official documentation about MFMailComposeViewController. It opens a modal view controller with the email, which is dismissed after sending. Thus user stays in your app.






                      share|improve this answer



















                      • 1





                        OP asked for Swift code, your reference displays Obj-C instead.

                        – LinusGeffarth
                        Nov 13 '15 at 14:57






                      • 1





                        The link leads to Apple's documentation, where are all information, which could Jan possibly need, not only specific parts (methods/properties). Apple has the sample code only in ObjC in this case, but it could be easily translated to Swift.

                        – David Rysanek
                        Nov 14 '15 at 10:14
















                      0














                      I suggest to use Apple's way, which you can find in official documentation about MFMailComposeViewController. It opens a modal view controller with the email, which is dismissed after sending. Thus user stays in your app.






                      share|improve this answer



















                      • 1





                        OP asked for Swift code, your reference displays Obj-C instead.

                        – LinusGeffarth
                        Nov 13 '15 at 14:57






                      • 1





                        The link leads to Apple's documentation, where are all information, which could Jan possibly need, not only specific parts (methods/properties). Apple has the sample code only in ObjC in this case, but it could be easily translated to Swift.

                        – David Rysanek
                        Nov 14 '15 at 10:14














                      0












                      0








                      0







                      I suggest to use Apple's way, which you can find in official documentation about MFMailComposeViewController. It opens a modal view controller with the email, which is dismissed after sending. Thus user stays in your app.






                      share|improve this answer













                      I suggest to use Apple's way, which you can find in official documentation about MFMailComposeViewController. It opens a modal view controller with the email, which is dismissed after sending. Thus user stays in your app.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '15 at 14:28









                      David RysanekDavid Rysanek

                      656911




                      656911








                      • 1





                        OP asked for Swift code, your reference displays Obj-C instead.

                        – LinusGeffarth
                        Nov 13 '15 at 14:57






                      • 1





                        The link leads to Apple's documentation, where are all information, which could Jan possibly need, not only specific parts (methods/properties). Apple has the sample code only in ObjC in this case, but it could be easily translated to Swift.

                        – David Rysanek
                        Nov 14 '15 at 10:14














                      • 1





                        OP asked for Swift code, your reference displays Obj-C instead.

                        – LinusGeffarth
                        Nov 13 '15 at 14:57






                      • 1





                        The link leads to Apple's documentation, where are all information, which could Jan possibly need, not only specific parts (methods/properties). Apple has the sample code only in ObjC in this case, but it could be easily translated to Swift.

                        – David Rysanek
                        Nov 14 '15 at 10:14








                      1




                      1





                      OP asked for Swift code, your reference displays Obj-C instead.

                      – LinusGeffarth
                      Nov 13 '15 at 14:57





                      OP asked for Swift code, your reference displays Obj-C instead.

                      – LinusGeffarth
                      Nov 13 '15 at 14:57




                      1




                      1





                      The link leads to Apple's documentation, where are all information, which could Jan possibly need, not only specific parts (methods/properties). Apple has the sample code only in ObjC in this case, but it could be easily translated to Swift.

                      – David Rysanek
                      Nov 14 '15 at 10:14





                      The link leads to Apple's documentation, where are all information, which could Jan possibly need, not only specific parts (methods/properties). Apple has the sample code only in ObjC in this case, but it could be easily translated to Swift.

                      – David Rysanek
                      Nov 14 '15 at 10:14


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33694771%2fopen-email-from-app-with-predefined-text-in-ios%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values