smtp API reference
An SMTP send client: the line-oriented command/response dialogue of RFC 5321, over the net system library (plaintext, implicit TLS, or STARTTLS) with SASL AUTH: PLAIN, LOGIN, XOAUTH2, CRAM-MD5, and SCRAM-SHA-1 / SCRAM-SHA-256. The message body is any string, typically built by the mime module. Because it uses net, this module runs on the default jennifer binary only (jennifer-tiny stubs the network stack). send (the one-shot convenience) throws a catchable smtp.SmtpError (kind "smtp") when the server rejects a command: its code (4xx temporary, 5xx permanent), RFC 3463 enhancedCode and the command. To deliver a queue of messages over a single TLS + auth handshake, open a persistent Session, sendOn each message, and close. TLS certificate verification is the net default. An IDN host or envelope domain is IDNA-encoded to its xn-- form (via the idna module); a non-ASCII address local part still throws (it needs SMTPUTF8).
Import with import "smtp.j" as smtp;. See the smtp guide for prose and examples.
Contents
| Name | What it is |
|---|---|
close | Close a session: send QUIT (best-effort) and close the socket. |
open | Open an authenticated SMTP session: connect per opts.security, read the greeting, EHLO, upgrade with STAR... |
send | Deliver message (a full RFC 5322 message, e.g. |
sendOn | Deliver one message (a full RFC 5322 message, e.g. |
Options | struct: Connection settings for an SMTP session. |
Session | struct: A live, authenticated SMTP connection: the handshake (greeting, EHLO, STARTTLS, and AUTH) has run once, so ... |
SmtpError | struct: A failed SMTP exchange. |
Functions
close
smtp.close(session as Session)Close a session: send QUIT (best-effort) and close the socket. Call it once; the session's open field is not changed (value semantics), and a later sendOn on it throws kind "closed".
session- the session to close
open
smtp.open(opts as Options)Open an authenticated SMTP session: connect per opts.security, read the greeting, EHLO, upgrade with STARTTLS when asked (refusing a server that did not advertise it - anti-downgrade), and authenticate when credentials are present. The returned Session is ready for one or more sendOn calls, so a queue of N messages pays this TLS + auth handshake once instead of N times.
opts- the connection and auth settings
- Returns
Session - an authenticated, ready session
- Throws
SmtpError - kind "smtp" if the connection, greeting, STARTTLS, or auth fails (including a SCRAM 235 that skips the server-final, whose signature was then never verified)
- Throws
Error - kind "closed" when the server closes the connection before a complete reply
send
smtp.send(
opts as Options,
from as string,
recipients as list of string,
message as string)Deliver message (a full RFC 5322 message, e.g. from mime.encode) to every recipient, with from as the envelope sender - the one-shot convenience: open a session, sendOn this one message, and close. To send several messages over one handshake, call open / sendOn / close yourself.
opts- the connection and auth settings
from- the envelope sender address
recipients- the envelope recipient addresses
message- the full RFC 5322 message body
- Throws
SmtpError - kind "smtp" when the server rejects a command or an address is not a valid ASCII RFC 5321 address
- Throws
Error - kind "closed" when the server closes the connection before a complete reply
sendOn
smtp.sendOn(
session as Session,
from as string,
recipients as list of string,
message as string)Deliver one message (a full RFC 5322 message, e.g. from mime.encode) over an open session: RSET, MAIL FROM / RCPT TO / DATA. The session is reusable afterwards (call sendOn again for the next message; close when done).
session- an open session (from
open) from- the envelope sender address
recipients- the envelope recipient addresses
message- the full RFC 5322 message body
- Throws
SmtpError - kind "smtp" when the server rejects a command or an address is not a valid ASCII RFC 5321 address (or
openis false) - Throws
Error - kind "closed" when the session was closed or the server closed the connection before a complete reply
Structs
Options
Connection settings for an SMTP session.
def struct Options {
host as string,
port as int,
security as transport.Security,
clientName as string,
user as string,
pass as string,
auth as string,
allowInsecureAuth as bool
};host- the mail server hostname (IDNA-encoded when non-ASCII)
port- the server port (e.g. 25, 465, 587)
securitytransport.Security.None(plaintext),.Tls(implicit TLS on connect), or.Starttls(upgrade after EHLO)clientName- the EHLO identity (defaults to "localhost" when empty)
user- the SASL username (empty means no auth)
pass- the SASL password, or the OAuth2 access token for xoauth2
auth- the SASL mechanism: "" (default - no auth when
useris empty, else PLAIN), "auto" (negotiate the strongest mechanism the server's EHLO advertises, falling back to PLAIN), "plain", "login", "xoauth2", "cram" (CRAM-MD5), "scram-sha-1", or "scram-sha-256" allowInsecureAuth- force SASL auth over an unencrypted ("none") connection (default false - credentials are refused over plaintext)
Session
A live, authenticated SMTP connection: the handshake (greeting, EHLO, STARTTLS, and AUTH) has run once, so sendOn can deliver many messages over it. Value-semantic, but the conn handle is shared across copies (a net.Conn), so the socket survives being passed to sendOn. Build with open; retire with close. close cannot reach the caller's copy, so open stays true afterwards; a sendOn on a closed session throws kind "closed" (the socket is gone).
def struct Session {
conn as net.Conn,
open as bool
};conn- the underlying (possibly TLS-upgraded) socket, shared across copies
open- true for a session from
open;sendOnrefuses one built with false
SmtpError
A failed SMTP exchange. Every error of kind "smtp" is one, so its fields are safe to read once the kind matches. A server's rejection carries its reply code: 4xx is temporary (temporary is true; retry later), 5xx permanent. A connection that closes before a complete reply throws the generic kind "closed" instead: the command's outcome is unknown.
def struct SmtpError {
kind as string,
message as string,
code as int,
enhancedCode as string,
command as string,
temporary as bool
};kind- always
"smtp" message<command>: <code> <reply text>, or what was refused locallycode- the server's reply code (such as 550); 0 for a local check
enhancedCode- the RFC 3463 enhanced status code at the start of the reply text (such as
5.1.1), "" when there is none command- the command the server rejected (
MAIL FROM,RCPT TO,DATA, ...), "" for a local check temporary- true for a 4xx reply: the same message may be accepted later