func sendEmail()

in NIOSMTP/NIOSMTP/ViewController.swift [26:61]


func sendEmail(_ email: Email,
               group: EventLoopGroup,
               communicationHandler: @escaping (String) -> Void,
               queue: DispatchQueue,
               _ handler: @escaping (Error?) -> Void) {
    let emailSentPromise: EventLoopPromise<Void> = group.next().makePromise()

    let bootstrap: NIOClientTCPBootstrap
    do {
        bootstrap = try configureBootstrap(group: group,
                                           email: email,
                                           emailSentPromise: emailSentPromise,
                                           communicationHandler: communicationHandler)
    } catch {
        queue.async {
            handler(error)
        }
        return
    }

    let connection = bootstrap.connect(host: Configuration.shared.serverConfig.hostname,
                                       port: Configuration.shared.serverConfig.port)

    connection.cascadeFailure(to: emailSentPromise)
    emailSentPromise.futureResult.map {
        connection.whenSuccess { $0.close(promise: nil) }
        queue.async {
            handler(nil)
        }
    }.whenFailure { error in
        connection.whenSuccess { $0.close(promise: nil) }
        queue.async {
            handler(error)
        }
    }
}