smtp - send mail (SMTP client)
Import with import "smtp.j" as smtp;. An SMTP send client: the line-oriented command/response dialogue of RFC 5321 over the net system library, with plaintext / implicit-TLS / STARTTLS transport and SASL AUTH (PLAIN / LOGIN / XOAUTH2 / CRAM-MD5 / SCRAM-SHA-1 / SCRAM-SHA-256). The message body is any string, typically built by the mime module. Because it uses net, this module needs the default jennifer binary; on the stock jennifer-tiny a send raises a friendly error.
On
jennifer-tiny: "needs the defaultjenniferbinary" refers to the stock tiny build, which ships without a network driver - not a TinyGo limitation. Ajennifer-tinyrebuilt with a network stack runs this module too; see the note onnetand TinyGo.
import "smtp.j" as smtp;
import "mime.j" as mime;
import "transport.j" as transport;
def msg as mime.Part init mime.text("text/plain", "Hello!");
$msg = mime.withHeader($msg, "Subject", "Hi");
def opts as smtp.Options init smtp.Options{host: "mail.example.com", port: 587,
security: transport.Security.Starttls, clientName: "me.example.com",
user: "me@example.com", pass: "secret"};
smtp.send($opts, "me@example.com", ["you@example.com"], mime.encode($msg));Runnable: examples/modules/smtp_demo.j.
Surface
| Call / type | Notes |
|---|---|
smtp.Options | host, port, security, clientName, user, pass, auth, allowInsecureAuth. |
smtp.send(opts, from, recipients, message) | One-shot: open a session, deliver message, close. Throws on a server rejection. |
smtp.Session | A live, authenticated connection (conn, open) for delivering many messages over one handshake. |
smtp.open(opts) | Connect, greet, STARTTLS, and authenticate once; returns a ready Session. |
smtp.sendOn(session, from, recipients, message) | Deliver one message over an open session (RSET + MAIL / RCPT / DATA); reusable afterwards. |
smtp.close(session) | QUIT (best-effort) and close the session's socket. |
Options fields:
| Field | Notes |
|---|---|
host | Server hostname. |
port | Server port (25 / 587 plaintext or STARTTLS, 465 implicit TLS). |
security | A transport.Security: .None (plaintext), .Starttls (upgrade after EHLO), or .Tls (implicit TLS on connect). Needs import "transport.j" as transport;. |
clientName | The EHLO identity; defaults to "localhost" when empty. |
auth | SASL mechanism: "" (none when user is empty, else PLAIN), "auto" (negotiate the strongest mechanism EHLO advertises, falling back to PLAIN), "plain", "login", "xoauth2", "cram" (CRAM-MD5), "scram-sha-1", or "scram-sha-256" (via sasl; SASLprep is not applied). |
user | SASL username; "" with auth: "" skips authentication. |
pass | SASL password. |
allowInsecureAuth | Force SASL AUTH over an unencrypted (security: transport.Security.None) connection. Default false: credentials are refused over plaintext (use .Tls / .Starttls, or set this to force). |
What send does
One call runs the whole delivery, throwing a catchable SmtpError (kind "smtp") the moment the server rejects a step:
- Connect per
security(net.connect, ornet.connectTLSfor.Tls), bounded by a connection-establishment timeout. - Read the
220greeting, sendEHLO. - For
.Starttls: confirm theEHLOresponse advertisedSTARTTLS(an anti-downgrade check - a MITM that strips the capability to keep the session in plaintext is refused), thenSTARTTLS,net.startTLS, and a secondEHLO. - Authenticate per
auth(via thesaslmechanisms):AUTH PLAIN, theAUTH LOGINtwo-step,AUTH XOAUTH2(an OAuth2 bearer token inpass- how Google / Microsoft 365 authenticate),AUTH CRAM-MD5, or the SCRAM challenge-response (AUTH SCRAM-SHA-1/SCRAM-SHA-256, whose server signature the client verifies; a235that arrives without the server-final334continuation is refused with anSmtpError, kind"smtp", since the server never proved it knows the password). MAIL FROM:<from>, oneRCPT TO:<r>per recipient,DATA.- Send the message ended by
<CRLF>.<CRLF>. Every line ending (CRLF, bare LF, bare CR) becomes CRLF before dot-stuffing, so a server that ends lines on a bare CR or LF cannot see an early end of data (SMTP smuggling). QUITand close.
The from / recipients are the envelope (who the server routes to), separate from the From: / To: header lines in the message - set both. Each envelope address is validated before the connection opens: control characters and CRLF (command injection) are rejected, and the address must be a well-formed local@domain per RFC 5321 (a missing @, an empty local part or domain, or a length over 254 throws). The local part must be a dot-string (a.b+tag) or a quoted string ("a b"), the domain LDH labels or an address literal ([192.0.2.1]): a >, < or space outside quotes throws, so an address cannot close its angle brackets and append ESMTP parameters (a@example.com> NOTIFY=NEVER).
When auth runs over an unencrypted (security: transport.Security.None) connection, send refuses to hand SASL credentials to the server unless allowInsecureAuth: true is set, so a password does not cross the wire in the clear by accident. .Tls and a completed .Starttls upgrade are encrypted and unaffected.
Certificate verification for .Tls / .Starttls is the net default (on; see net.md for the opt-out).
Persistent session (many messages, one handshake)
smtp.send is the one-shot convenience: it opens a session, delivers one message, and closes. To send a queue of messages, do the handshake (connect + STARTTLS + AUTH) once with smtp.open, then smtp.sendOn each message, and smtp.close at the end - so N messages pay one TLS + auth round-trip instead of N:
def s as smtp.Session init smtp.open($opts);
for (def m in $queue) {
smtp.sendOn($s, $m.from, $m.recipients, $m.message);
}
smtp.close($s);The Session is value-semantic, but its conn is a shared net.Conn handle, so passing $s to each sendOn reuses the same socket. Each sendOn issues a RSET first, so a rejected message cannot bleed into the next. close sends a best-effort QUIT (a dead connection is closed regardless). It cannot change the caller's copy, so $s.open stays true; a sendOn on a closed session throws the generic kind "closed" (its socket is gone). A rejected sendOn leaves the session open - close it (or keep sending) as you choose.
Errors
A rejection at any step throws an smtp.SmtpError (kind "smtp") carrying the step and the server's reply, so wrap untrusted sends in try / catch. Every error of kind "smtp" is one:
| Field | Meaning |
|---|---|
kind | always "smtp" |
message | <command>: <code> <reply text>, or what was refused locally |
code | the reply code (550, 451, ...); 0 for a local check |
enhancedCode | the RFC 3463 code at the start of the reply text (5.1.1, 4.7.1), "" when none |
command | the rejected command (MAIL FROM, RCPT TO, DATA, ...), "" for a local check |
temporary | true for a 4xx: the same message may be accepted later; a 5xx is permanent |
use io;
try {
smtp.send($opts, $from, $rcpts, $wire);
} catch (e) {
io.printf("send failed: %s\n", $e.message);
}A connection failure (host down, port blocked) surfaces as the underlying net error through the same path. A server that closes the connection before a complete reply - including a final line cut off without its CRLF, such as 250 que - throws the generic kind "closed": the command's outcome is unknown, so it is never taken for success.
Testing
The pure protocol logic - reply-code parsing (including multi-line 250- continuations), AUTH PLAIN base64, and dot-stuffing - is unit-tested in the overlay. The networked send path is covered end to end by an in-process fake SMTP server in the Go test suite (so it runs in CI without an external server); a live send against a real daemon is the demo's job.
Out of scope
- Send only. Receiving is
pop/imap; this module does not fetch mail. - No ESMTP
PIPELINING. Commands go one at a time, each waiting for its reply.sendopens, delivers, and closes one connection per call; to reuse a connection across messages, useopen+sendOn+close. - No SMTPUTF8 (RFC 6531). A non-ASCII local part (before the
@) throws rather than sending a misrouted address. An internationalized domain in the host or an envelope address (user@münchen.de) is IDNA-encoded to itsxn--form (viaidna).
Timeouts
Reads carry a 30 s idle timeout (a deadline re-armed before each read), so a hung server fails with a catchable error instead of blocking the caller. The initial connect and the STARTTLS handshake are bounded by their own 30 s connection-establishment timeout, so a slow or unreachable server fails the dial.
See also
- mime.md - build the
message(headers, multipart, encodings). - net.md - the transport (
connect/connectTLS/startTLS) and TLS optionssmtpbuilds on. - modules/index.md - the module catalog and import rules.
Academy talks
| Talk | What it shows |
|---|---|
| Email with Jennifer: SMTP, IMAP and POP3 | sending mail with envelopes over one session |
API reference
Every exported function, struct and constant of smtp, with its signature, generated from the module's doc comments: the smtp API reference.