Open eMail from App with predefined text in iOS
Solution 1
You can do it using MFMailComposeViewController
:
import MessageUI
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["[email protected]"])
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
Solution 2
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:[email protected]?\(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:[email protected]?subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
if let emailURL:NSURL = NSURL(string: coded!)
{
if UIApplication.sharedApplication().canOpenURL(emailURL)
{
UIApplication.sharedApplication().openURL(emailURL)
}
Solution 3
Swift 3 Version
let subject = "Some subject"
let body = "Plenty of email body."
let coded = "mailto:[email protected]?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)
}
}
Solution 4
Swift 4.0
let email = "[email protected]"
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.")
}
})
}
}
}
Solution 5
Use the MFMailComposeViewController
like this:
Import the MessageUI
import MessageUI
Add the delegate to your class:
class myClass: UIViewController, MFMailComposeViewControllerDelegate {}
Configure the email preset you want to have
let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setSubject("Subject") mail.setMessageBody("Body", isHTML: true) mail.setToRecipients(["[email protected]"]) presentViewController(mail, animated: true, completion: nil)
Put this method in your code:
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) { dismissViewControllerAnimated(true, completion: nil) }
There you go, works now.
JanScott
Updated on July 26, 2022Comments
-
JanScott almost 2 years
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 = "[email protected]" let urlEMail = NSURL(string: "mailto:\(email)") if UIApplication.sharedApplication().canOpenURL(urlEMail!) { UIApplication.sharedApplication().openURL(urlEMail!) } else { print("Ups") }
-
LinusGeffarth over 8 yearsOP asked for Swift code, your reference displays Obj-C instead.
-
David Rysanek over 8 yearsThe 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.
-
Fattie almost 8 yearsthe actual correct answer to the question on the page! thanks man! :)
-
Au Ris about 7 yearsBefore you create your mailComposerVC it is not a bad idea to check if you actually can send an email using
MFMailComposeViewController.canSendMail()