Package -

SendGrid-Swift

swift4 macOS iOS Linux*

This library allows you to quickly and easily send emails through SendGrid using Swift.

Important: Version 1.0.0 Breaking Changes

Versions 1.0.0 and higher have been migrated to Swift 4 and adds Linux support, which contains code-breaking API changes.

Previous Breaking Changes

  • Versions 0.2.0 and higher uses Swift 3, which introduces breaking changes from previous versions.
  • Versions 0.1.0 and higher have been migrated over to use SendGrid's V3 Mail Send Endpoint, which contains code-breaking changes.

*A Note About Linux

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. While this library uses what has been implemented to make the HTTP requests, critical implementations such as invalidating the session are unavailable, which could lead to unexpected behaviors such as memory leaks. This being said, Linux supported in this library should be treated as experimental.

Full Documentation

Full documentation of the library is available here.

Table Of Contents

Installation

With Cocoapods

Add the following to your Podfile:

pod 'SendGrid', :git => 'https://github.com/scottkawai/sendgrid-swift.git'

Swift Package Manager

Add the following to your Package.swift:

// swift-tools-version:4.0

import PackageDescription

let package = Package(
    name: "MyApp",
    dependencies: [
        .package(
            url: "https://github.com/scottkawai/sendgrid-swift.git",
            from: "1.0.0"
        )
    ],
    targets: [
        .target(
            name: "MyApp",
            dependencies: ["SendGrid"])
    ]
)

Note! Make sure you also list "SendGrid" as a dependency in the "targets" section of your manifest.

As A Submodule

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.

Usage

Authorization

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)

API Calls

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:

Development

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 Ubuntu 16.04 with Swift 4 installed. 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>.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-fancy-new-feature)
  3. Commit your changes (git commit -am 'Added fancy new feature')
  4. Write tests for any changes and ensure existing tests pass
    • Note! Be sure that your changes also work on Linux. You can use the Docker container to quickly test this by running docker-compose run --rm app swift test
  5. Push to the branch (git push origin my-fancy-new-feature)
  6. Create a new Pull Request

License

The MIT License (MIT)

Copyright (c) 2017 Scott K.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Github

link
Stars:

Advertisement