@@ -15,6 +15,7 @@ import SendGrid
1515/// Emailing service
1616public protocol MailerService : Service {
1717 func send( _ message: Mailer . Message , on req: Request ) throws -> Future < Mailer . Result >
18+ func send( _ messages: [ Mailer . Message ] , on req: Request ) throws -> Future < [ ( Mail , Mailer . Result ) ] >
1819}
1920
2021
@@ -91,17 +92,17 @@ public class Mailer: MailerService {
9192 let mailgunClient = try req. make ( Mailgun . self)
9293 return try mailgunClient. send ( message. asMailgunContent ( ) , on: req) . map ( to: Mailer . Result. self) { _ in
9394 return Mailer . Result. success
94- } . catchMap ( { error in
95- return Mailer . Result. failure ( error: error)
95+ } . catchMap ( { error in
96+ return Mailer . Result. failure ( error: error)
9697 }
9798 )
9899 case . sendGrid( _) :
99100 let email = message. asSendGridContent ( )
100101 let sendGridClient = try req. make ( SendGridClient . self)
101102 return try sendGridClient. send ( [ email] , on: req. eventLoop) . map ( to: Mailer . Result. self) { _ in
102103 return Mailer . Result. success
103- } . catchMap ( { error in
104- return Mailer . Result. failure ( error: error)
104+ } . catchMap ( { error in
105+ return Mailer . Result. failure ( error: error)
105106 }
106107 )
107108 case . smtp( let smtp) :
@@ -119,4 +120,23 @@ public class Mailer: MailerService {
119120 }
120121 }
121122
123+ /// Send multiple messages using a provider defined in `config: Config`
124+ public func send( _ messages: [ Message ] , on req: Request ) throws -> Future < [ ( Mail , Mailer . Result ) ] > {
125+ switch config {
126+ case . mailgun:
127+ throw Abort ( . notImplemented, reason: " Sending mass email using Mailgun is not currently supported. " )
128+ case . sendGrid( _) :
129+ throw Abort ( . notImplemented, reason: " Sending mass email using SendGrid is not currently supported. " )
130+ case . smtp( let smtp) :
131+ let promise = req. eventLoop. newPromise ( [ ( Mail , Mailer . Result ) ] . self)
132+ smtp. send ( messages. map { $0. asSmtpMail ( ) } , progress: nil ) { mails, errors in
133+ let errors = errors. map { ( $0. 0 , Mailer . Result. failure ( error: $0. 1 ) ) }
134+ let successes = mails. map { ( $0, Mailer . Result. success) }
135+ promise. succeed ( result: errors + successes)
136+ }
137+ return promise. futureResult
138+ default :
139+ throw Abort ( . serviceUnavailable, reason: " Mails could not be sent: you need to configure your mail service. " )
140+ }
141+ }
122142}
0 commit comments