Outbound SMTP
[endpoints.transport]type = "smtp"
[endpoints.transport.settings]host = "smtp.mailgun.org"port = 587username = "postmaster@mg.yourdomain.com"password = "${env.SMTP_PASSWORD}"# Set this when the relay requires a public domain in EHLO/HELO.hello_hostname = "mail.yourdomain.com"# "starttls" (default) upgrades after EHLO; "implicit" runs TLS before the# greeting, which is what port 465 expects; "none" is plaintext.tls = "starttls"A self-hosted mail server (Stalwart, Mailcow, Postfix) that exposes submission on port 465 only:
[endpoints.transport.settings]host = "mail.example.com"port = 465tls = "implicit"username = "${env.SMTP_USERNAME}"password = "${env.SMTP_PASSWORD}"Settings
Section titled “Settings”| Setting | Required | Default | Description |
|---|---|---|---|
host | yes | — | SMTP server hostname. |
port | yes | — | SMTP server port. 587 for STARTTLS, 465 for implicit TLS (set tls = "implicit"), 2525 on relays that offer it as an alternative STARTTLS port. Port 25 is blocked outbound on most cloud providers. |
username | yes | — | SMTP AUTH PLAIN username. |
password | yes | — | SMTP AUTH PLAIN password. Never logged in any code path. |
hello_hostname | no | "localhost" | Hostname announced in EHLO/HELO. Set this to a domain your relay recognizes when it rejects localhost clients. |
tls | no | "starttls" | "starttls" connects in plaintext and upgrades before AUTH, refusing a server that doesn’t offer STARTTLS. "implicit" runs the TLS handshake before the SMTP greeting (port 465). "none" is plaintext, for local testing or a trusted internal network only. Since v2.1. |
require_tls | no | — | Older spelling of tls: true is "starttls", false is "none". Still accepted. Setting both keys is a config error. |
tls_insecure_skip_verify | no | false | Skip TLS certificate validation in either TLS mode. Test-only escape hatch for self-signed certs in local dev. |
Behavior
Section titled “Behavior”| Aspect | Behavior |
|---|---|
| Protocol flow | starttls: EHLO → STARTTLS → AUTH PLAIN → MAIL FROM → RCPT TO → DATA → QUIT. implicit: TLS handshake first, then the same sequence without STARTTLS. none: no TLS step. |
| EHLO/HELO hostname | hello_hostname when configured; localhost otherwise |
| Per-request timeout | 30s, applied to the socket, so it caps the whole transaction from connect to QUIT (longer than HTTP transports because SMTP has slower greeting + DATA round trips) |
| Connection reuse | None — one TCP connection per Send. (Connection pooling is a v2 optimization.) |
| Message body | Hand-constructed RFC 5322 message (From/To/Subject/Reply-To headers + plain-text body, UTF-8) |
| Subject encoding | RFC 2047 B-encoding for non-ASCII subjects; ASCII passes through unchanged |
transport_message_id | Empty — stdlib net/smtp doesn’t expose the server’s queued as <id> response. Operators must correlate by timestamp + recipient in the relay’s own logs. |
Use cases
Section titled “Use cases”The SMTP transport is for operators who want Posthorn to relay to an SMTP server they already trust rather than calling an HTTP API. Common shapes:
- Mailgun’s SMTP gateway (
smtp.mailgun.org:587) — same Mailgun account, SMTP wire instead of HTTP - SendGrid’s SMTP relay (
smtp.sendgrid.net:587) - A self-hosted Postfix smarthost that handles the actual delivery to recipients
- Your own mail server (Stalwart, Mailcow, Postfix) on its 465 or 587 submission port
- Mailtrap or MailHog for testing in a local development environment
- An internal mail server for org-internal-only notifications that never leave the network
If you’re not sure which to pick, prefer one of the HTTP API transports (Postmark, Resend, Mailgun) — they have better operational ergonomics (per-request error responses, structured rate-limit headers, dashboards). SMTP is the “I need SMTP wire” fallback.
Error classification
Section titled “Error classification”| Condition | Error class | Retry? |
|---|---|---|
| Connect / TCP timeout / DNS failure | ErrTransient | yes, after 1s |
STARTTLS not advertised when tls = "starttls" | ErrTerminal | no — operator must fix config or pick a different server |
| TLS handshake failure (either mode) | ErrTerminal | no — the certificate didn’t verify, or the mode doesn’t match the port (see gotchas) |
| Connected, but no greeting before the deadline | ErrTransient | yes, after 1s — when the port is 465 the error suggests tls = "implicit" |
AUTH rejected (typically 535 5.7.8) | ErrTerminal (bad credentials) | no |
MAIL FROM / RCPT TO 4xx (greylisting, 450 4.7.1) | ErrTransient | yes, after 1s |
MAIL FROM / RCPT TO 5xx (relay-rejected, bad address, 550 5.1.1) | ErrTerminal | no |
DATA initial command 5xx (e.g., 552 5.3.4 Message too big) | ErrTerminal | no |
Carriage-return / line-feed (CRLF) in From / To / Subject / Reply-To (header injection attempt) | ErrTerminal | no — rejected at Posthorn’s edge before dialing |
Header injection defense
Section titled “Header injection defense”SMTP is the only Posthorn transport that hand-writes the RFC 5322 message envelope (because there’s no JSON struct to lean on). The structural header-injection defense is pre-write validation: before the wire conversation starts, Posthorn checks that no submitter-controlled header value contains CR or LF. Any CRLF in From/To/Subject/Reply-To is rejected with ErrTerminal and the dial never happens.
Additionally:
- MAIL FROM / RCPT TO envelopes go through stdlib
net/smtp.Clientwhich has its own CRLF validation - DATA body is wrapped in
net/textproto.DotWriterwhich handles dot-stuffing automatically — a body line starting with.cannot prematurely terminate the DATA section
Common gotchas
Section titled “Common gotchas”| Symptom | Likely cause | Fix |
|---|---|---|
smtp: server does not advertise STARTTLS (tls = "starttls" requires it) | Server’s EHLO response omits STARTTLS (rare for legitimate relays) | Verify server hostname/port; if intentional (local-network plaintext), set tls = "none" explicitly |
smtp: TLS handshake failed: server answered in plaintext, so port 587 probably expects tls = "starttls" | tls = "implicit" pointed at a STARTTLS port | Set tls = "starttls", or use the server’s implicit-TLS port (usually 465) |
smtp: connected but no SMTP greeting arrived (port 465 usually expects tls = "implicit") | tls = "starttls" pointed at an implicit-TLS port; both sides wait for the other to speak first | Set tls = "implicit" |
smtp: TLS handshake failed or smtp: STARTTLS handshake failed with an x509 error | The server’s certificate isn’t trusted: self-signed, wrong hostname, expired | Fix the certificate; for local testing only, tls_insecure_skip_verify = true |
535 5.7.8 Authentication failed | Wrong username or password, or AUTH PLAIN not supported by server | Check creds; some servers require username to be the literal email address, not a username |
Relay rejects EHLO localhost / HELO localhost | Relay policy requires an identifiable client hostname | Set hello_hostname to a domain accepted by the relay |
| Connection times out on connect | Cloud provider blocking outbound port 25 (and sometimes 587) | Use port 2525 if your provider blocks 587, or switch to an HTTP-API transport |
450 4.7.1 errors retry but then succeed | Greylisting — server delays first-time-from-this-sender mail | Working as intended; Posthorn’s retry policy handles this |
| Mail accepted but never delivered | The SMTP relay accepted but failed downstream — Posthorn has no visibility | Check the relay’s own logs and bounce notifications |