Send emails in Swift 5 (on iOS, Mac, and Linux) with ease.
Send Email in Xcode: A Guide to Setting up Swift Environment and Using SMTP and HTTP APIs to Send Email in Swift for iOS/Mac Development
Swift is a popular programming language from Apple for iOS/Mac development, web development, data, and machine learning. It is also a great language for sending emails. In this guide we will show you how to set up a swift environment using Swift PackageManager and send emails in code and tests using SMTP and HTTP APIs.
Getting started with Swift
Swift is a cross-platform programming language that is primarily aimed as Mac users but is also available for Linux and Windows environments. For these examples we will focus on OSX.
Install Swift
The first step is to search the MacStore for Xcode and install the development environment:
Then go to the Swift homepage and download the Swift compiler package.
Verify installation
Once installed you can verify the installation using the REPL in a terminal. Run swift
in the Terminal app to start the command line:
% swift
Welcome to Apple Swift
Type :help for assistance.
Note if you encounter errors you may need to accept the XCode terms and conditions. Run
sudo xcodebuild -license
in a terminal to accept the terms.
Use the REPL (Read-Eval-Print Loop) to execute code and test Swift functionality in the terminal:
1> print("hello")
hello
Setup new project
The swift standard library contains many functions but to use SMTP functionality we will need to install external packages. It is therefore useful to learn about the inbuilt swift package management system called PackageManager.
To initialize a new project using Swift Package Manager navigate to your project directory (we will use a folder swift-email-smtp-examples
) and run the init command:
swift package init
This command will scaffold a new project and create the following files:
Creating library package: swift-email-smtp-examples
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/swift-email-smtp-examples/swift_email_smtp_examples.swift
Creating Tests/
Creating Tests/swift-email-smtp-examplesTests/
Creating Tests/swift-email-smtp-examplesTests/swift_email_smtp_examplesTests.swift
To integrate the package with Xcode we can generate associated files with the following:
swift package generate-xcodeproj
Adding dependencies
Your project should now have a Package.swift
file that contains our project description, platform targets, and dependencies. Here is an example file:
// swift-tools-version:5.5
import PackageDescription
let package = Package(
name: "your-package",
products: [
],
dependencies: [
],
targets: [
.testTarget(
name: "your-package-tests",
dependencies: [
]),
]
)
Notice the empty dependencies
array. That is where we can add Swift packages from GitHub URLs. Each package take the form of:
.package(url: "https://github.com/user-name/package-name.git", .exact("1.0.0"))
We can find packages to add on the Swift Package Index.
Swift email libraries
In this example we will explore sending emails using several libraries: