Skip to content

SMTP ingress

The SMTP ingress lets internal apps that speak SMTP — Ghost’s admin login, Gitea’s notifications, anything that natively talks to a mail server — point at Posthorn instead of a real SMTP server. Posthorn authenticates the client, parses the message, and forwards it via the same outbound transport you already configured (Postmark, Resend, Mailgun, SES, outbound-SMTP). Posthorn is not a mail server — it doesn’t host mailboxes, doesn’t act as an MX, doesn’t do inbound spam filtering. It’s an authenticated relay for known internal clients only.

The canonical use case: a self-hosted app on a homelab can’t reach the public SMTP ports (25/465/587 blocked by the cloud provider), but it can reach a Posthorn instance you also run. Posthorn relays the message out via Postmark’s HTTP API. The app keeps speaking SMTP — it never knows the difference.

[smtp_listener]
listen = ":2525"
require_tls = true
tls_cert = "/etc/posthorn/cert.pem"
tls_key = "/etc/posthorn/key.pem"
auth_required = "smtp-auth" # smtp-auth | client-cert | either
allowed_senders = ["*@yourdomain.com"]
max_recipients_per_session = 10
max_message_size = "1MB"
idle_timeout = "60s"
[[smtp_listener.smtp_users]]
username = "ghost"
password = "${env.GHOST_SMTP_PASSWORD}"
[[smtp_listener.smtp_users]]
username = "gitea"
password = "${env.GITEA_SMTP_PASSWORD}"
[smtp_listener.transport]
type = "postmark"
[smtp_listener.transport.settings]
api_key = "${env.POSTMARK_API_KEY}"

The SMTP listener has its own [smtp_listener.transport] block, separate from [[endpoints]]. One SMTP listener has one outbound transport; per-recipient routing through different transports is a future feature.

From v2.1, one process can serve several listeners, each on its own port with its own transport, credentials, and allowlists. Use the array form, [[smtp_listeners]], and give each listener a name:

[[smtp_listeners]]
name = "tenant-a"
listen = ":2525"
auth_required = "smtp-auth"
tls_cert = "/etc/posthorn/tls/tenant-a.crt"
tls_key = "/etc/posthorn/tls/tenant-a.key"
allowed_senders = ["*@tenant-a.example"]
[[smtp_listeners.smtp_users]]
username = "gitea-a"
password = "${env.TENANT_A_SMTP_PASSWORD}"
[smtp_listeners.transport]
type = "postmark"
[smtp_listeners.transport.settings]
api_key = "${env.TENANT_A_POSTMARK_KEY}"
[[smtp_listeners]]
name = "tenant-b"
listen = ":2526"
auth_required = "smtp-auth"
tls_cert = "/etc/posthorn/tls/tenant-b.crt"
tls_key = "/etc/posthorn/tls/tenant-b.key"
allowed_senders = ["*@tenant-b.example"]
[[smtp_listeners.smtp_users]]
username = "gitea-b"
password = "${env.TENANT_B_SMTP_PASSWORD}"
[smtp_listeners.transport]
type = "ses"
[smtp_listeners.transport.settings]
region = "eu-north-1"
access_key_id = "${env.TENANT_B_SES_ACCESS_KEY_ID}"
secret_access_key = "${env.TENANT_B_SES_SECRET_ACCESS_KEY}"

Rules, all checked at config load:

  • The single [smtp_listener] table keeps working and is the same as a one-element array. Declaring both forms is an error.
  • name is required when there is more than one listener. It must match [A-Za-z0-9_-]+ and be unique. A lone listener without a name is called smtp_listener, which is the label single-listener deployments have always had.
  • listen addresses must not overlap. Two listeners on the same port conflict when they name the same host or when either binds every interface, so :2525 next to 0.0.0.0:2525 is refused.
  • Every per-listener check applies to each listener on its own: auth mode, TLS, allowlists, connection caps, and the trusted_network rule for auth_required = "none".
  • Each listener has exactly one transport. Posthorn never picks a transport by sender or recipient; the port a message arrived on decides.

The name is how you tell listeners apart afterwards. It is the endpoint label on the SMTP metrics, the endpoint value in the submission log, a listener field on every log line the listener writes, and what posthorn validate prints.

The retry queue, the suppression list, and lifecycle event ingestion are per process, not per listener. With [storage] configured, an address suppressed after one listener’s bounce is suppressed for every listener.

Mail waiting in the retry queue is tied to its listener’s name. If you name a listener that used to be unnamed, keep name = "smtp_listener" on it until posthorn_retry_queue_depth is 0, then rename it. Posthorn refuses to start when it finds queued mail for smtp_listener and no listener by that name.

One thing the config can’t do for you: on a plain Docker network, a container attached to several networks is reachable on all of its ports from every one of them, so “one port per tenant” is only a real boundary if each listener binds an address that only that tenant’s network can reach, or if the tenants sit behind a firewall or Kubernetes NetworkPolicy that allows one port each. Per-listener credentials and allowed_senders are the boundary that holds regardless. The per-tenant listeners recipe walks through both.

By default the listener keeps three headers from an inbound message, Subject, From, and Reply-To, and drops everything else. Apps that send lists, listmonk for one, set List-Unsubscribe and List-Unsubscribe-Post on every message so mailbox providers can offer one-click unsubscribe (RFC 8058); Gmail and Yahoo expect those from high-volume senders. To carry them to the provider, name them on the listener. This needs Posthorn 2.1 or later; earlier versions reject the key as an unknown field:

[smtp_listener]
# ...
passthrough_headers = ["List-Unsubscribe", "List-Unsubscribe-Post"]

How it behaves:

  • Only three names are allowed: List-Unsubscribe, List-Unsubscribe-Post, and List-Id. Anything else, including To, Cc, and Bcc, is a config error, so recipients still come from the SMTP envelope only.
  • Values are copied as received, in the order you listed the names. No decoding is applied.
  • Each header is carried once. The RFCs that define these headers allow one of each per message, so if a message has the same header twice the first is carried and the rest are left behind. A header with an empty value is not carried.
  • A value has to be one line of printable ASCII that fits in a 998-character header line. Anything else fails the message with 554 5.6.0 Invalid header value and a smtp_passthrough_header_rejected log line naming the header: a carriage return or line feed (the header-injection check), other control characters, raw 8-bit bytes, or an over-long value. The message is refused rather than sent without the header, so a missing unsubscribe header is never silent.
  • Every mail transport carries them through the provider’s own custom-header field: Postmark, Resend, Mailgun, SES, and outbound SMTP. Each one re-checks the names and values before it sends anything.
  • The webhook transport has no mail headers to put them in. passthrough_headers on a listener whose transport is webhook is a config error.
  • With [storage] configured, the headers are stored with the submission, so a send that is queued and retried later goes out with them.
  • With passthrough_headers unset, nothing changes from v2.0.

Posthorn refuses to relay arbitrary mail. Every accepted message must clear all of:

  1. Authentication — SMTP AUTH PLAIN against smtp_users, or a TLS client certificate signed by client_cert_ca. auth_required = "either" accepts both. For internal-network-only deployments where network access already implies trust, auth_required = "none" skips this layer entirely (see the internal-SMTP-relay recipe).
  2. STARTTLS — when require_tls = true (default), clients can’t send AUTH or MAIL until they’ve upgraded to TLS.
  3. Sender allowlist — allowed_senders is required. Entries can be exact addresses or *@domain wildcards. * allows any sender (only do this with strong auth or a tightly-firewalled internal listener).
  4. Recipient bound — either allowed_recipients (allowlist) or max_recipients_per_session (cap, default 10). Both protect against RCPT bombing.
  5. Size cap — max_message_size (default 1MB). DATA blobs exceeding this return 552 5.3.4.
DefenseFailure response
AUTH not yet completed530 5.7.0 Authentication required
AUTH failed535 5.7.8 Authentication failed
STARTTLS required but not done530 5.7.0 Must issue STARTTLS first
Sender not in allowlist550 5.7.1 Sender not authorized
Recipient not in allowlist550 5.7.1 Recipient not authorized
Recipient cap exceeded452 4.5.3 Too many recipients
Message too big552 5.3.4 Message too big

Recipients come from the envelope, never the MIME

Section titled “Recipients come from the envelope, never the MIME”

A subtle but important invariant: the outbound message’s recipients come from the SMTP RCPT TO commands, never from the Multipurpose Internet Mail Extensions (MIME) To:/Cc:/Bcc: headers in the message body. A malicious client that sent:

RCPT TO:<alice@example.com>
DATA
From: legitimate@yourdomain.com
To: alice@example.com
Bcc: attacker@target.com
Subject: hi
...

would have attacker@target.com ignored entirely. Posthorn relays only to alice@example.com because that’s the only RCPT TO. The smuggled Bcc header lands in the parsed headers map and is discarded.

Ghost (and most modern web apps) configure SMTP via four environment variables or config keys:

Terminal window
mail__from='"Ghost" <noreply@yourdomain.com>'
mail__transport=SMTP
mail__options__host=posthorn.yourdomain.com
mail__options__port=2525
mail__options__secure=false # using STARTTLS, not implicit TLS
mail__options__auth__user=ghost
mail__options__auth__pass="$GHOST_SMTP_PASSWORD"

Match mail__options__auth__user to a [[smtp_listener.smtp_users]] username, mail__options__auth__pass to its password, and Ghost’s mail__from address must match the Posthorn allowed_senders allowlist.

  • HTML-only message bodies. Posthorn requires a text/plain part (either standalone or inside a multipart/alternative). HTML-only messages are rejected with 550 5.6.0. HTML body support is v2 scope.
  • File attachments. Multipart messages with attachments parse successfully, but only the text/plain part reaches the outbound transport — attachments are dropped. Attachment support is v2 scope.
  • Per-RCPT routing. One SMTP listener has one outbound transport. Routing different recipients to different upstream providers is v2 territory.

The SMTP listener emits structured log lines for the full session lifecycle:

smtp_session_open — connection accepted
smtp_tls_established — STARTTLS upgrade completed
smtp_auth_ok — AUTH PLAIN succeeded (includes `user` field)
smtp_auth_failed — AUTH PLAIN rejected (includes `user`; password never logged)
smtp_sender_rejected — MAIL FROM not in allowlist
smtp_recipient_rejected — RCPT TO not in allowlist
smtp_passthrough_header_rejected — a passthrough header value was refused (554); names the header, never the value
smtp_submission_sent — DATA delivered to upstream transport (with submission_id, transport_message_id, size_bytes)
smtp_submission_failed — upstream transport failed
smtp_session_close — connection closed

Each line carries a per-session session_id UUID so operators can trace a single connection through the protocol exchange.

The /metrics endpoint exposes the same posthorn_submissions_sent_total / posthorn_submissions_failed_total counters with endpoint set to the listener’s name ("smtp_listener" for a lone unnamed listener) so operators can split inbound-via-HTTP from inbound-via-SMTP traffic, and one listener from another, in Prometheus.