Email Sending in Swift (iOS/macOS): Complete Guide
Sending emails is one of the key features of modern mobile and desktop applications. On Apple platforms (iOS, macOS, watchOS, tvOS), Swift developers can use several approaches: from the built-in MailComposeViewController to powerful third-party libraries that work directly with SMTP servers. In this article, we will cover the main ways to implement email sending in Swift, including practical code examples, SMTP configuration, and working with attachments.
Why is email sending needed in an app?
Email functionality can be useful in various scenarios:
- Registration and account confirmation
- Sending error reports and feedback
- Sending notifications and newsletters
- Sending receipts, invoices, and documents
- Password recovery
Ways to send email in Swift
There are two main approaches:
- Built-in Mail Compose (MFMailComposeViewController) — uses the standard iOS/macOS mail app. Simple, but requires the user to be logged into a mail client.
- Direct sending via SMTP — sends emails directly from the server, without user involvement. Ideal for automatic notifications.
Method 1: Using MFMailComposeViewController
This method is suitable when you need to allow the user to send an email from the app using their personal mailbox.
import UIKitimport MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate { @IBAction func sendEmailTapped(_ sender: UIButton) { guard MFMailComposeViewController.canSendMail() else { print("Mail client is not configured") return } let mailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self mailComposer.setToRecipients(["support@example.com"]) mailComposer.setSubject("Feedback from the app") mailComposer.setMessageBody("Hello! I have a question...", isHTML: false) // Adding an attachment if let fileData = "Example text".data(using: .utf8) { mailComposer.addAttachmentData(fileData, mimeType: "text/plain", fileName: "file.txt") } present(mailComposer, animated: true, completion: nil) } // MARK: - MFMailComposeViewControllerDelegate func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { controller.dismiss(animated: true, completion: nil) switch result { case .sent: print("Email sent") case .saved: print("Email saved") case .cancelled: print("Sending cancelled") case .failed: print("Sending error: \\(error?.localizedDescription ?? "")") @unknown default: break } }}Method 2: Direct sending via SMTP (SwiftSMTP)
For automatic email sending without user involvement, the SwiftSMTP library is used. It allows connecting to any SMTP server (Gmail, Yandex, Mail.ru, custom server).
Installation via Swift Package Manager:
// In Package.swift or via Xcode: File → Add Packagesdependencies: [ .package(url: "https://github.com/Kitura/Swift-SMTP.git", from: "2.0.0")]Example of sending an email via SMTP:
import SwiftSMTP
func sendEmail() { let smtp = SMTP( hostname: "smtp.gmail.com", // SMTP s