SMTP

public struct SMTP

Used to connect to an SMTP server and send emails.

  • TLSMode enum for what form of connection security to enforce.

    See more

    Declaration

    Swift

    public enum TLSMode
  • Initializes an SMTP instance.

    Note

    Note:

    • You may need to enable access for less secure apps for your account on the SMTP server.
    • Some servers like Gmail support IPv6, and if your network does not, you will first attempt to connect via IPv6, then timeout, and fall back to IPv4. You can avoid this by disabling IPv6 on your machine.

    Declaration

    Swift

    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)

    Parameters

    hostname

    Hostname of the SMTP server to connect to, i.e. smtp.example.com.

    email

    Username to log in to server.

    password

    Password to log in to server, or access token if using XOAUTH2 authorization method.

    port

    Port to connect to the server on. Defaults to 465.

    tlsMode

    TLSMode enum indicating what form of connection security to use.

    tlsConfiguration

    TLSConfiguration used to connect with TLS. If nil, a configuration with no backing certificates is used. See TLSConfiguration for other configuration options.

    authMethods

    AuthMethods to use to log in to the server. If blank, tries all supported methods.

    domainName

    Client domain name used when communicating with the server. Defaults to localhost.

    timeout

    How long to try connecting to the server to before returning an error. Defaults to 10 seconds.

  • Send an email.

    Declaration

    Swift

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

    Parameters

    mail

    Mail object to send.

    completion

    Callback when sending finishes. Error is nil on success. (optional)

  • Send multiple emails.

    Note

    Note:

    • Each call to send will first log in to your server, attempt to send the mails, then closes the connection. Pass in an array of Mails to send them all in one session.
    • If any of the email addresses in a Mail‘s to, cc, or bcc are invalid, the entire mail will not send and return an SMTPError.
    • If an individual Mail fails while sending an array of Mails, the whole sending process will continue until all pending Mails are attempted.
    • Each call to send queues it’s Mails and sends them one by one. To send Mails concurrently, send them in separate calls to send.

    Declaration

    Swift

    public func send(_ mails: [Mail],
                     progress: Progress = nil,
                     completion: Completion = nil)

    Parameters

    mails

    Array of Mails to send.

    progress

    (Mail, Error) callback after each Mail is sent. Mail is the mail sent and Error is the error if it failed. (optional)

    completion

    ([Mail], [(Mail, Error)]) callback after all Mails have been attempted. [Mail] is an array of successfully sent Mails. [(Mail, Error)] is an array of failed Mails and their corresponding Errors. (optional)