前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vapor smtp发送邮件provider

vapor smtp发送邮件provider

作者头像
大话swift
发布2019-07-04 11:03:11
1.1K0
发布2019-07-04 11:03:11
举报
文章被收录于专栏:大话swift

最经几天一直在拜读Vapor中service的源码,连续几天上班途中和下班之后都是在翻看源码,从刚开始感觉云山雾绕到逐渐清晰

)

而最终内部的所有被注册的server都会通过下面来进行实例化注册到系统中来使用

看了这么久我们根据最新发送邮件的需求自己写个service,在此我们通过Provider来实现(基于IBM开源的邮件发送服务:https://github.com/IBM-Swift/Swift-SMTP)

在此我们分为**SKSmtpProvider**和**SKSmtpConfig**两部分,其中SKSmtpConfig为SMTP的信息配置项而SKSmtpProvider为真实的Server实现部分

先上Provider的代码

```

class SKSmtpProvider: Provider {

func register(_ services: inout Services) throws {

services.register { (container) -> SKSmtp in

let config = try container.make(SKSmtpConfig.self)

assert(config != nil, "选注册SKSmtpConfig实例,才能使用SKSmtp")

return SKSmtp.init(config: config)

}

}

func didBoot(_ container: Container) throws -> EventLoopFuture<Void> {

let result = container.eventLoop.newPromise(Void.self)

result.succeed()

return result.futureResult

}

}

```

对于使用Provider来实现service则必须实现```Provider 协议```

![Provider官方注释]

然后是我们SMTP的server实现,对于service协议,就只是一个类型声明:```public protocol Service {}```

```

class SKSmtp : Service {

fileprivate var smtp: SMTP?

init(config: SKSmtpConfig) {

self.smtp = SMTP.init(hostname: config.hostname, email: config.email, password: config.password, port: config.port, useTLS: config.useTLS, tlsConfiguration: config.tlsConfiguration, authMethods:config.authMethods, domainName: config.domainName, timeout: config.timeout)

}

/// Send an email.

///

/// - Parameters:

/// - mail: `Mail` object to send.

/// - completion: Callback when sending finishes. `Error` is nil on success. (optional)

public func send(_ mail: Mail, completion: ((Error?) -> Void)? = nil) {

smtp?.send(mail, completion: completion)

}

public func send(_ mails: [Mail],

progress: Progress = nil,

completion: Completion = nil) {

smtp?.send(mails, progress: progress, completion: completion)

}

}

```

最后是Config,很简单Config其实就是SMTP的配置信息

```

public struct SKSmtpConfig : Service{

var hostname: String

var email: String

var password: String

var port: Int32 = 465

var useTLS: Bool = false

var tlsConfiguration: SwiftSMTP.TLSConfiguration?

var authMethods:[ AuthMethod] = []

var domainName: String = "localhost"

var timeout: UInt = 10

init(hostname: String, email: String, password: String, port: Int32 = 465, useTLS:Bool = false,tlsConfiguration:SwiftSMTP.TLSConfiguration? = nil, authMethods:[ AuthMethod] = [], domainName: String = "localhost", timeout: UInt = 10) {

self.hostname = hostname

self.email = email

self.password = password

self.port = port

self.useTLS = useTLS

self.tlsConfiguration = tlsConfiguration

self.domainName = domainName

self.timeout = timeout

}

}

```

所有的都已经实现了,接下来就是使用了

1 系统的configure中注册对应的Provider

```

let smtpConfig = SKSmtpConfig.init(hostname: "", email: "", password: "")

services.register(smtpConfig)

try services.register(SKSmtpProvider())

```

2 在网络请求中实现

```

public func regist(req: Request)throws-> EventLoopFuture<String>{

let smtp: SKSmtp = try req.make(SKSmtp.self)

smtp.send(<#T##mail: Mail##Mail#>, completion: <#T##((Error?) -> Void)?##((Error?) -> Void)?##(Error?) -> Void#>)

```

**Vapor集成使用**

```

.package(url: "https://github.com/skeyboy/SKSmtp.git", from:"0.0.1")

```

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-10-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大话swift 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档