Skip to content

Commit

Permalink
Add proper Vapor Provider for cleaner Config integration (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
vzsg authored and bygri committed Jun 21, 2017
1 parent 885d3fb commit ad2a6d6
Show file tree
Hide file tree
Showing 20 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Package.swift
@@ -1,7 +1,7 @@
import PackageDescription

let package = Package(
name: "SendGrid",
name: "SendGridProvider",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2),
]
Expand Down
38 changes: 28 additions & 10 deletions README.md
Expand Up @@ -2,16 +2,16 @@

![Swift](http://img.shields.io/badge/swift-3.1-brightgreen.svg)
![Vapor](http://img.shields.io/badge/vapor-2.0-brightgreen.svg)
[![CircleCI](https://circleci.com/gh/vapor-community/sendgrid-provider.svg?style=svg)](https://circleci.com/gh/vapor-community/sendgrid-provider)
[![CircleCI](https://circleci.com/gh/vapor-community/sendgrid-provider.svg?style=shield)](https://circleci.com/gh/vapor-community/sendgrid-provider)

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:

```JSON
.Package(url: "https://github.com/vapor-community/sendgrid-provider.git", majorVersion: 2, minor: 1)
```swift
.Package(url: "https://github.com/vapor-community/sendgrid-provider.git", majorVersion: 2)
```

Add a configuration file named `sendgrid.json` with the following format:
Expand All @@ -22,16 +22,27 @@ Add a configuration file named `sendgrid.json` with the following format:
}
```

Set your Droplet to use SendGrid:
Register the provider with the configuration system:

```swift
import Vapor
import SendGrid
import SendGridProvider

let drop = try Droplet(
config: config,
mail: SendGrid(config: config)
)
extension Config {
/// Configure providers
private func setupProviders() throws {
...
try addProvider(SendGridProvider.Provider.self)
}
}
```

And finally, change the Droplet's mail implementation by editing `droplet.json`:

```js
{
"mail": "sendgrid",
// other configuration keys redacted for brevity
}
```

## Sending simple emails
Expand All @@ -40,16 +51,23 @@ SendGrid can act as a drop-in replacement for Vapor's built-in SMTP support.
Simply make use of `drop.mail`:

```swift
import SMTP

let email = Email(from: , to: , subject: , body: )
try drop.mail.send(email)
```

Don't forget to `import SMTP` if you need to work with `Email` or
`EmailAddress` objects.

## Sending complex emails

Use the `SendGridEmail` class to fully configure your email, including open
and click tracking, templating, and multiple recipients.

```swift
import SendGridProvider

if let sendgrid = drop.mail as? SendGrid {
let email = SendGridEmail()
try sendgrid.send(email)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions Sources/SendGridProvider/Provider.swift
@@ -0,0 +1,14 @@
import Vapor

public final class Provider: Vapor.Provider {
public static let repositoryName = "sendgrid-provider"

public init(config: Config) throws { }

public func boot(_ config: Config) throws {
config.addConfigurable(mail: SendGrid.init, name: "sendgrid")
}

public func boot(_ drop: Droplet) throws { }
public func beforeRun(_ drop: Droplet) {}
}
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions Tests/LinuxMain.swift
@@ -1,10 +1,10 @@
#if os(Linux)

import XCTest
@testable import SendGridTests
@testable import SendGridProviderTests

XCTMain([
testCase(SendGridTests.allTests),
testCase(SendGridProviderTests.allTests),
])

#endif
@@ -1,11 +1,11 @@
import XCTest
import SendGrid
import SendGridProvider
import SMTP
@testable import Vapor

// Test inbox: https://www.mailinator.com/inbox2.jsp?public_to=vapor-sendgrid

class SendGridTests: XCTestCase {
class SendGridProviderTests: XCTestCase {
static let allTests = [
("testDroplet", testDroplet),
("testSend", testSend),
Expand All @@ -15,16 +15,17 @@ class SendGridTests: XCTestCase {

func testDroplet() throws {
let config: Config = try [
"droplet": [
"mail": "sendgrid"
],
"sendgrid": [
"apiKey": apiKey
],
].makeNode(in: nil).converted()
let drop = try Droplet(
config: config,
mail: SendGrid(config: config)
)
try config.addProvider(Provider.self)
let drop = try Droplet(config)
guard let _ = drop.mail as? SendGrid else {
XCTFail()
XCTFail("drop.mail is \(drop.mail)")
return
}
}
Expand Down

0 comments on commit ad2a6d6

Please sign in to comment.