Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
.package(url: "https://github.com/vapor-community/sendgrid-kit.git", from: "1.0.0"),
.package(url: "https://github.com/vapor-community/sendgrid-kit.git", .branch("vapor-email")),
.package(url: "https://github.com/vapor/email.git", .branch("master"))
],
targets: [
.target(name: "SendGrid", dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "SendGridKit", package: "sendgrid-kit"),
.product(name: "Email", package: "email"),
]),
.testTarget(name: "SendGridTests", dependencies: ["SendGrid"])
]
Expand Down
42 changes: 0 additions & 42 deletions Sources/SendGrid/Application+SendGrid.swift

This file was deleted.

54 changes: 54 additions & 0 deletions Sources/SendGrid/Emails+SendGrid.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Vapor
import SendGridKit
import Email

extension Application.Emails.Provider {
static func sendgrid(apiKey: String) -> Self {
.init { app in
app.emails.use {
SendGridClient(httpClient: $0.http.client.shared, eventLoop: $0.eventLoopGroup.next(), apiKey: apiKey)
}
}
}
}


//extension Application {
// public struct Sendgrid {
// private final class Storage {
// let apiKey: String
//
// init(apiKey: String) {
// self.apiKey = apiKey
// }
// }
//
// private struct Key: StorageKey {
// typealias Value = Storage
// }
//
// private var storage: Storage {
// if self.application.storage[Key.self] == nil {
// self.initialize()
// }
// return self.application.storage[Key.self]!
// }
//
// public func initialize() {
// guard let apiKey = Environment.process.SENDGRID_API_KEY else {
// fatalError("No sendgrid API key provided")
// }
//
// self.application.storage[Key.self] = .init(apiKey: apiKey)
// }
//
// fileprivate let application: Application
//
// public var client: SendGridClient {
// .init(httpClient: self.application.http.client.shared, apiKey: self.storage.apiKey)
// }
// }
//
// public var sendgrid: Sendgrid { .init(application: self) }
//}

64 changes: 64 additions & 0 deletions Sources/SendGrid/Sendgrid+Email.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Vapor
import Email
import SendGridKit

extension SendGridClient: EmailClient {
public func send(_ emails: [VaporEmail]) -> EventLoopFuture<Void> {
self.send(emails: emails.map { convert($0) })
}

public func send(_ email: VaporEmail) -> EventLoopFuture<Void> {
return self.send(email: convert(email))
}

private func convert(_ email: VaporEmail) -> SendGridEmail {
let personalization = Personalization(
to: email.to.sendgrid(),
cc: email.cc?.sendgrid(),
bcc: email.bcc?.sendgrid(),
subject: email.subject
)

var content = [[
"type": "text/plain",
"value": email.text
]]

if let html = email.html {
content.append([
"type": "text/html",
"value": html
])
}

var sendgridEmail = SendGridEmail(
personalizations: [personalization],
from: .init(email: email.from.email, name: email.from.name),
subject: email.subject,
content: content,
attachments: nil
)

if let replyTo = email.replyTo {
sendgridEmail.replyTo = .init(email: replyTo.email, name: replyTo.name)
}

return sendgridEmail
}

public func delegating(to eventLoop: EventLoop) -> EmailClient {
return self.hopped(to: eventLoop)
}
}

extension Array where Element == Email.EmailAddress {
func sendgrid() -> [SendGridKit.EmailAddress]? {
guard !self.isEmpty else {
return nil
}

return self.map {
SendGridKit.EmailAddress(email: $0.email, name: $0.name)
}
}
}