Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an email from swift 3

I am trying to set up an app with send email option.

I have this code:

import Foundation import MessageUI import UIKit  class emailClass: UIViewController, MFMailComposeViewControllerDelegate {     override func viewDidLoad() {         super.viewDidLoad()          if !MFMailComposeViewController.canSendMail() {             print("Mail services are not available")             return         }                 sendEmail()      }      func sendEmail() {               let composeVC = MFMailComposeViewController()         composeVC.mailComposeDelegate = self         // Configure the fields of the interface.         composeVC.setToRecipients(["[email protected]"])         composeVC.setSubject("Hello!")         composeVC.setMessageBody("Hello this is my message body!", isHTML: false)         // Present the view controller modally.         self.present(composeVC, animated: true, completion: nil)     }      func mailComposeController(controller: MFMailComposeViewController,                            didFinishWithResult result: MFMailComposeResult, error: NSError?) {         // Check the result or perform other tasks.         // Dismiss the mail compose view controller.         controller.dismiss(animated: true, completion: nil)     }      override func didReceiveMemoryWarning() {         super.didReceiveMemoryWarning()         // Dispose of any resources that can be recreated.     } } 

So I get this message: "Mail services are not available". Now I've logged in the simulator device in iCloud... So i think it should do it but it's not. Why isn't this working? Can you tell me whats wrong and how can I move forward?

like image 490
H.N. Avatar asked Nov 30 '16 11:11

H.N.


People also ask

What is a swift email?

Swift Mailer is a component based library for sending e-mails from PHP applications.


2 Answers

It will not work with simulator. Please test it on iPhone device. You can refer Apple Developer Portal - MFMailComposeViewController

like image 51
SandeepM Avatar answered Sep 16 '22 16:09

SandeepM


Here's how I did it. It looks like you followed the documentation very well, I thought I'd add my variation in case it helps someone else. Plus, this is a little more updated to current (Aug 2017) syntax.

Conform to the MFMailComposeViewControllerDelegate protocol, and check if the device can send mail.

import Foundation import UIKit import MessageUI  class WelcomeViewController: UIViewController, MFMailComposeViewControllerDelegate {   override func viewDidLoad() {     super.viewDidLoad()      if !MFMailComposeViewController.canSendMail() {         print("Mail services are not available")         return     } } 

My app uses an IBAction to initiate the mail composition.

@IBAction func sendFeedbackButtonTapped(_ sender: Any) {      let composeVC = MFMailComposeViewController()     composeVC.mailComposeDelegate = self      // Configure the fields of the interface.     composeVC.setToRecipients(["[email protected]"])     composeVC.setSubject("Message Subject")     composeVC.setMessageBody("Message content.", isHTML: false)      // Present the view controller modally.     self.present(composeVC, animated: true, completion: nil)  } 

About the following mailComposeController function, the documentation says

The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController(_:didFinishWith:error:) method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in Listing 3. You can also use this method to check the result of the operation.

func mailComposeController(_ controller: MFMailComposeViewController,                            didFinishWith result: MFMailComposeResult, error: Error?) {     // Check the result or perform other tasks.      // Dismiss the mail compose view controller.     controller.dismiss(animated: true, completion: nil)    } } 

Source Apple Documentation: MFMailComposeViewController

like image 39
Josh Avatar answered Sep 18 '22 16:09

Josh