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 in the Terminal app to start the command line:

Note if you encounter errors you may need to accept the XCode terms and conditions. Run in a terminal to accept the terms.

Use the REPL (Read-Eval-Print Loop) to execute code and test Swift functionality in the terminal:

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 ) and run the init command:

This command will scaffold a new project and create the following files:

To integrate the package with Xcode we can generate associated files with the following:

Adding dependencies

Your project should now have a file that contains our project description, platform targets, and dependencies. Here is an example file:

Notice the empty array. That is where we can add Swift packages from GitHub URLs. Each package take the form of:

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: