📚 Encyclopedia

Servers & Hosting · updated 2026-10-26

Sending mail from PHP with Postfix, SPF, DKIM and DMARC

emailpostfixphp-mailspfdkimopendkimdmarcdeliverability

The server sends transactional mail (contact forms, password resets) itself, with Postfix and PHP's mail(), and no third-party mail service (Justin does not want Mailgun or similar; do not suggest one). Getting Gmail to accept and not spam-folder that mail takes three DNS records and a DKIM signer. tldrnerd.com, hampton.website and oklahomafreightbrokerassociation.org all send this way.

PHP side

Postfix

DNS records (per sending domain)

  1. SPF TXT on the domain: v=spf1 ip4:<server v4> ip6:<server v6> ~all. Gmail requires SPF or DKIM to accept at all.
  2. DKIM TXT at <selector>._domainkey.example.com holding v=DKIM1; k=rsa; p=<public key>.
  3. DMARC TXT at _dmarc.example.com: start with v=DMARC1; p=none. Verify each with dig +short TXT ... before debugging anything else. Remove dead MX records that point at old providers.

SPF alone still landed in spam for hampton.website; DKIM fixed the classification.

Adding a new sending domain

The steps used for oklahomafreightbrokerassociation.org on 2026-10-23, on a server already signing for other domains: 1. Generate a key for the domain (selector mail) into /etc/opendkim/keys/<domain>/, owned by opendkim, and add a line for it to opendkim's KeyTable and SigningTable. Restart opendkim. 2. Publish SPF (v=spf1 ip4:<server> ~all), the DKIM TXT from the generated .txt file, and DMARC. Wait until dig @1.1.1.1 shows all three. 3. Send a test and check "Show original" in Gmail for SPF, DKIM and DMARC PASS. Then DMARC can go to p=quarantine, which tells receivers to quarantine anything from the domain that did not come from this server. 4. Expect a brand-new domain's first messages to land in Spam even with all three passing. Marking them "Not spam" in the receiving Gmail account trained it; later mail went to the inbox. 5. Until the domain is set up, send from a domain the server is already authorized for (the OFBA form briefly used noreply@tldrnerd.com); the From: domain must be one whose SPF/DKIM cover the server.

If the domain's MX points at a web server that does not accept inbound mail, nobody can write to addresses at that domain. That is fine for noreply@, but say so in the docs.

opendkim with Postfix (Alma 9)

Debugging checklist

  1. dig +short TXT domain, dig +short TXT mail._domainkey.domain, dig +short TXT _dmarc.domain.
  2. journalctl -u postfix / /var/log/maillog for the delivery status from Gmail.
  3. In Gmail, "Show original" reports SPF, DKIM and DMARC pass/fail for a received message.

See also

Linked from

Sources: hampton-website memory (hosting-layout), infra memory (email notes), 2026-07-09, dad repo association/README.md (OFBA, 2026-10-23)