A library that allows you to quickly and easily send emails through SendGrid using Swift.
This library allows you to quickly and easily send emails through SendGrid using Swift.
Versions 2.0.0 and higher have been migrated to Swift 5. Some existing classes, properties, and functions have been renamed or removed. Deprecation warnings will populate where appropriate.
Version 2 of this library re-architects how requests are sent. Previously a Request
instance housed it’s API parameters alongside other properties. Now, Request
instances hold all their API-related parameters in a new parameters
property. The parameters
property is an Encodable
instance, which simplifies how a request transforms its properties into the API parameters. In addition, the Session
class’s callback now utilize’s Swift 5’s Result
enum to provide back either the API response or any errors that arose.
Previous Breaking Changes
While this library does function on Linux via the Swift Package Manager, it relies upon the open source Foundation library (specifically URLSession
). As it stands, URLSession
hasn’t been fully implemented yet. This library uses what has been implemented to make the HTTP requests, but critical implementations such as invalidating the session are unavailable, which could lead to unexpected behaviors such as memory leaks. That being said, Linux supported in this library should be treated as experimental.
Full documentation of the library is available here.
Add the SendGrid module to the “dependencies” and “targets” sections of your Package.swift file like so:
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(
url: "https://github.com/scottkawai/sendgrid-swift.git",
from: "2.2.1"
)
],
targets: [
.target(
name: "MyApp",
dependencies: ["SendGrid"]
)
]
)
Note! Make sure you also list “SendGrid” as a dependency in the “targets” section of your manifest.
Add the following to your Podfile:
pod 'SendGrid', :git => 'https://github.com/scottkawai/sendgrid-swift.git'
Add this repo as a submodule to your project and update:
cd /path/to/your/project
git submodule add https://github.com/scottkawai/sendgrid-swift.git
This will add a sendgrid-swift
folder to your directory. Next, you need to add all the Swift files under /sendgrid-swift/Sources/
to your project.
The V3 endpoint supports authorization via API keys (preferred) and basic authentication via your SendGrid username and password (Note: the Mail Send API only allows API keys). Using the Session
class, you can configure an instance with your authorization method to be used over and over again to send email requests.
It is also highly recommended that you do not hard-code any credentials in your code. If you’re running this on Linux, it’s recommended that you use environment variables instead, like so:
///
/// Assuming you set your SendGrid API key as an environment variable
/// named "SG_API_KEY"...
///
let session = Session()
guard let myApiKey = ProcessInfo.processInfo.environment["SG_API_KEY"] else {
print("Unable to retrieve API key")
return
}
session.authentication = Authentication.apiKey(myApiKey)
///
/// Alternatively `Session` has a singleton instance that you can
/// configure once and reuse throughout your code.
///
/// Session.shared.authentication = Authentication.apiKey(myApiKey)
All the available API calls are located in their own folders under the ./Sources/SendGrid/API
folder, and each one has its own README explaining how to use it. Below is a list of the currently available API calls:
If you’re developing on macOS, you an generate an Xcode project by running the following:
cd /path/to/sendgrid-swift
swift package generate-xcodeproj
This project also contains a Dockerfile and a docker-compose.yml file which runs Swift 5. Running docker-compose up
will execute the swift build
command in the Linux container. If you want to run other commands, you can run docker-compose run --rm app <command>
.
git checkout -b my-fancy-new-feature
)git commit -am 'Added fancy new feature'
)swift test --generate-linuxmain && docker-compose run --rm app swift test
git push origin my-fancy-new-feature
)