Mailing wrapper for multiple mailing services like Mailgun, SendGrid or SMTP
- Mailgun
- SendGrid
- SMTP
- Attachments
- Multiple emails sent at the same time
- Multiple recipint, CC & BCC fields
Just add following line package to your Package.swift file.
.package(url:"https://github.com/LiveUI/MailCore.git",.branch("master"))Usage is really simple mkey!
First create your client configuration:
letconfig=Mailer.Config.mailgun(key:"{mailgunApi}", domain:"{mailgunDomain}", region:"{mailgunRegion}")letconfig=Mailer.Config.sendGrid(key:"{sendGridApiKey}")Use the SMTP struct as a handle to your SMTP server:
letsmtp=SMTP(hostname:"smtp.gmail.com", // SMTP server address
email:"user@gmail.com", // username to login
password:"password") // password to login
letconfig=Mailer.Config.smtp(smtp)All parameters of SMTP struct:
letsmtp=SMTP(hostname: String,
email: String,
password: String,
port: Int32 =465,
useTLS: Bool =true,
tlsConfiguration: TLSConfiguration?=nil,
authMethods:[AuthMethod]=[],
accessToken: String?=nil,
domainName: String ="localhost",
timeout: UInt =10)letconfig=Mailer.Config.smtp(smtp)Register and configure the service in your apps configure method.
Mailer(config: config, registerOn:&services)Mailer.Config is an enum and you can choose from any integrated services to be used
letmail=Mailer.Message(from:"admin@liveui.io", to:"bobby.ewing@southfork.com", subject:"Oil spill", text:"Oooops I did it again", html:"<p>Oooops I did it again</p>")
return try req.mail.send(mail).flatMap(to:Response.self){ mailResult inprint(mailResult)
// ... Return your response for example
}Mailcore provides a MailCoreTestTools framework which you can import into your tests to get MailerMock.
To register, and potentially override any existing "real" Mailer service, just initialize MailerMock with your services.
// Register
MailerMock(services:&services)
// Retrieve in your tests
letmailer=try! req.make(MailerService.self)as!MailerMockMailerMock will store the last used result as well as the received message and request. Structure of the moct can be seen below:
publicclassMailerMock:MailerService{publicvarresult:Mailer.Result=.success
publicvarreceivedMessage:Mailer.Message?publicvarreceivedRequest:Request?
// MARK: Initialization
@discardableResultpublicinit(services:inoutServices){
services.remove(type:Mailer.self)
services.register(self, as:MailerService.self)}
// MARK: Public interface
publicfunc send(_ message:Mailer.Message, on req:Request)throws->Future<Mailer.Result>{
receivedMessage = message
receivedRequest = req
return req.eventLoop.newSucceededFuture(result: result)}publicfunc clear(){
result =.success
receivedMessage =nil
receivedRequest =nil}}Join our Slack, channel #help-boost to ... well, get help :)
Core package for Einstore, a completely open source enterprise AppStore written in Swift!
- Website: http://www.einstore.io
- Github: https://github.com/Einstore/Einstore
- EinstoreCore - AppStore core module
- ApiCore - Base user & team management including forgotten passwords, etc ...
- Mailgun - https://github.com/twof/VaporMailgunService
- SendGrig - https://github.com/vapor-community/sendgrid-provider
- SMTP - https://github.com/IBM-Swift/Swift-SMTP
We love PR’s, we can’t get enough of them ... so if you have an interesting improvement, bug-fix or a new feature please don’t hesitate to get in touch. If you are not sure about something before you start the development you can always contact our dev and product team through our Slack.
Ondrej Rafaj (@rafiki270 on Github, Twitter, LiveUI Slack and Vapor Slack)
MIT license, please see LICENSE file for more details.
