Sending Email in Swift: Libraries and Examples for iOS/macOS

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Email Sending in Swift (iOS/macOS): Libraries and Practical Examples



Sending emails is one of the key functions of many applications: from user registration and password reset to sending notifications and marketing newsletters. In the Swift ecosystem (iOS/macOS), there are several approaches to implementing this task. In this article, we will review the main libraries, their capabilities, and provide ready-made code examples for integration.



Main Approaches to Sending Email in Swift



There are three main ways to send emails from Swift applications:

  • Using MFMailComposeViewController — a built-in system interface for sending emails (iOS only). It allows the user to send an email via the mail client installed on the device, but is not suitable for automatic (server-side) sending.
  • Direct SMTP Connection — libraries that implement the SMTP protocol on the client side (e.g., SwiftSMTP). Suitable for sending emails directly from the device without an intermediate server.
  • Using Third-Party Service APIs — cloud solutions (Mailgun, SendGrid, Amazon SES) that provide a REST API. This is the most reliable and scalable method for production applications.


SwiftSMTP Library for Direct SMTP Connection



SwiftSMTP is a popular open-source library that implements an SMTP client in Swift. It supports:

  • Sending text and HTML emails
  • Attachments (images, PDFs, archives)
  • TLS/SSL support
  • Login/password authentication


Example of sending a simple email via Gmail SMTP:



import SwiftSMTP

let smtp = SMTP( host: "smtp.gmail.com", // SMTP server port: 587, // port for TLS username: "you@gmail.com", password: "your-app-password")

let mail = Mail( from: Mail.User(name: "Sender", email: "you@gmail.com"), to: [Mail.User(name: "Recipient", email: "recipient@example.com")], subject: "Hello from Swift!", text: "This is a test email sent using SwiftSMTP.")

smtp.send(mail) { error in if let error = error { print("Error sending: \\(error)") } else { print("Email sent successfully!") }}


To work with Gmail, you will need an App Password, as a regular password will not work when two-factor authentication is enabled.



Sending Email via Mailgun (API)



Mailgun is one of the most popular services for sending transactional emails. It provides a simple REST API and an SDK for Swift. Key advantages:

  • High deliverability (due to IP reputation)
  • Open and click tracking
  • Template support


Example of sending an email via the Mailgun API using URLSession:



import Foundation

func sendEmailViaMailgun(to recipient: String, subject: String, body: String) { let apiKey = "YOUR_MAILGUN_API_KEY" let domain = "YOUR_DOMAIN.mailgun.org" let urlString = "https://api.mailgun.net/v3/\\(domain)/messages" guard let url = URL(string: urlString) else { return } var request = URLRequest(url: url) request.httpMethod = "POST" let credentials = "api:\\(apiKey)" let authData = credentials.data(using: .utf8)!.base64EncodedString() request.setValue("Basic \\(authData)", forHTTPHeaderField: "Authorization") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") let parameters = [ "from": "My App <noreply@\\(domain)>", "to": recipient, "subject": subject, "text": body ] let bodyString = parameters

Recommendations