vapor 发送email
2018-10-05 本文已影响5人
搞好关系
- 导入库
/// mail Server
.package(url: "https://github.com/IBM-Swift/Swift-SMTP.git", from: "4.0.1"),
- 邮件发送
router.get("email", String.parameter) { req -> EventLoopFuture<HTTPResponseStatus> in
let password = ""
let fromEmail = ""
let smtp: SMTP = SMTP.init(hostname: "smtp.163.com", email: fromEmail, password: password)
let fromUser = Mail.User(name: "注册码确认邮件", email: fromEmail)
let email = try req.parameters.next(String.self)
let toUser = Mail.User.init(email: email)
let mail = Mail(from: fromUser
, to: [toUser]
, cc: [], bcc: []
, subject: "欢迎®️"
, text: "您的注册码是\(VerfiyCodeRender.renderInstance.default)"
, attachments: []
, additionalHeaders: [:])
let result = req.eventLoop.newPromise(Bool.self)
smtp.send(mail, completion: { (error) in
print(error as Any)
result.succeed(result: error == nil)
})
// HTTPResponseStatus
return result.futureResult.map({ (b) -> HTTPResponseStatus in
return b ? .ok : .expectationFailed
})
}