@@ -16,7 +16,7 @@ Mailing wrapper for multiple mailing services like MailGun, SendGrig or SMTP
1616
1717- [x] MailGun
1818- [x] SendGrid
19- - [ ] SMTP
19+ - [x ] SMTP
2020
2121## Install
2222
@@ -26,24 +26,64 @@ Just add following line package to your `Package.swift` file.
2626.package (url : " https://github.com/LiveUI/MailCore.git" , .branch (" master" ))
2727```
2828
29- ## Use
29+ ## Usage
3030
3131Usage is really simple. First register the service in your apps ` configure ` method:
3232
33+ #### Mailgun
34+
3335``` swift
34- // Mailgun
3536let config = Mailer.Config .mailgun (key : " {mailGunApi}" , domain : " {mailGunDomain}" )
37+ ```
38+
39+ #### SendGrid
3640
37- // or SendGrid
41+ ``` swift
3842let config = Mailer.Config .sendGrid (key : " {sendGridApiKey}" )
43+ ```
44+
45+ #### SMTP
46+
47+ Use the ` SMTP ` struct as a handle to your SMTP server:
48+
49+ ``` swift
50+ let smtp = SMTP (hostname : " smtp.gmail.com" , // SMTP server address
51+ email :
" [email protected] " ,
// username to login 52+ password : " password" ) // password to login
53+
54+ let config = Mailer.Config .smtp (smtp)
55+ ```
3956
40- // Register and configure the service
57+ <b >Using TLS:</b >
58+
59+ All parameters of ` SMTP ` struct:
60+
61+ ``` swift
62+ let smtp = SMTP (hostname : String ,
63+ email : String ,
64+ password : String ,
65+ port : Int32 = 465 ,
66+ useTLS : Bool = true ,
67+ tlsConfiguration : TLSConfiguration? = nil ,
68+ authMethods : [AuthMethod] = [],
69+ accessToken : String ? = nil ,
70+ domainName : String = " localhost" ,
71+ timeout : UInt = 10 )
72+
73+ let config = Mailer.Config .smtp (smtp)
74+ ```
75+
76+ #### Register service
77+
78+ Register and configure the service.
79+
80+ ``` swift
4181Mailer (config : config, registerOn : & services)
4282```
4383
4484` Mailer.Config ` is an ` enum ` and you can choose from any integrated services to be used
4585
46- And send an email:
86+ #### Send an email
4787
4888``` swift
4989let mail
= Mailer.
Message (
from :
" [email protected] " ,
to :
" [email protected] " ,
subject :
" Oil spill" ,
text :
" Oooops I did it again" ,
html :
" <p>Oooops I did it again</p>" )
@@ -53,6 +93,53 @@ return try req.mail.send(mail).flatMap(to: Response.self) { mailResult in
5393}
5494```
5595
96+ ## Testing
97+
98+ Mailcore provides a ` MailCoreTestTools ` framework which you can import into your tests to get ` MailerMock ` .
99+
100+ To register, and potentially override any existing "real" Mailer service, just initialize ` MailerMock ` with your services.
101+
102+ ``` swift
103+ // Register
104+ MailerMock (services : & services)
105+
106+ // Retrieve in your tests
107+ let mailer = try ! req.make (MailerService.self ) as! MailerMock
108+ ```
109+
110+ ` MailerMock ` will store the last used result as well as the received message and request. Structure of the moct can be seen below:
111+
112+ ``` swift
113+ public class MailerMock : MailerService {
114+
115+ public var result: Mailer.Result = .success
116+ public var receivedMessage: Mailer.Message?
117+ public var receivedRequest: Request?
118+
119+ // MARK: Initialization
120+
121+ @discardableResult public init (services : inout Services) {
122+ services.remove (type : Mailer.self )
123+ services.register (self , as : MailerService.self )
124+ }
125+
126+ // MARK: Public interface
127+
128+ public func send (_ message : Mailer.Message, on req : Request) throws -> Future<Mailer.Result> {
129+ receivedMessage = message
130+ receivedRequest = req
131+ return req.eventLoop .newSucceededFuture (result : result)
132+ }
133+
134+ public func clear () {
135+ result = .success
136+ receivedMessage = nil
137+ receivedRequest = nil
138+ }
139+
140+ }
141+ ```
142+
56143## Support
57144
58145Join our [ Slack] ( http://bit.ly/2B0dEyt ) , channel <b >#help-boost</b > to ... well, get help :)
@@ -70,6 +157,12 @@ Core package for <b>[Boost](http://www.boostappstore.com)</b>, a completely open
70157* [ DBCore] ( https://github.com/LiveUI/DbCore/ ) - Set of tools for work with PostgreSQL database
71158* [ VaporTestTools] ( https://github.com/LiveUI/VaporTestTools ) - Test tools and helpers for Vapor 3
72159
160+ ## Implemented thirdparty providers
161+
162+ * <b >MailGun</b > - https://github.com/twof/VaporMailgunService
163+ * <b >SendGrig</b > - https://github.com/vapor-community/sendgrid-provider
164+ * <b >SMTP</b > - https://github.com/IBM-Swift/Swift-SMTP
165+
73166## Code contributions
74167
75168We 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.
0 commit comments