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
- Use plain
mail($to, $subject, $body, $headers, '-fnoreply@example.com'). PHPMailer was removed from these apps. - Do not add a
To:header in$headers;mail()adds it from the first argument and you get a duplicate. - The envelope sender (
-ffifth argument) must match theFrom:header domain. A mismatch trips spam filters and breaks SPF alignment. - SELinux must allow it:
setsebool -P httpd_can_sendmail 1(see SELinux file contexts and booleans on web servers). - Contact forms: send From your own domain, put the visitor in
Reply-To. Never use the visitor's address asFrom:; it fails SPF/DMARC for their domain. Pressing Reply still answers the visitor. - Strip CR/LF from every value that goes into a header (name, subject) to block header injection,
and MIME-encode non-ASCII with
mb_encode_mimeheader(). - Save the submission first (file or database) and treat the email as best-effort: if
mail()fails, log it to the PHP error log rather than losing the message.
Postfix
inet_protocols = ipv4was used on the original setup to avoid IPv6 delivery problems when the IPv6 address had no SPF/PTR coverage. If you enable IPv6, include the v6 address in SPF.- The PTR (reverse DNS) is a generic Linode name by default; setting it to something like
mail.example.comin the Linode UI helps reputation.
DNS records (per sending domain)
- SPF TXT on the domain:
v=spf1 ip4:<server v4> ip6:<server v6> ~all. Gmail requires SPF or DKIM to accept at all. - DKIM TXT at
<selector>._domainkey.example.comholdingv=DKIM1; k=rsa; p=<public key>. - DMARC TXT at
_dmarc.example.com: start withv=DMARC1; p=none. Verify each withdig +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)
- Installing
opendkimneeded the CRB repo enabled (forlibmemcached). SigningTablelimited to the one domain (*@hampton.website), selectormail, private key under/etc/opendkim/keys/<domain>/mail.private, socketinet:8891@127.0.0.1.- Postfix:
smtpd_milters = inet:127.0.0.1:8891,non_smtpd_milters = inet:127.0.0.1:8891,milter_default_action = accept. The last one makes it fail open: if opendkim is down, mail still goes out unsigned, and other domains on the box (tldr) are unaffected. non_smtpd_miltersis the one that matters formail(), since PHP submits via the sendmail binary, not SMTP.
Debugging checklist
dig +short TXT domain,dig +short TXT mail._domainkey.domain,dig +short TXT _dmarc.domain.journalctl -u postfix//var/log/maillogfor the delivery status from Gmail.- In Gmail, "Show original" reports SPF, DKIM and DMARC pass/fail for a received message.