Adds a mail backend for SendGrid to the Vapor web framework. Send simple emails, or leverage the full capabilities of SendGrid's V3 API.
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)
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)
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)
}