Skip to content

Commit fa6e92c

Browse files
authored
Merge pull request #9 from Mikroservices/feature/application-send
Add possibility to use Request and Application
2 parents 650f1fa + cb13da3 commit fa6e92c

File tree

4 files changed

+69
-18
lines changed

4 files changed

+69
-18
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ let email = Email(from: EmailAddress(address: "[email protected]", name: "John
6969
subject: "The subject (text)",
7070
body: "This is email body.")
7171

72-
request.send(email).map { result in
72+
request.smtp.send(email).map { result in
7373
switch result {
7474
case .success:
7575
print("Email has been sent")
@@ -79,12 +79,20 @@ request.send(email).map { result in
7979
}
8080
```
8181

82+
Also you can send emails directly via `application` class.
83+
84+
```swift
85+
app.smtp.send(email).map { result in
86+
...
87+
}
88+
```
89+
8290
## Troubleshoots
8391

8492
You can use `logHandler` to handle and print all messages send/retrieved from email server.
8593

8694
```swift
87-
request.send(email) { message in
95+
request.smtp.send(email) { message in
8896
print(message)
8997
}.map { result in
9098
...

Sources/Smtp/Request+Smtp.swift renamed to Sources/Smtp/Application+Send.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,19 @@ import Vapor
9797
/// `StartTlsHandler` is responsible for establishing SSL encryption after `STARTTLS`
9898
/// command (this handler adds dynamically `OpenSSLClientHandler` to the pipeline if
9999
/// server supports that encryption.
100-
public extension Request {
101-
100+
public extension Application.Smtp {
102101
/// Sending an email.
103102
///
104103
/// - parameters:
105104
/// - email: Email which will be send.
106105
/// - logHandler: Callback which can be used for logging/printing of sending status messages.
107106
/// - returns: An `EventLoopFuture<Result<Bool, Error>>` with information about sent email.
108107
func send(_ email: Email, logHandler: ((String) -> Void)? = nil) -> EventLoopFuture<Result<Bool, Error>> {
109-
let emailSentPromise: EventLoopPromise<Void> = self.eventLoop.makePromise()
108+
let emailSentPromise: EventLoopPromise<Void> = self.application.eventLoopGroup.next().makePromise()
110109
let configuration = self.application.smtp.configuration
111110

112111
// Client configuration
113-
let bootstrap = ClientBootstrap(group: self.eventLoop)
112+
let bootstrap = ClientBootstrap(group: self.application.eventLoopGroup.next())
114113
.connectTimeout(configuration.connectTimeout)
115114
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
116115
.channelInitializer { channel in
@@ -142,7 +141,7 @@ public extension Request {
142141
connection.whenSuccess { $0.close(promise: nil) }
143142
return Result.success(true)
144143
}.flatMapError { error -> EventLoopFuture<Result<Bool, Error>> in
145-
return self.eventLoop.makeSucceededFuture(Result.failure(error))
144+
return self.application.eventLoopGroup.future(Result.failure(error))
146145
}
147146
}
148147
}

Sources/Smtp/Request+Send.swift

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Foundation
2+
import NIO
3+
import NIOSSL
4+
import Vapor
5+
6+
public extension Request {
7+
var smtp: Smtp {
8+
.init(request: self)
9+
}
10+
11+
struct Smtp {
12+
let request: Request
13+
14+
func send(_ email: Email, logHandler: ((String) -> Void)? = nil) -> EventLoopFuture<Result<Bool, Error>> {
15+
return self.request.application.smtp.send(email, logHandler: logHandler)
16+
}
17+
}
18+
19+
@available(*, deprecated, message: "Function is depraceted and will be deleted in Smtp 3.0. Please use: request.smtp.send() instead.")
20+
func send(_ email: Email, logHandler: ((String) -> Void)? = nil) -> EventLoopFuture<Result<Bool, Error>> {
21+
return self.application.smtp.send(email, logHandler: logHandler)
22+
}
23+
}

Tests/SmtpTests/SmtpTests.swift

+32-11
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,36 @@ final class SmtpTests: XCTestCase {
3838
body: "This is email body.")
3939

4040
let request = Request(application: application, on: application.eventLoopGroup.next())
41-
try request.send(email) { message in
41+
try request.smtp.send(email) { message in
4242
print(message)
4343
}.flatMapThrowing { result in
4444
XCTAssertTrue(try result.get())
4545
}.wait()
46-
46+
4747
sleep(3)
4848
}
4949

50+
func testSendTextMessageViaApplication() throws {
51+
let application = Application()
52+
defer {
53+
application.shutdown()
54+
}
55+
56+
application.smtp.configuration = smtpConfiguration
57+
let email = Email(from: EmailAddress(address: "[email protected]", name: "John Doe"),
58+
to: [EmailAddress(address: "[email protected]", name: "Ben Doe")],
59+
subject: "The subject (text) - \(timestamp)",
60+
body: "This is email body.")
61+
62+
try application.smtp.send(email) { message in
63+
print(message)
64+
}.flatMapThrowing { result in
65+
XCTAssertTrue(try result.get())
66+
}.wait()
67+
68+
sleep(3)
69+
}
70+
5071
func testSendTextMessageWithoutNames() throws {
5172
let application = Application()
5273
defer {
@@ -60,7 +81,7 @@ final class SmtpTests: XCTestCase {
6081
body: "This is email body.")
6182

6283
let request = Request(application: application, on: application.eventLoopGroup.next())
63-
try request.send(email) { message in
84+
try request.smtp.send(email) { message in
6485
print(message)
6586
}.flatMapThrowing { result in
6687
XCTAssertTrue(try result.get())
@@ -83,7 +104,7 @@ final class SmtpTests: XCTestCase {
83104
isBodyHtml: true)
84105

85106
let request = Request(application: application, on: application.eventLoopGroup.next())
86-
try request.send(email) { message in
107+
try request.smtp.send(email) { message in
87108
print(message)
88109
}.flatMapThrowing { result in
89110
XCTAssertTrue(try result.get())
@@ -108,7 +129,7 @@ final class SmtpTests: XCTestCase {
108129
email.addAttachment(Attachment(name: "image.png", contentType: "image/png", data: Attachments.image()))
109130

110131
let request = Request(application: application, on: application.eventLoopGroup.next())
111-
try request.send(email) { message in
132+
try request.smtp.send(email) { message in
112133
print(message)
113134
}.flatMapThrowing { result in
114135
XCTAssertTrue(try result.get())
@@ -134,7 +155,7 @@ final class SmtpTests: XCTestCase {
134155
email.addAttachment(Attachment(name: "image.png", contentType: "image/png", data: Attachments.image()))
135156

136157
let request = Request(application: application, on: application.eventLoopGroup.next())
137-
try request.send(email) { message in
158+
try request.smtp.send(email) { message in
138159
print(message)
139160
}.flatMapThrowing { result in
140161
XCTAssertTrue(try result.get())
@@ -159,7 +180,7 @@ final class SmtpTests: XCTestCase {
159180
body: "This is email body.")
160181

161182
let request = Request(application: application, on: application.eventLoopGroup.next())
162-
try request.send(email) { message in
183+
try request.smtp.send(email) { message in
163184
print(message)
164185
}.flatMapThrowing { result in
165186
XCTAssertTrue(try result.get())
@@ -188,7 +209,7 @@ final class SmtpTests: XCTestCase {
188209
body: "This is email body.")
189210

190211
let request = Request(application: application, on: application.eventLoopGroup.next())
191-
try request.send(email) { message in
212+
try request.smtp.send(email) { message in
192213
print(message)
193214
}.flatMapThrowing { result in
194215
XCTAssertTrue(try result.get())
@@ -211,7 +232,7 @@ final class SmtpTests: XCTestCase {
211232
replyTo: EmailAddress(address: "[email protected]"))
212233

213234
let request = Request(application: application, on: application.eventLoopGroup.next())
214-
try request.send(email) { message in
235+
try request.smtp.send(email) { message in
215236
print(message)
216237
}.flatMapThrowing { result in
217238
XCTAssertTrue(try result.get())
@@ -236,7 +257,7 @@ final class SmtpTests: XCTestCase {
236257
email.addAttachment(Attachment(name: "image.png", contentType: "image/png", data: Attachments.image()))
237258

238259
let request = Request(application: application, on: application.eventLoopGroup.next())
239-
try request.send(email) { message in
260+
try request.smtp.send(email) { message in
240261
print(message)
241262
}.flatMapThrowing { result in
242263
XCTAssertTrue(try result.get())
@@ -259,7 +280,7 @@ final class SmtpTests: XCTestCase {
259280
email.addAttachment(Attachment(name: "image.png", contentType: "image/png", data: Attachments.image()))
260281

261282
let request = Request(application: application, on: application.eventLoopGroup.next())
262-
try request.send(email) { message in
283+
try request.smtp.send(email) { message in
263284
print(message)
264285
}.flatMapThrowing { result in
265286
XCTAssertTrue(try result.get())

0 commit comments

Comments
 (0)