Cocoanetics/SwiftMail
Swift Mail Access to IMAP and SMTP
Overview
SwiftMail is a powerful email package that enables you to work with email protocols in your Swift applications. The package provides two main components:
IMAPServer
Handles IMAP server connections for retrieving and managing emails. Implements key IMAP capabilities including:
- Mailbox operations (SELECT, LIST, COPY, MOVE)
- Message operations (FETCH headers/parts/structure, STORE flags) with automatic chunking for large sets
- Special-use mailbox support
- Creating new messages via APPEND (draft-friendly)
- Namespace-aware mailbox resolution (NAMESPACE)
- Extended search with structured results (ESEARCH)
- Append size preflight checks (APPENDLIMIT)
- TLS encryption
- UID-based operations via UIDPLUS
π IMAP Capability Support: Gmail vs iCloud vs Dovecot vs Exchange vs IMAPServer
The table below compares common IMAP capabilities across Gmail, iCloud, Dovecot, Exchange, and SwiftMail's IMAPServer. NIOIMAP indicates capability/command/parser support present in the underlying swift-nio-imap grammar layer. The final column indicates whether IMAPServer implements support for each capability.
| IMAP Capability | Description | Gmail | iCloud | Dovecot | Exchange | NIOIMAP | IMAPServer | |-----------------|---------------------------------------------------------------|:-----:|:------:|:-------:|:--------:|:------:|:----------:| | IMAP4rev1 | Standard IMAP protocol (RFC 3501) | β | β | β | β | β | β | | UNSELECT | Unselect mailbox without selecting another (RFC 3691) | β | β | β | β | β | β | | IDLE | Push new message alerts (RFC 2177) | β | β | β | β | β | β | | NAMESPACE | Query folder structure roots (RFC 2342) | β | β | β | β | β | β | | QUOTA | Storage quota reporting (RFC 2087) | β | β | β | β | β | β | | ID | Identify client/server (RFC 2971) | β | β | β | β | β | β | | XLIST | Gmail folder role mapping (legacy) | β | β | β | β | β | β | | CHILDREN | Show folder substructure (RFC 3348) | β | β | β | β | β | β | | X-GM-EXT-1 | Gmail labels, threads, msg IDs | β | β | β | β | β | β | | UIDPLUS | Enhanced UID operations (RFC 4315) | β | β | β | β | β | β | | COMPRESS=DEFLATE | zlib compression (RFC 4978) | β | β | β | β | β | β | | ENABLE | Enable optional extensions (RFC 5161) | β | β | β | β | β | β | | MOVE | Native IMAP MOVE command (RFC 6851) | β | β | β | β | β | β | | CONDSTORE | Efficient state sync (RFC 7162) | β | β | β | β | β | β | | ESEARCH | Extended search (RFC 4731) | β | β | β | β | β | β | | UTF8=ACCEPT | UTF-8 folder & header support (RFC 6855) | β | β | β | β | β | β | | LIST-EXTENDED | Advanced mailbox listing (RFC 5258) | β | β | β | β | β | β | | LIST-STATUS | List + status in one (RFC 5819) | β | β | β | β | β | β | | LITERAL- | Literal string optimization (RFC 7888) | β | β | β | β | β | β | | SPECIAL-USE | Modern folder role marking (RFC 6154) | β | β | β | β | β | β | | APPENDLIMIT=β¦ | Message size limit for uploads | β | β | β | β | β | β | | QRESYNC | Quick resync (RFC 5162) | β | β | β | β | β | β | | SORT | Server-side message sorting (RFC 5256) | β | β | β | β | β | β | | ESORT | Extended SORT results (RFC 5267) | β | β | β | β | β | β | | CONTEXT=SORT | Persistent sort context | β | β | β | β | β | β | | WITHIN | Search by relative time (RFC 5032) | β | β | β | β | β | β | | SASL-IR | Initial SASL response support (RFC 4959) | β | β | β | β | β | β | | XAPPLEPUSHSERVICE | Apple push integration for Mail app | β | β | β | β | β | β | | XAPPLELITERAL | Apple literal transmission optimization | β | β | β | β | β | β | | X-APPLE-REMOTE-LINKS | Apple-specific remote links extension | β | β | β | β | β | β |
Exchange source (captured 2026-03-05 from outlook.office365.com:993 via IMAPS CAPABILITY):
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
SMTPServer
Handles email sending via SMTP with support for:
- Multiple authentication methods (PLAIN, LOGIN, XOAUTH2)
- TLS encryption
- 8BITMIME support
- Full MIME email composition
- Multiple recipients (To, CC, BCC)
Command Line Demos
The package includes command line demos that showcase the functionality of both the IMAP and SMTP libraries:
- SwiftIMAPCLI: Demonstrates IMAP operations like listing mailboxes and fetching messages
- SwiftSMTPCLI: Demonstrates sending emails via SMTP
Both demos look for a .env file in the current working directory for configuration. Create a .env file with the following variables:
# IMAP Configuration
IMAP_HOST=imap.example.com
IMAP_PORT=993
IMAP_USERNAME=your_username
IMAP_PASSWORD=your_password
# SMTP Configuration
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USERNAME=your_username
SMTP_PASSWORD=your_passwordNote for Gmail Users: When using Gmail, you cannot authenticate with your Google account password. Instead, you must create an app-specific password and use that as your password in the configuration above.
To run the demos:
# Run the IMAP demo
swift run SwiftIMAPCLI
# Run the SMTP demo
swift run SwiftSMTPCLI
# Run with debug logging enabled (recommended for development)
ENABLE_DEBUG_OUTPUT=1 OS_ACTIVITY_DT_MODE=debug swift run SwiftIMAPCLI
ENABLE_DEBUG_OUTPUT=1 OS_ACTIVITY_DT_MODE=debug swift run SwiftSMTPCLIThe debug logging options:
ENABLE_DEBUG_OUTPUT=1: Enables trace level loggingOS_ACTIVITY_DT_MODE=debug: Formats debug output in a readable way
Creating Drafts via IMAP
SwiftMail lets you build a draft with the shared Email model (also used by SMTP) and store it directly on the server:
let draft = Email(
sender: EmailAddress(name: "Me", address: "me@example.com"),
recipients: [],
subject: "Quarterly update",
textBody: "Jot down your notes hereβ¦"
)
let appendResult = try await imapServer.createDraft(from: draft)
if let uid = appendResult.firstUID {
print("Draft stored with UID \(uid.value)")
}Need a custom target mailbox or additional flags? Use the lower-level helper:
try await imapServer.append(
email: draft,
to: "Archive/Drafts",
flags: [.seen]
)Requirements
- Swift 5.9+
- macOS 11.0+
- iOS 14.0+
- tvOS 14.0+
- watchOS 7.0+
- macCatalyst 14.0+
Dependencies
- SwiftNIO
- SwiftNIOSSL
- SwiftNIOIMAP (for IMAP only)
- SwiftDotenv (for CLI demos)
- Swift Testing (for tests only)
- Swift Logging
License
This project is licensed under the BSD 2-Clause License - see the LICENSE file for details.
Package Metadata
Repository: Cocoanetics/SwiftMail
Stars: 68
Forks: 27
Open issues: 1
Default branch: main
Primary language: swift
License: BSD-2-Clause
README: README.md