Skip to main content

Sending mail from another address

When the server's address lands on a DNS blocklist, outgoing mail can move to another address with one command: another address of the same server, or an egress node, a small machine elsewhere that the server tunnels its SMTP connections through. The queue stays on the server, so the Outgoing Mail Queue, the quarantine and the traffic log keep working, and the server's SPF follows the move by itself.

ModeMail leaves fromUse it for
directWhatever address the kernel picks (the default)Normal operation.
localAnother address on an interface of this server, or the public address in front of it (a DigitalOcean reserved IP, a 1:1 NAT)A listing of the one address.
nodeThe egress node's address, through a WireGuard tunnelA listing of the provider's whole range, which a second address of the same server does not escape.

Everything that connects out on port 25 moves: every customer domain, the server's own mail, outgoing gateways and relay pushes. While a path is active, the firewall lets root and Postfix connect out to port 25 only from the path's addresses, so a family the path has no address in (IPv6 on a node without IPv6) is not used for mail at all, rather than leaving from the listed address.

What the address needs​

A path has its own HELO name, usually the name its address's PTR points to. Before any mail leaves from it, every public address of the path must pass:

  • Forward-confirmed reverse DNS: one of the address's PTR names has an A (or AAAA) that holds the address, and the HELO name's A (or AAAA) holds it too. The HELO name need not be the PTR name: a PTR vps-7.provider.example whose A is the address, with the HELO name egress1.example.net on the same address, passes.
  • HELO SPF: the SPF of the HELO name gives pass for the address. Receivers check it for bounces and other mail with an empty sender.
  • Not listed on the blocklists the panel checks (Spamhaus ZEN, Barracuda, SpamCop, PSBL). A lookup that fails is "unknown", not a listing.

For the name egress1.example.net on 198.51.100.7:

; at the address's provider (its reverse DNS)
7.100.51.198.in-addr.arpa. PTR egress1.example.net.

; in the zone of example.net
egress1.example.net. A 198.51.100.7
egress1.example.net. TXT "v=spf1 a -all"

These records belong to whoever controls the address and the name; the panel writes none of them. If the name is in a zone the server hosts, add the A and TXT there like any other record. Each check names the record that is missing or wrong, and what it should hold.

Port 25 at the provider

Many clouds block outgoing port 25 on a new machine (DigitalOcean among them). Ask the provider to open it on the node, or the end-to-end check never passes.

Another address of this server​

Add the address to an interface first. It doesn't need to be in a shared IP address group; a mail-only address is the normal case. Then:

elpanctl server mail-egress local 203.0.113.9
elpanctl server mail-egress local 10.15.0.5 --public 203.0.113.9

The second form is for an address the server binds that the world sees as another one: a DigitalOcean reserved IP is sent from by binding the droplet's anchor address. Give one address per family, an IPv4 and an IPv6 for both. The HELO name is the first PTR name of the first public address that resolves back to it, or --helo <name>, which must pass the same checks. The command refuses, and says which record is wrong, when a check fails; the refusal is kept, and status shows it.

An egress node​

elpanctl server mail-egress node create

It prints one docker run and waits, showing the node's progress:

Run this on the egress node (Linux, Docker or Podman, outbound port 25 open):

docker run -d --name elpan-egress-server1 --restart unless-stopped \
--network host --read-only --cap-drop ALL --security-opt no-new-privileges \
ghcr.io/elpan-panel/elpan-egress:1 \
run --token eyJhbGciOiJFZERTQSIs…

If the node is itself an Elpan server, add --user 0:0: its firewall lets only root and postfix
connect out on port 25. The container keeps --cap-drop ALL, --read-only and no-new-privileges.

Waiting for the node to register (Ctrl-C leaves it waiting; see "elpanctl server mail-egress status")…
12:04:31 registered: 198.51.100.7, 2001:db8:7::25 as egress1.example.net
12:04:32 tunnel up
12:04:40 checks passed: FCrDNS, HELO SPF, not listed, end to end
12:05:02 SPF of server1.example.net published with the new addresses
12:10:02 switched: mail leaves from 198.51.100.7 / 2001:db8:7::25
A node that is itself an Elpan server

Add --user 0:0 to the command. Its firewall lets only root and Postfix connect out on port 25, and the image runs as uid 65532, whose port-25 check is refused otherwise. The container keeps --cap-drop ALL, --read-only and no-new-privileges.

Running that command on the node is the whole setup. The container finds its public addresses by asking the server which address it sees it call from, takes the PTR of each as its name, checks them as above, and registers with the server. Each start checks again, so after fixing a record there is nothing to rerun: Docker restarts the refused container with its back-off. The node needs no inbound port: it dials the server's UDP port 51821 (or 51822, the second slot), which the server's firewall opens. It keeps no state, needs no capability, and relays only TCP to ports 25, 465 and 587.

--no-wait prints the command and returns; --command-only prints the command alone. The node's own options (--ipv4, --bind-ipv4, --no-ipv6, --hostname, …) override what it detects, and are documented on the image's page.

The token in the command is the node's credential for as long as the path lives: it has no expiry. It holds no key of the tunnel, and a node that presents it must still pass the server's own checks of its address. To replace it, for example after it leaked:

elpanctl server mail-egress node rotate

It prints a new command; the old token is refused from then on. Replace the container with the new one.

To move from one node to another, create the new one: mail goes on through the old node until the new one is switched to, then the old one is dropped.

The switch​

A new path is pending. Every minute the server walks it through:

  1. Connected (a node): registered, its tunnel up, and its status answer through the tunnel.
  2. Checked by the server, which never takes the node's word: the three checks above, and end to end: from the address mail-out will bind, the banner of the server's own mail.<server> on port 25.
  3. Published: the server's SPF, which every customer domain includes, lists the new addresses on every authoritative name server of the server's zone; the flattened SPF records that include it are resolved again; and the old record's TTL (300 seconds) has passed.
  4. Switched: mail-out and the relay pushes bind the new address and greet with its HELO name, the firewall's port-25 rule moves, and the previous path, if any, is dropped.

The server's SPF keeps the shared group addresses it always listed and adds the paths' addresses. mail-out.<server> keeps its A and AAAA records; only what is said to the receiving server changes.

The active path is checked again every hour. A check that starts failing leaves it active, since mail depends on it, and shows on status and in telemetry.

Flattened SPF on a manual DNS backend

A domain whose SPF is published flattened, with the server's addresses written out, gets the new addresses on its next publication. On a manual DNS backend the account holder must retype it: status lists those names, and the domain's records list shows the new value. The switch doesn't wait for them.

Domains with an outgoing gateway​

A domain that sends through an outgoing gateway now reaches the gateway from the new address. Its recipients never see the server's address, so its SPF doesn't change, but a gateway that admits the server by address refuses it until its administrator adds the new one. local, node create and status list those domains: tell their account holders before the switch.

While a tunnel is down​

When the node stops answering (no WireGuard handshake for three minutes), mail-out holds its queue instead of trying every recipient and timing out. It never falls back to sending directly, which would put the mail back on the listed address. When the tunnel is up again, the hold is lifted and the queue flushed. status says when the queue is held.

Status and the way back​

elpanctl server mail-egress status
elpanctl server mail-egress check
elpanctl server mail-egress direct
  • status: the active and the pending path, their HELO names and every check with its result, the tunnel, the node's own answer and its last report, the recent refused and replaced paths, and the gateway domains and flattened names above. --json prints it as one object.
  • check: the server's checks now. It exits 1 when one fails.
  • direct: back to sending directly, at once and with no check. This is the way back from a broken node. Every path is dropped and its token refused.

Telemetry​

MetricMeaning
elpan_mail_egress_path{state,mode,helo}1 for the active and the pending path; none when mail leaves directly.
elpan_mail_egress_address{family,address,helo}The public addresses mail leaves from.
elpan_mail_egress_checks_failing{state}How many of a path's last checks failed.
elpan_mail_egress_up{slot}, elpan_mail_egress_handshake_age_seconds{slot}Whether a tunnel is up, and the age of its last handshake.
elpan_mail_egress_held1 while mail-out holds its queue for a tunnel that is down.