# 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 Nim's SMTP",
                            "Hello!.\n Is this awesome or what?",
                            @["foo@gmail.com"])
    let smtpConn = newSmtp(useSsl = true, debug=true)
    smtpConn.connect("smtp.gmail.com", Port 465)
    smtpConn.auth("username", "password")
    smtpConn.sendmail("username@gmail.com", @["foo@gmail.com"], $msg)

Example for startTls use:

    var msg = createMessage("Hello from Nim's SMTP",
                            "Hello!.\n Is this awesome or what?",
                            @["foo@gmail.com"])
    let smtpConn = newSmtp(debug=true)
    smtpConn.connect("smtp.mailtrap.io", Port 2525)
    smtpConn.startTls()
    smtpConn.auth("username", "password")
    smtpConn.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`.

# インポート

net, strutils, strtabs, base64, os, asyncnet, asyncdispatch, parsecfg

# 型

    Message = object
      msgTo: seq[string]
      msgCc: seq[string]
      msgSubject: string
      msgOtherHeaders: StringTableRef
      msgBody: string

  ソース 編集

    ReplyError = object of IOError

  ソース 編集

    Smtp = SmtpBase[Socket]

  ソース 編集

    AsyncSmtp = SmtpBase[AsyncSocket]

  ソース 編集

# プロシージャ

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

  MIME 準拠メッセージを新規作成します。 ソース 編集

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

  Alternate version of the above. ソース 編集

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

  stringify for `Message`. ソース 編集

    proc newSmtp(useSsl = false; debug = false; sslContext: SslContext = nil): Smtp {...}{.
        raises: [OSError, SslError, Exception, IOError],
        tags: [RootEffect, ReadDirEffect].}

  新しい `Smtp` インスタンスを作成します。 ソース 編集

    proc newAsyncSmtp(useSsl = false; debug = false; sslContext: SslContext = nil): AsyncSmtp {...}{.
        raises: [OSError, Exception, SslError, IOError],
        tags: [RootEffect, ReadDirEffect].}

  新しい `AsyncSmtp` インスタンスを作成します。 ソース 編集

    proc connect(smtp: AsyncSmtp; address: string; port: Port): owned(Future[void]) {...}{.
        raises: [Exception, FutureError], tags: [RootEffect].}

  Establishes a connection with a SMTP server. May fail with ReplyError or with a socket error. ソース 編集

    proc connect(smtp: Smtp; address: string; port: Port) {...}{.
        raises: [OSError, SslError, TimeoutError, ReplyError],
        tags: [ReadIOEffect, TimeEffect, WriteIOEffect].}

  Establishes a connection with a SMTP server. May fail with ReplyError or with a socket error. ソース 編集

    proc startTls(smtp: AsyncSmtp; sslContext: SslContext = nil): owned(Future[void]) {...}{.
        raises: [Exception, FutureError], tags: [RootEffect, ReadDirEffect].}

  Put the SMTP connection in TLS (Transport Layer Security) mode. May fail with ReplyError Source Edit

    proc startTls(smtp: Smtp; sslContext: SslContext = nil) {...}{.
        raises: [SslError, OSError, TimeoutError, ReplyError, Exception, IOError],
        tags: [WriteIOEffect, ReadIOEffect, TimeEffect, RootEffect, ReadDirEffect].}

  Put the SMTP connection in TLS (Transport Layer Security) mode. May fail with ReplyError Source Edit

    proc auth(smtp: AsyncSmtp; username, password: string): owned(Future[void]) {...}{.
        raises: [Exception, FutureError], tags: [RootEffect].}

  Sends an AUTH command to the server to login as the `username` using `password`. May fail with ReplyError. ソース 編集

    proc auth(smtp: Smtp; username, password: string) {...}{.
        raises: [SslError, OSError, TimeoutError, ReplyError],
        tags: [WriteIOEffect, ReadIOEffect, TimeEffect].}

  Sends an AUTH command to the server to login as the `username` using `password`. May fail with ReplyError. ソース 編集

    proc sendMail(smtp: AsyncSmtp; fromAddr: string; toAddrs: seq[string]; msg: string): owned(
        Future[void]) {...}{.raises: [Exception, FutureError], tags: [RootEffect].}

  Sends `msg` from `fromAddr` to the addresses specified in `toAddrs`. Messages may be formed using `createMessage` by converting the Message into a string. ソース 編集

    proc sendMail(smtp: Smtp; fromAddr: string; toAddrs: seq[string]; msg: string) {...}{.
        raises: [SslError, OSError, TimeoutError, ReplyError],
        tags: [WriteIOEffect, ReadIOEffect, TimeEffect].}

  Sends `msg` from `fromAddr` to the addresses specified in `toAddrs`. Messages may be formed using `createMessage` by converting the Message into a string. ソース 編集

    proc close(smtp: AsyncSmtp): owned(Future[void]) {...}{.raises: [Exception, FutureError],
        tags: [RootEffect].}

  SMTP サーバから切断してソケットを閉じます。 ソース 編集

    proc close(smtp: Smtp) {...}{.raises: [SslError, OSError], tags: [WriteIOEffect].}

  SMTP サーバから切断してソケットを閉じます。 ソース 編集

# エクスポート

Port