Sending Email in Swift (iOS/macOS): Complete Guide
Sending emails is one of the key functions of modern mobile and desktop applications. In the Apple ecosystem for Swift (iOS/macOS), there are two main approaches: using the built-in MFMailComposeViewController controller for simple sending via the device's mail client, and working over the SMTP protocol through third-party libraries such as SwiftSMTP. In this article, we will thoroughly examine both methods, their advantages, limitations, and provide practical code examples.
1. Sending Email via MFMailComposeViewController
MFMailComposeViewController is a component from the MessageUI.framework framework that provides a ready-made interface for creating and sending an email. This method is ideal when you need to quickly implement sending without configuring an SMTP server.
Advantages and Limitations
- Advantages: does not require SMTP configuration, supports HTML content, attachments, automatically uses the mail account configured on the device.
- Limitations: works only on real devices (not in the simulator), requires a configured mail account, not suitable for background sending without a UI.
Implementation Example
First, import the framework and implement the delegate:
import UIKitimport MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBAction func sendEmailTapped(_ sender: UIButton) { guard MFMailComposeViewController.canSendMail() else { print("Mail is not configured on the device") return }
let mailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self mailComposer.setToRecipients(["recipient@example.com"]) mailComposer.setSubject("Hello from Swift!") mailComposer.setMessageBody("<h1>Test Email</h1><p>This is HTML content.</p>", isHTML: true)
// Adding an attachment if let fileData = "Text file".data(using: .utf8) { mailComposer.addAttachmentData(fileData, mimeType: "text/plain", fileName: "example.txt") }
present(mailComposer, animated: true, completion: nil) }
// Delegate to handle the result func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { switch result { case .cancelled: print("Sending cancelled") case .saved: print("Email saved as draft") case .sent: print("Email sent") case .failed: print("Sending error: \\(error?.localizedDescription ?? "unknown")") @unknown default: break } controller.dismiss(animated: true, completion: nil) }}2. Sending Email via SMTP with the SwiftSMTP Library
If you need to send emails programmatically, without user interaction (for example, for registration emails or notifications), use the SMTP protocol. One of the popular libraries for Swift is SwiftSMTP. It supports TLS/SSL, authentication, and working with attachments.
Installation via Swift Package Manager
Add the dependency to Package.swift:
dependencies: [ .package(url: "https://github.com/Kitura/Swift-SMTP.git", from: "2.0.0")]Example of Sending via Gmail SMTP Server
import SwiftSMTP
func sendEmailViaSMTP() { let smtp = SMTP( hostname: "smtp.gmail.com", // SMTP server email: "your-email@gmail.com", password: "your-app-passwo