Skip to content

Commit

Permalink
Merge pull request #13 from LinusGeffarth/master
Browse files Browse the repository at this point in the history
Added bulk-email feature for SMTP
  • Loading branch information
rafiki270 authored May 8, 2020
2 parents 3126558 + e3a19e9 commit 3c80669
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
9 changes: 6 additions & 3 deletions Sources/MailCore/Extensions/Request+Mail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ public struct MailProperty {
return try mailer.send(message, on: request)
}

/// Send email
public func send(_ messages: [Mailer.Message]) throws -> EventLoopFuture<[(Mail, Mailer.Result)]> {
let mailer = try request.make(MailerService.self)
return try mailer.send(messages, on: request)
}

/// Send email
public func send(from: String, to: String, subject: String, text: String) throws -> EventLoopFuture<Mailer.Result> {
return try send(Mailer.Message(from: from, to: to, subject: subject, text: text))
}

}


extension Request {

/// Mail functionality accessor for request
public var mail: MailProperty {
return MailProperty(request: self)
}

}
28 changes: 24 additions & 4 deletions Sources/MailCore/MailCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import SendGrid
/// Emailing service
public protocol MailerService: Service {
func send(_ message: Mailer.Message, on req: Request) throws -> Future<Mailer.Result>
func send(_ messages: [Mailer.Message], on req: Request) throws -> Future<[(Mail, Mailer.Result)]>
}


Expand Down Expand Up @@ -91,17 +92,17 @@ public class Mailer: MailerService {
let mailgunClient = try req.make(Mailgun.self)
return try mailgunClient.send(message.asMailgunContent(), on: req).map(to: Mailer.Result.self) { _ in
return Mailer.Result.success
}.catchMap({ error in
return Mailer.Result.failure(error: error)
}.catchMap({ error in
return Mailer.Result.failure(error: error)
}
)
case .sendGrid(_):
let email = message.asSendGridContent()
let sendGridClient = try req.make(SendGridClient.self)
return try sendGridClient.send([email], on: req.eventLoop).map(to: Mailer.Result.self) { _ in
return Mailer.Result.success
}.catchMap({ error in
return Mailer.Result.failure(error: error)
}.catchMap({ error in
return Mailer.Result.failure(error: error)
}
)
case .smtp(let smtp):
Expand All @@ -119,4 +120,23 @@ public class Mailer: MailerService {
}
}

/// Send multiple messages using a provider defined in `config: Config`
public func send(_ messages: [Message], on req: Request) throws -> Future<[(Mail, Mailer.Result)]> {
switch config {
case .mailgun:
throw Abort(.notImplemented, reason: "Sending mass email using Mailgun is not currently supported.")
case .sendGrid(_):
throw Abort(.notImplemented, reason: "Sending mass email using SendGrid is not currently supported.")
case .smtp(let smtp):
let promise = req.eventLoop.newPromise([(Mail, Mailer.Result)].self)
smtp.send(messages.map { $0.asSmtpMail() }, progress: nil) { mails, errors in
let errors = errors.map { ($0.0, Mailer.Result.failure(error: $0.1)) }
let successes = mails.map { ($0, Mailer.Result.success) }
promise.succeed(result: errors + successes)
}
return promise.futureResult
default:
throw Abort(.serviceUnavailable, reason: "Mails could not be sent: you need to configure your mail service.")
}
}
}

0 comments on commit 3c80669

Please sign in to comment.