/sendgrid-provider

SendGrid-powered mail backend for Vapor

Primary LanguageSwift

SendGrid Provider for Vapor

Swift Vapor CircleCI

Adds a mail backend for SendGrid to the Vapor web framework. Send simple emails, or leverage the full capabilities of SendGrid's V3 API.

Setup

Add the dependency to Package.swift:

.package(url: "https://github.com/vapor-community/sendgrid-provider.git", from: "3.0.0")

Register the config and the provider.

let config = SendGridConfig(apiKey: "SG.something")

services.register(config)

try services.register(SendGridProvider())

app = try Application(services: services)

sendGridClient = try app.make(SendGridClient.self)

Using the API

You can use all of the available parameters here to build your SendGridEmail Usage in a route closure would be as followed:

import SendGrid

let email = SendGridEmail()
let sendGridClient = try req.make(SendGridClient.self)

try sendGridClient.send([email], on: req.eventLoop)

Error handling

If the request to the API failed for any reason a SendGridError is thrown and has an errors property that contains an array of errors returned by the API. Simply ensure you catch errors thrown like any other throwing function

do {
    try sendgridClient.send(...)
}
catch let error as SendGridError {
    print(error)
}