I build mailkube, and the relay in this post is its open-source container. The pattern itself is older than all of us: it's how SMTP servers have always handed mail to each other.

Call an email API from a request handler and your user waits on a TLS handshake, an AUTH exchange and the provider's reply before they ever see "check your inbox". That's the good case. The bad case is the provider answering 450 4.7.1 because you went over your send rate, and now a rate limit is an exception in your signup endpoint.

Diagram of the store-and-forward relay. An app and a worker send plain SMTP to smtp-relay on port 25, which answers 250 in milliseconds and writes the message to a durable queue. The relay's SMTP client delivers from the queue to smtp.mailkube.com on port 587 over STARTTLS with AUTH, reusing one connection. A dashed arrow from the client back to the queue reads: 4xx, defer, retry later.
The whole pattern. The dashed edge is the reason it exists.

Store and forward

A mail server that says 250 to a message has taken responsibility for it. It writes the message to disk, answers, and delivers later on its own schedule. A 4xx from the next hop means "try again later", so it goes back in the queue with a backoff. A 5xx means "never", so it's logged as bounced. Every MTA on the internet works like this.

Run one of those inside your own network and your app gets the cheap half of that contract. The 250 comes back in milliseconds from a container next to it, and the slow, failure-prone half (TLS to the provider, authentication, rate limits, network blips) happens in the background where nobody's waiting.

If you already run a job queue like Sidekiq, Celery or BullMQ, a "send email" job buys you most of this, and that's a perfectly good answer. I still put a relay in, for three reasons. The SMTP credential lives in one container instead of in every service's config. Anything that can speak SMTP to port 25 becomes a sender with no code change, which covers the Grafana alerting, the WordPress install and the cron job you forgot about. And Postfix's retry logic for SMTP reply codes has been written and tested for decades, so I don't have to write mine.

The price is real. You now operate a stateful container, and its listener accepts unauthenticated mail from anything that can reach it. Both are manageable, and the rest of this post is mostly about managing them.

The relay

mailkube publishes the relay as ghcr.io/mailkube/smtp-relay. It's Postfix inside: plain SMTP on port 25 for your apps, and a client that forwards to smtp.mailkube.com on port 587 with mandatory, certificate-verified STARTTLS and authentication. The upstream host is hardcoded, and 587 is the only port it accepts.

It needs two values. SMTP_USERNAME is <username>@<verified-domain>, like myapp01@notify.example.com; a bare username doesn't authenticate. SMTP_PASSWORD is the SMTP credential's secret. It isn't an API key, and the relay warns you at startup if the value looks like one.

The smallest Compose file that works

This is the repo's examples/compose/basic, pinned to a major version instead of latest:

services: smtp-relay: image: ghcr.io/mailkube/smtp-relay:1 container_name: mailkube-smtp-relay restart: unless-stopped environment: SMTP_USERNAME: ${SMTP_USERNAME} SMTP_PASSWORD: ${SMTP_PASSWORD} volumes: - relay-spool:/var/spool/postfix # the queue: must survive  - relay-data:/var/lib/postfix # Postfix state: one per instance  stop_grace_period: 60s networks: - mailkube volumes: relay-spool: relay-data: networks: mailkube: driver: bridge 
export SMTP_USERNAME=myapp01@notify.example.com export SMTP_PASSWORD='your-smtp-credential-secret' docker compose up -d 

The two volumes are doing different jobs. /var/spool/postfix is the queue. Lose it and whatever was waiting out an upstream outage goes with the old container. /var/lib/postfix is Postfix's own state, and it has to be per instance: Postfix refuses to start if it finds another running instance's state there, so never share it between replicas.

stop_grace_period matters more than it looks. On SIGTERM the relay spends up to SHUTDOWN_DRAIN_TIMEOUT seconds (20 by default) flushing the queue. Compose's default grace period is 10 seconds, which would kill the drain halfway, hence 60.

Notice there's no ports: section. The relay is reachable only on the mailkube network, and it should stay that way.

From any container on that network, sending is plain smtplib with no login and no STARTTLS:

import smtplib from email.message import EmailMessage msg = EmailMessage() msg["From"] = "hello@notify.example.com" # must be on your verified domain msg["To"] = "jordan@example.com" msg["Subject"] = "Welcome to Acme" msg.set_content("Sent through the local relay.") with smtplib.SMTP("smtp-relay", 25) as s: s.send_message(msg) 

send_message returns as soon as the relay has queued it.

The hardened version

An unauthenticated listener means anything that can reach port 25 can send as your verified domain. On a single Compose host the network is your access control, so make it narrow. These are the lines from examples/compose/hardened that change things:

services: smtp-relay: image: ghcr.io/mailkube/smtp-relay:1 read_only: true tmpfs: - /run # runtime config and the rendered credential  - /tmp cap_drop: [ALL] cap_add: [CHOWN, DAC_OVERRIDE, FOWNER, SETGID, SETUID, NET_BIND_SERVICE] security_opt: - no-new-privileges:true environment: SMTP_USERNAME: ${SMTP_USERNAME} SMTP_PASSWORD_FILE: /run/secrets/smtp_password RELAY_NETWORKS_MODE: replace RELAY_NETWORKS: 172.28.5.0/24 secrets: - smtp_password # volumes, stop_grace_period and networks as in basic  secrets: smtp_password: file: ./secrets/smtp_password networks: mailkube: driver: bridge ipam: config: - subnet: 172.28.5.0/24 

RELAY_NETWORKS_MODE: replace is the line I'd fight for. In the default append mode the relay admits its private-range defaults plus your list, which means anything on any RFC 1918 network that can route to it. replace makes your CIDR the whole allowlist (loopback is always kept), and pinning the subnet in ipam is what makes that CIDR true on every host.

The credential arrives as a file, so it never shows up in docker inspect. Write that file with printf '%s', not echo. The relay strips trailing whitespace from _FILE values, but an invisible newline in a credential file is still the most common reason for a 535 you can't explain, and other tools that read the same file won't be as forgiving.

It still runs as root, because Postfix's master process needs it. What the profile takes away is everything root could do beyond those six capabilities, and every writable path except the two volumes and the two tmpfs mounts. Set LISTEN_PORT=1025 and you can drop NET_BIND_SERVICE too.

Tell it your send rate

The relay paces itself. Postfix can only pace one delivery stream in whole seconds, so a single stream tops out at one message per second, and the relay runs several paced "lanes" instead. RELAY_LANES is your rate in messages per second, and the default of 4 matches the Free plan:

Your plan RELAY_LANES Upstream connections held
Free 4 (default) 8 of 20
Launch, Momentum 6 12 of 20
Elite, Ultimate 8 16 of 20
Scale, Volume, Enterprise 10 20 of 20

On a paid plan, leaving it at 4 just wastes the capacity you're paying for. The right-hand column is the one that bites if you scale out: mailkube's edge admits 20 concurrent connections per source IP, each lane holds two (one delivering, one idle for reuse), and every relay on a Compose host leaves through the same egress address. The budget is 2 × RELAY_LANES × instances ≤ 18. Two replicas at four lanes hold 16, and a third one goes over.

RELAY_LANES=0 turns pacing off. Then every message over your plan's rate comes back 450 4.7.1, and those rejections count against your sending address, so do it on purpose or not at all.

What failure looks like now

A 4xx from upstream is a deferral in the relay's log, not an exception in your app. The queue keeps retrying for MAX_QUEUE_LIFETIME, one day by default. For password resets and one-time codes I set it to 1h, because a code that lands tomorrow is worse than one that never lands.

A 5xx is final, and the one you'll meet first is 550 when the From isn't on your verified domain. ENFORCE_FROM_DOMAIN=yes rewrites the domain and keeps the local part, so wordpress@localhost leaves as wordpress@notify.example.com. It's a migration aid for software you can't change. Fix the sender where you can.

The relay doesn't raise your limits; it queues and retries when you hit them. If you want an alert before users notice, examples/compose/with-metrics adds a Prometheus exporter for the queue, and a rising queue age is the earliest sign of a bad credential or an upstream problem.

Every variable, the inbound-auth mode and the error codes are in the relay docs. Next in this series, the same container on Kubernetes, where you get to choose between one StatefulSet behind a Service and a sidecar in every pod, and the queue's durability is what decides it.