Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move implementation to mailgun-kit and support vapor/email #50

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
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
23 changes: 13 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

// swift-tools-version:5.3
import PackageDescription

let package = Package(
Expand All @@ -9,26 +7,31 @@ let package = Package(
.macOS(.v10_15)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Mailgun",
targets: ["Mailgun"]),
targets: ["Mailgun"]
),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0")
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
.package(url: "https://github.com/vapor-community/mailgun-kit.git", .branch("v1")),
.package(url: "https://github.com/vapor/email.git", .branch("api")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Mailgun",
dependencies: [
.product(name: "Vapor", package: "vapor"),
]),
.product(name: "Email", package: "email"),
.product(name: "MailgunKit", package: "mailgun-kit"),
]
),
.testTarget(
name: "MailgunTests",
dependencies: [
.target(name: "Mailgun"),
]),
.product(name: "XCTVapor", package: "vapor"),
]
),
]
)
18 changes: 18 additions & 0 deletions Sources/Mailgun/Emails+Mailgun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Vapor
import Email
import MailgunKit

extension Application.Emails.Provider {
public static func mailgun(_ configuration: Mailgun.Configuration) -> Self {
.init { app in
app.emails.use {
LiveMailgunClient(
config: configuration,
eventLoop: $0.eventLoopGroup.next(),
httpClient: $0.http.client.shared,
logger: $0.logger
)
}
}
}
}
50 changes: 0 additions & 50 deletions Sources/Mailgun/Enums/Error.swift

This file was deleted.

5 changes: 0 additions & 5 deletions Sources/Mailgun/Enums/Region.swift

This file was deleted.

2 changes: 2 additions & 0 deletions Sources/Mailgun/Exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@_exported import MailgunKit
@_exported import Email
105 changes: 0 additions & 105 deletions Sources/Mailgun/Extensions/Mailgun+Application.swift

This file was deleted.

12 changes: 0 additions & 12 deletions Sources/Mailgun/Extensions/Mailgun+Encode.swift

This file was deleted.

21 changes: 0 additions & 21 deletions Sources/Mailgun/Extensions/Mailgun+ParseResponse.swift

This file was deleted.

22 changes: 0 additions & 22 deletions Sources/Mailgun/Extensions/Mailgun+PostRequest.swift

This file was deleted.

14 changes: 0 additions & 14 deletions Sources/Mailgun/Extensions/Mailgun+Request.swift

This file was deleted.

87 changes: 87 additions & 0 deletions Sources/Mailgun/LiveMailgunClient+EmailClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import Vapor
import MailgunKit
import Email
import NIO

public enum VaporMailgunError: Error {
case failedToSendAttachment(Vapor.File)
}

extension LiveMailgunClient: EmailClient {
public func send(_ messages: [EmailMessage]) -> EventLoopFuture<Void> {
do {
return try messages.map { message -> Mailgun.Message in
let html: String?
let text: String?

switch message.content {
case let .text(_text):
text = _text
html = nil
case let .html(_html):
html = _html
text = nil
case let .universal(_text, _html):
text = _text
html = _html
}

let attachments = try message.attachments?.toMailgunAttachments() ?? []
let inlines = try message.attachments?.toMailgunInlineAttachments() ?? []

return Mailgun.Message(
from: message.from.fullAddress,
to: message.to.map(\.mailgun),
replyTo: message.replyTo?.fullAddress,
cc: message.cc?.map(\.mailgun),
bcc: message.bcc?.map(\.mailgun),
subject: message.subject,
text: text ?? "",
html: html,
attachments: attachments,
inline: inlines
)
}
.map { self.sendRequest(.send($0)).transform(to: ()) }
.flatten(on: self.eventLoop)
} catch {
return self.eventLoop.future(error: error)
}
}
}

extension Collection where Element == EmailAttachment {
func toMailgunAttachments() throws -> [Mailgun.File] {
try self.reduce(into: []) { result, attachment in
guard case let .attachment(file) = attachment else { return }
return try result.append(file.toMailgunFile())
}
}

func toMailgunInlineAttachments() throws -> [Mailgun.File] {
try self.reduce(into: []) { result, attachment in
guard case let .inline(file) = attachment else { return }
return try result.append(file.toMailgunFile())
}
}
}

extension Vapor.File {
func toMailgunFile() throws -> Mailgun.File {
guard let contentType = self.contentType?.serialize() else {
throw VaporMailgunError.failedToSendAttachment(self)
}

return .init(
data: self.data,
filename: self.filename,
contentType: contentType
)
}
}

extension EmailAddress {
var mailgun: Mailgun.FullEmail {
(self.email, self.name)
}
}
Loading