Hedwig

public struct Hedwig

Hedwig is the manager type of Hedwig framework. You can initialize an instance with common SMTP config and then send an email with it.

  • Initialize a Hedwig instance with SMTP config.

    Note

    Initializing a Hedwig instance will not do the actual connecting work. It will not try to connect to server until you send a mail.

    Declaration

    Swift

    public init(hostName: String,
                    user: String?,
                    password: String?,
                    port: Port? = nil,
                    secure: SMTP.Secure = .tls,
                    validation: SMTP.Validation = .default,
                    domainName: String = "localhost",
                    authMethods: [SMTP.AuthMethod] =
                            [.plain, .cramMD5, .login, .xOauth2])

    Parameters

    hostName

    Hostname of your SMTP server. The host name should not include any scheme. For example, smtp.example.com is a valid host name.

    user

    User name used to auth with SMTP server. Pass nil to this parameter if there is no need to auth with your server.

    password

    Password used to auth with SMTP server. Pass nil to this parameter if there is no need to auth with your server.

    port

    Port number which Hedwig should connect to. Default is nil, which means Hedwig will determine the port for you according to secure parameter and use the standard port.

    secure

    Security level used when communicating with server. Default is .tls.

    validation

    Validation used when setup a secure connection with server.

    domainName

    The clinet domain name used when communicating with server. Default is localhost

    authMethods

    Auth methods accepted in client when auth with server. By default all auth methods (.plain, .cramMD5, .login, .xOauth2) in Hedwig are supported.

  • Send a single email.

    Declaration

    Swift

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

    Parameters

    mail

    The email which will be sent.

    completion

    Callback when sending finishes, with an optional Error to indicate whether there is an error happened while sending. If the mail is sent successfully, callback parameter would be nil.

  • Send an array of emails.

    Note

    Note:

    • If a failure is encountered when while sending multiple mails, the whole sending process will not stop until all pending mails are sent. Each mail sending will trigger an invocation of progress, and when all mails sending finish, completion handler will be called.
    • The parameter of progress block contains the mail and an optional Error. If the mail is sent successfully, the error parameter would be nil. Otherwise, it contains the error type.
    • The first parameter of completion is an array of sucessully sent mails, while the second is an array of failed mails and corresponding errors for each.
    • This method will queue the mails and send them one by one. If you need to send mails in a concurrent way, call send(_:progress:completion:) again with another array of mails.

    Declaration

    Swift

    public func send(_ mails: [Mail],
                         progress: ((Mail, Error?) -> Void)? = nil,
                         completion: (
                          (_ sent: [Mail],
                           _ failed: [(mail: Mail, error: Error)]) -> Void)? = nil)

    Parameters

    mails

    The emails which will be sent.

    progress

    Callback when an email sending finishes.

    completion

    Callback when all emails sending finished.