# Module smtp

This module implements the SMTP client protocol as specified by RFC 5321, this can be used to send mail to any SMTP Server.

This module also implements the protocol used to format messages, as specified by RFC 2822.

Example gmail use:

    var msg = createMessage("Hello from Nimrod's SMTP",
                            "Hello!.\n Is this awesome or what?",
                            @["foo@gmail.com"])
    var smtp = connect("smtp.gmail.com", 465, True, True)
    smtp.auth("username", "password")
    smtp.sendmail("username@gmail.com", @["foo@gmail.com"], $msg)

For SSL support this module relies on OpenSSL. If you want to enable SSL, compile with `-d:ssl`.

# Imports

sockets, strutils, strtabs, base64, os

# Types

    TSMTP = object
      sock: TSocket
      debug: bool

    TMessage = object
      msgTo: seq[string]
      msgCc: seq[string]
      msgSubject: string
      msgOtherHeaders: PStringTable
      msgBody: string

    EInvalidReply = object of EIO

# Procs

    proc connect(address: string; port = 25; ssl = false; debug = false): TSMTP {.
        raises: [ESSL, EOS, EIO, ETimeout, EInvalidReply],
        tags: [FReadDir, FReadIO, FTime, FWriteIO].}

  Establishes a connection with a SMTP server. May fail with EInvalidReply or with a socket error.

    proc auth(smtp: var TSMTP; username, password: string) {.
        raises: [ESSL, EOS, ETimeout, EInvalidReply],
        tags: [FWriteIO, FReadIO, FTime].}

  Sends an AUTH command to the server to login as the username using password. May fail with EInvalidReply.

    proc sendmail(smtp: var TSMTP; fromaddr: string; toaddrs: seq[string];
                  msg: string) {.raises: [ESSL, EOS, ETimeout, EInvalidReply],
                                 tags: [FWriteIO, FReadIO, FTime].}

  Sends msg from fromaddr to toaddr. Messages may be formed using `createMessage` by converting the TMessage into a string.

    proc close(smtp: TSMTP) {.raises: [ESSL, EOS], tags: [FWriteIO].}

  Disconnects from the SMTP server and closes the socket.

    proc createMessage(mSubject, mBody: string; mTo, mCc: seq[string];
                       otherHeaders: openarray[tuple[name, value: string]]): TMessage {.
        raises: [], tags: [].}

  Creates a new MIME compliant message.

    proc createMessage(mSubject, mBody: string; mTo, mCc: seq[string] = @ []): TMessage {.
        raises: [], tags: [].}

  Alternate version of the above.

    proc `$`(msg: TMessage): string {.raises: [], tags: [].}

  stringify for `TMessage`.

Generated: 2013-05-21 09:55:15 UTC