by IBM-Swift

IBM-Swift /Swift-SMTP

Swift SMTP client

138 Stars 36 Forks Last release: 12 months ago (5.1.2) Apache License 2.0 280 Commits 24 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


Swift-SMTP bird

Swift SMTP client.

Build Status macOS Linux Apache 2


  • Connect securely through SSL/TLS when needed
  • Authenticate with CRAM-MD5, LOGIN, PLAIN, or XOAUTH2
  • Send emails with local file, HTML, and raw data attachments
  • Add custom headers
  • Documentation

Swift Version

macOS & Linux:

Swift 4.0.3
Swift 4.1
Swift 4.1.2


You can add

to your project using Swift Package Manager. If your project does not have a
file, create one by running
swift package init
in the root directory of your project. Then open
and add
as a dependency. Be sure to add it to your desired targets as well:
// 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"]), ] )

After adding the dependency and saving, run

swift package generate-xcodeproj
in the root directory of your project. This will fetch dependencies and create an Xcode project which you can open and begin editing.

Migration Guide


brings breaking changes. See the quick migration guide here.


Initialize an

import SwiftSMTP

let smtp = SMTP( hostname: "smtp.gmail.com", // SMTP server address email: "[email protected]", // username to login password: "password" // password to login )


Additional parameters of

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)

By default, the

struct connects on port
and sends mail only if a TLS connection can be established. It also uses a
that uses no backing certificates. View the docs for more configuration options.

Send email

Create a

object and use your
handle to send it. To set the sender and receiver of an email, use the
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) } }

Add Cc and 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." )


Send attachments

Create an

, attach it to your
, and send it through the
handle. Here's an example of how you can send the three supported types of attachments--a local file, HTML, and raw data:
// 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: "Here's an image: ", // 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 Attachments 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 */

Send multiple mails

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



Inspired by Hedwig and Perfect-SMTP.


Apache v2.0

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.