|
1 |
| -# SendGrid Provider for Vapor |
2 |
| - |
3 |
| -<p align="center"> |
4 |
| - <a href="https://github.com/vapor-community/sendgrid/actions"> |
5 |
| - <img src="https://github.com/vapor-community/sendgrid/workflows/test/badge.svg" alt="Continuous Integration"> |
| 1 | +<div align="center"> |
| 2 | + <img src="https://avatars.githubusercontent.com/u/26165732?s=200&v=4" width="100" height="100" alt="avatar" /> |
| 3 | + <h1>SendGrid</h1> |
| 4 | + <a href="https://swiftpackageindex.com/vapor-community/sendgrid/documentation"> |
| 5 | + <img src="https://design.vapor.codes/images/readthedocs.svg" alt="Documentation"> |
| 6 | + </a> |
| 7 | + <a href="https://discord.gg/vapor"><img src="https://design.vapor.codes/images/discordchat.svg" alt="Team Chat"></a> |
| 8 | + <a href="LICENSE"><img src="https://design.vapor.codes/images/mitlicense.svg" alt="MIT License"></a> |
| 9 | + <a href="https://github.com/vapor-community/sendgrid/actions/workflows/test.yml"> |
| 10 | + <img src="https://img.shields.io/github/actions/workflow/status/vapor-community/sendgrid/test.yml?event=push&style=plastic&logo=github&label=tests&logoColor=%23ccc" alt="Continuous Integration"> |
| 11 | + </a> |
| 12 | + <a href="https://codecov.io/github/vapor-community/sendgrid"> |
| 13 | + <img src="https://img.shields.io/codecov/c/github/vapor-community/sendgrid?style=plastic&logo=codecov&label=codecov"> |
6 | 14 | </a>
|
7 | 15 | <a href="https://swift.org">
|
8 |
| - <img src="http://img.shields.io/badge/swift-5.2-brightgreen.svg" alt="Swift 5.2"> |
| 16 | + <img src="https://design.vapor.codes/images/swift60up.svg" alt="Swift 6.0+"> |
9 | 17 | </a>
|
10 |
| -</p> |
| 18 | +</div> |
| 19 | +<br> |
| 20 | + |
| 21 | +📧 SendGrid library for the Vapor web framework, based on [SendGridKit](https://github.com/vapor-community/sendgrid-kit). |
| 22 | + |
| 23 | +Send simple emails, or leverage the full capabilities of [SendGrid's V3 API](https://www.twilio.com/docs/sendgrid/api-reference/mail-send/mail-send). |
| 24 | + |
| 25 | +### Getting Started |
11 | 26 |
|
12 |
| -Adds a mail backend for SendGrid to the Vapor web framework. Send simple emails, |
13 |
| -or leverage the full capabilities of SendGrid's V3 API. |
| 27 | +Use the SPM string to easily include the dependendency in your `Package.swift` file |
14 | 28 |
|
15 |
| -## Setup |
16 |
| -Add the dependency to Package.swift: |
| 29 | +```swift |
| 30 | +.package(url: "https://github.com/vapor-community/sendgrid.git", from: "6.0.0-rc.1") |
| 31 | +``` |
17 | 32 |
|
18 |
| -~~~~swift |
19 |
| -.package(url: "https://github.com/vapor-community/sendgrid.git", from: "4.0.0") |
20 |
| -~~~~ |
| 33 | +and add it to your target's dependencies: |
21 | 34 |
|
22 |
| -Make sure `SENDGRID_API_KEY` is set in your environment. This can be set in the |
23 |
| -Xcode scheme, or specified in your `docker-compose.yml`, or even provided as |
24 |
| -part of a `swift run` command. |
| 35 | +```swift |
| 36 | +.product(name: "SendGrid", package: "sendgrid") |
| 37 | +``` |
25 | 38 |
|
26 |
| -Optionally, explicitly initialize the provider (this is strongly recommended, as |
27 |
| -otherwise a missing API key will cause a fatal error some time later in your |
28 |
| -application): |
| 39 | +## Overview |
29 | 40 |
|
30 |
| -~~~~swift |
31 |
| -app.sendgrid.initialize() |
32 |
| -~~~~ |
| 41 | +> [!WARNING] |
| 42 | +> Make sure that the `SENDGRID_API_KEY` variable is set in your environment. |
| 43 | +This can be set in the Xcode scheme, or specified in your `docker-compose.yml`, or even provided as part of a `swift run` command. |
| 44 | +A missing API key will result in a fatal error. |
33 | 45 |
|
34 |
| -Now you can access the client at any time: |
35 |
| -~~~~swift |
36 |
| -app.sendgrid.client |
37 |
| -~~~~ |
| 46 | +### Using the API |
38 | 47 |
|
39 |
| -## Using the API |
| 48 | +You can use all of the available parameters here to build your `SendGridEmail`. |
40 | 49 |
|
41 |
| -You can use all of the available parameters here to build your `SendGridEmail` |
42 | 50 | Usage in a route closure would be as followed:
|
43 | 51 |
|
44 |
| -~~~~swift |
| 52 | +```swift |
45 | 53 | import SendGrid
|
46 | 54 |
|
47 | 55 | let email = SendGridEmail(…)
|
| 56 | +try await req.sendgrid.client.send(email) |
| 57 | +``` |
48 | 58 |
|
49 |
| -try await req.application.sendgrid.client.send(email) |
50 |
| -~~~~ |
| 59 | +### Error handling |
51 | 60 |
|
52 |
| -## Error handling |
| 61 | +If the request to the API failed for any reason a `SendGridError` is thrown, which has an `errors` property that contains an array of errors returned by the API. |
53 | 62 |
|
54 |
| -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: |
| 63 | +Simply ensure you catch errors thrown like any other throwing function. |
55 | 64 |
|
56 |
| -~~~~swift |
| 65 | +```swift |
57 | 66 | do {
|
58 |
| - try await req.application.sendgrid.client.send(email) |
| 67 | + try await req.sendgrid.client.send(email) |
59 | 68 | } catch let error as SendGridError {
|
60 |
| - req.logger.error("\(error)") |
| 69 | + req.logger.error("\(error.errors)") |
61 | 70 | }
|
62 |
| -~~~~ |
| 71 | +``` |
0 commit comments