Swift-SMTP

Swift-SMTP bird

Swift SMTP 客户端。

Build Status macOS Linux Apache 2

特性

Swift 版本

macOS & Linux: Swift 5.2 或更高版本。

安装

您可以使用 Swift Package ManagerSwiftSMTP 添加到您的项目中。如果您的项目没有 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/Kitura/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: "user@gmail.com",        // 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: "drlight@gmail.com")
let megaman = Mail.User(name: "Megaman", email: "megaman@gmail.com")

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)
    }
}

添加抄送和密送

let roll = Mail.User(name: "Roll", email: "roll@gmail.com")
let zero = Mail.User(name: "Zero", email: "zero@gmail.com")

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
    }
)

致谢

灵感来自 HedwigPerfect-SMTP

许可证

Apache v2.0