Swift-SMTP
Swift SMTP 客户端。
功能
- 需要时通过 SSL/TLS 安全连接
- 通过 CRAM-MD5、LOGIN、PLAIN 或 XOAUTH2 进行身份验证
- 使用本地文件、HTML 和原始数据附件发送电子邮件
- 添加自定义头
- 文档
Swift 版本
macOS & Linux: Swift 4.0.3
, Swift 4.1
和 Swift 4.1.2
安装
您可以使用 Swift Package Manager 来将 SwiftSMTP
添加到您的项目中。如果您的项目没有 Package.swift
文件,请您在项目根目录下运行 swift package init
命令来创建一个。然后打开 Package.swift
文件,将 SwiftSMTP
加为一个依赖项。请确保将其添加到您期望的目标中。
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "MyProject",
products: [
.library(
name: "MyProject",
targets: ["MyProject"]),
],
dependencies: [
.package(url: "https://github.com/IBM-Swift/Swift-SMTP", .upToNextMinor(from: "5.1.0")), // add the dependency
],
targets: [
.target(
name: "MyProject",
dependencies: ["SwiftSMTP"]), // add targets
.testTarget( // note "SwiftSMTP" (NO HYPHEN)
name: "MyProjectTests",
dependencies: ["MyProject"]),
]
)
在添加依赖并保存后,在项目根目录下运行 swift package generate-xcodeproj
。这将检索依赖并创建一个 Xcode 项目,您可以打开并开始编辑。
迁移指南
版本 5.0.0
带来了破坏性更改。请参阅快速迁移指南 这里。
用法
初始化一个 SMTP
实例
import SwiftSMTP
let smtp = SMTP(
hostname: "smtp.gmail.com", // SMTP server address
email: "[email protected]", // username to login
password: "password" // password to login
)
TLS
SMTP
结构的额外参数
public init(hostname: String,
email: String,
password: String,
port: Int32 = 587,
tlsMode: TLSMode = .requireSTARTTLS,
tlsConfiguration: TLSConfiguration? = nil,
authMethods: [AuthMethod] = [],
domainName: String = "localhost",
timeout: UInt = 10)
默认情况下,SMTP
结构在端口 587
上连接,并且只有建立 TLS 连接时才发送邮件。它还使用一个不使用辅助证书的 TLSConfiguration
。查看 文档 以获取更多配置选项。
发送电子邮件
创建一个 Mail
对象,并使用您的 SMTP
处理程序发送它。要设置电子邮件的发送者和接收者,请使用 User
结构。
let drLight = Mail.User(name: "Dr. Light", email: "[email protected]")
let megaman = Mail.User(name: "Megaman", email: "[email protected]")
let mail = Mail(
from: drLight,
to: [megaman],
subject: "Humans and robots living together in harmony and equality.",
text: "That was my ultimate wish."
)
smtp.send(mail) { (error) in
if let error = error {
print(error)
}
}
添加Cc和Bcc
let roll = Mail.User(name: "Roll", email: "[email protected]")
let zero = Mail.User(name: "Zero", email: "[email protected]")
let mail = Mail(
from: drLight,
to: [megaman],
cc: [roll],
bcc: [zero],
subject: "Robots should be used for the betterment of mankind.",
text: "Any other use would be...unethical."
)
smtp.send(mail)
发送附件
创建一个 Attachment
,将其附加到您的 Mail
,并通过 SMTP
处理程序发送。以下是一个示例,说明您如何发送三种受支持的附件类型--本地文件、HTML 和原始数据。
// Create a file `Attachment`
let fileAttachment = Attachment(
filePath: "~/img.png",
// "CONTENT-ID" lets you reference this in another attachment
additionalHeaders: ["CONTENT-ID": "img001"]
)
// Create an HTML `Attachment`
let htmlAttachment = Attachment(
htmlContent: "<html>Here's an image: <img src=\"cid:img001\"/></html>",
// To reference `fileAttachment`
related: [fileAttachment]
)
// Create a data `Attachment`
let data = "{\"key\": \"hello world\"}".data(using: .utf8)!
let dataAttachment = Attachment(
data: data,
mime: "application/json",
name: "file.json",
// send as a standalone attachment
inline: false
)
// Create a `Mail` and include the `Attachment`s
let mail = Mail(
from: from,
to: [to],
subject: "Check out this image and JSON file!",
// The attachments we created earlier
attachments: [htmlAttachment, dataAttachment]
)
// Send the mail
smtp.send(mail)
/* Each type of attachment has additional parameters for further customization */
发送多封邮件
let mail1: Mail = //...
let mail2: Mail = //...
smtp.send([mail1, mail2],
// This optional callback gets called after each `Mail` is sent.
// `mail` is the attempted `Mail`, `error` is the error if one occured.
progress: { (mail, error) in
},
// This optional callback gets called after all the mails have been sent.
// `sent` is an array of the successfully sent `Mail`s.
// `failed` is an array of (Mail, Error)--the failed `Mail`s and their corresponding errors.
completion: { (sent, failed) in
}
)
致谢
灵感来源于 Hedwig 和 Perfect-SMTP。
许可
Apache v2.0