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?", @["[email protected]"]) let smtpConn = newSmtp(useSsl = true, debug=true) smtpConn.connect("smtp.gmail.com", Port 465) smtpConn.auth("username", "password") smtpConn.sendmail("[email protected]", @["[email protected]"], $msg)
For SSL support this module relies on OpenSSL. If you want to enable SSL, compile with -d:ssl
.
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: [].}
proc createMessage(mSubject, mBody: string; mTo, mCc: seq[string] = @[]): Message {...}{. raises: [], tags: [].}
proc `$`(msg: Message): string {...}{.raises: [], tags: [].}
Message
. proc newSmtp(useSsl = false; debug = false; sslContext = defaultSslContext): Smtp {...}{. raises: [OSError, SslError], tags: [].}
Smtp
instance. proc newAsyncSmtp(useSsl = false; debug = false; sslContext = defaultSslContext): AsyncSmtp {...}{. raises: [OSError, Exception, SslError], tags: [RootEffect].}
AsyncSmtp
instance. proc connect(smtp: AsyncSmtp; address: string; port: Port): Future[void] {...}{. raises: [FutureError], tags: [RootEffect].}
proc connect(smtp: Smtp; address: string; port: Port) {...}{. raises: [OSError, SslError, TimeoutError, ReplyError], tags: [ReadIOEffect, TimeEffect, WriteIOEffect].}
proc auth(smtp: AsyncSmtp; username, password: string): Future[void] {...}{. raises: [FutureError], tags: [RootEffect].}
proc auth(smtp: Smtp; username, password: string) {...}{. raises: [SslError, OSError, TimeoutError, ReplyError], tags: [WriteIOEffect, ReadIOEffect, TimeEffect].}
proc sendMail(smtp: AsyncSmtp; fromAddr: string; toAddrs: seq[string]; msg: string): Future[ void] {...}{.raises: [FutureError], tags: [RootEffect].}
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].}
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): Future[void] {...}{.raises: [FutureError], tags: [RootEffect].}
proc close(smtp: Smtp) {...}{.raises: [SslError, OSError], tags: [WriteIOEffect].}
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/smtp.html