Sending Email through your app in Swift

Posted By : Akshay Luthra | 18-Dec-2015

 

First of all, we need to import the MessageUI module.

 

Second, we need to specify that the View Controller will conform to the MFMailComposeViewControllerDelegate protocol.

 

In order to send Email through your app in Swift, you need a few things :

 

1. A View Controller from which the user will initiate the display of the email composer screen, say by tapping on a button.

@IBAction func sendEmailButtonTapped(sender: AnyObject) {
      // Logic to send the Email goes here
}
 

 

2. A configured MFMailComposeViewController to present.

func configuredMailComposeViewController() -> MFMailComposeViewController {
      let mailComposerVC = MFMailComposeViewController()
      mailComposerVC.mailComposeDelegate = self
        
      mailComposerVC.setToRecipients([])
      mailComposerVC.setSubject("Sending In-App Email")
      mailComposerVC.setMessageBody("Sending Email through your app in Swift", isHTML: false)
        
      return mailComposerVC
}
 

 

3. A MFMailComposeViewControllerDelegate to handle dismissing the email composer screen.

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

 

The whole Swift code for sending Email through your app :

import Foundation
import UIKit
import MessageUI
 
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func sendEmailButtonTapped(sender: AnyObject) {
        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(mailComposeViewController, animated: true, completion: nil)
        } else {
            self.showSendMailErrorAlert()
        }
    }
    
    func configuredMailComposeViewController() -> MFMailComposeViewController {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self
        
        mailComposerVC.setToRecipients([])
        mailComposerVC.setSubject("Sending In-App Email")
        mailComposerVC.setMessageBody("Sending Email through your app in Swift", isHTML: false)
        
        return mailComposerVC
    }
    
    func showSendMailErrorAlert() {
        let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send Email.  Please check Email configuration and try again.", delegate: self, cancelButtonTitle: "OK")
        sendMailErrorAlert.show()
    }
    
    // MFMailComposeViewControllerDelegate Method
    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
}
 

 

Thanks for reading this blog!

 

About Author

Author Image
Akshay Luthra

Akshay Luthra has excellent experience in developing Cross Platform Mobile Apps using JavaScript and Titanium Framework. He loves listening to music and travelling new places.

Request for Proposal

Name is required

Comment is required

Sending message..