
WordPress emails tend to fail in the same frustrating ways. Contact form messages disappear. Password resets never show up. WooCommerce receipts land in spam.
The quickest fix is to stop relying on PHP’s default mail() path. Send everything through an SMTP relay you control instead. This guide shows how to set up a Postfix relay for WordPress on a VPS with SMTP AUTH, TLS, and the DNS records that drive deliverability.
By the end, you’ll have a relay you own and a repeatable way to confirm your domain passes SPF, DKIM, and DMARC. The only assumption is that WordPress already runs somewhere (shared hosting, VPS, managed hosting). You’ll also need a separate VPS to act as the relay.
What you’ll build (and when this is the right fix)
You’ll install Postfix on a VPS and expose a submission service on port 587. It will use STARTTLS and SMTP AUTH.
WordPress will send via SMTP (through a plugin) to the relay. The relay will sign outbound mail with DKIM. It will then deliver to the internet with correct reverse DNS and policy records.
- Use this if: WordPress mail is unreliable, your host blocks outbound SMTP, or you want consistent deliverability across multiple sites.
- Don’t use this if: you can’t set DNS records or you can’t get rDNS set for the VPS IP. You’ll spend your time fighting spam folders.
For production, placing the VPS near your users can reduce latency. Deliverability, though, mostly comes down to DNS and identity.
If you don’t have a relay server yet, a small HostMyCode VPS (1 vCPU / 1 GB RAM) is plenty for relaying mail for several WordPress sites.
Prerequisites and naming (read once, avoids later confusion)
- A VPS running Ubuntu 24.04 LTS, Debian 12, AlmaLinux 10, or Rocky Linux 10. Commands below show Ubuntu/Debian first, with RHEL-family notes.
- A domain you control for sending, for example
example.com. - A hostname for the relay, for example
relay.example.com. - Ability to add DNS records for
example.com. - Root (or sudo) access on the VPS.
Recommendation: keep the relay on its own VPS. If you mix it with a busy web stack, load spikes can slow the queue. Those delays are also annoying to debug.
Step 1 — Set the VPS hostname and update the OS
On the relay VPS, set a proper FQDN hostname. Postfix uses this in SMTP greetings. Remote servers do pay attention to it.
sudo hostnamectl set-hostname relay.example.com
hostname -f
Update packages:
# Ubuntu / Debian
sudo apt update && sudo apt -y upgrade
# AlmaLinux / Rocky
sudo dnf -y update
Verify: hostname -f returns relay.example.com.
Step 2 — Install Postfix, Dovecot SASL, and OpenDKIM
We’ll use:
- Postfix for SMTP relay and delivery
- Dovecot for SMTP AUTH (SASL) on submission (587)
- OpenDKIM to sign outbound mail with DKIM
# Ubuntu / Debian
sudo apt -y install postfix postfix-pcre dovecot-core dovecot-imapd opendkim opendkim-tools ca-certificates
# AlmaLinux / Rocky
sudo dnf -y install postfix pcre2 dovecot opendkim opendkim-tools ca-certificates
On Debian/Ubuntu, the Postfix installer may ask for “General type of mail configuration.” Choose Internet Site and set the system mail name to example.com.
We’ll tighten the relay rules in the next step.
Verify Postfix runs:
sudo systemctl enable --now postfix
sudo systemctl status postfix --no-pager
Step 3 — Configure Postfix for authenticated submission only
Edit /etc/postfix/main.cf. A fresh install already includes defaults. You’ll add or adjust the lines below.
sudo nano /etc/postfix/main.cf
Set these values (replace domains/hostnames):
# Identity
myhostname = relay.example.com
mydomain = example.com
myorigin = $mydomain
# Listen on all interfaces (typical VPS)
inet_interfaces = all
inet_protocols = ipv4
# Don’t accept inbound mail for random domains
mydestination = localhost.$mydomain, localhost
# Relay policy: only authenticated clients or localhost
smtpd_relay_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
# Networks allowed without auth (keep tight)
mynetworks = 127.0.0.0/8 [::1]/128
# TLS basics
smtpd_tls_security_level = may
smtp_tls_security_level = may
smtpd_tls_auth_only = yes
smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
# SASL via Dovecot
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
broken_sasl_auth_clients = yes
# Size limits (optional, but helps with giant attachments)
message_size_limit = 26214400
Next, enable the submission service on port 587. Edit /etc/postfix/master.cf and make sure this block exists and is uncommented:
sudo nano /etc/postfix/master.cf
submission inet n - y - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
-o smtpd_sender_restrictions=reject_sender_login_mismatch
-o smtpd_sender_login_maps=texthash:/etc/postfix/sender_login_maps
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
Create a sender/login map so a compromised WordPress plugin can’t spoof random From addresses. For a relay, this single file provides strong damage control.
sudo nano /etc/postfix/sender_login_maps
Example: only allow the SMTP user wp-relay to send as no-reply@example.com and contact@example.com:
no-reply@example.com wp-relay
contact@example.com wp-relay
Compile the map and restart Postfix:
sudo postmap /etc/postfix/sender_login_maps
sudo systemctl restart postfix
Verify listening ports:
sudo ss -ltnp | egrep ':(25|587)\b'
You should see Postfix on :25 (outbound delivery) and :587 (submission). We’ll deal with firewall rules later.
Step 4 — Configure Dovecot SASL for SMTP AUTH
We only need Dovecot’s auth socket for Postfix. The exact config path varies a bit by distro. The settings are the same.
Ubuntu/Debian: edit /etc/dovecot/conf.d/10-master.conf
sudo nano /etc/dovecot/conf.d/10-master.conf
Find the service auth section and add/confirm this block exists:
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}
}
Enable plain login (common for SMTP clients), but keep it restricted to TLS. Edit /etc/dovecot/conf.d/10-auth.conf:
sudo nano /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = yes
auth_mechanisms = plain login
Restart Dovecot:
sudo systemctl enable --now dovecot
sudo systemctl restart dovecot
Create a dedicated SMTP user: This account lives only on the relay VPS. WordPress uses it to authenticate to port 587.
sudo adduser --disabled-password --gecos "" wp-relay
sudo passwd wp-relay
Common mistake: reusing your personal mailbox password. Don’t. Use a dedicated relay account with a strong, unique password.
Step 5 — Add a TLS certificate for the relay hostname
You need a certificate for relay.example.com. Let’s Encrypt is the simplest option.
On a mail-only VPS, standalone mode is straightforward. If you’d rather not open port 80 at all, use DNS validation instead.
Standalone HTTP validation (opens port 80 temporarily):
sudo apt -y install certbot
sudo systemctl stop postfix || true
sudo certbot certonly --standalone -d relay.example.com
sudo systemctl start postfix
Point Postfix at the cert. Add to /etc/postfix/main.cf:
smtpd_tls_cert_file=/etc/letsencrypt/live/relay.example.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/relay.example.com/privkey.pem
Reload Postfix:
sudo systemctl reload postfix
Verify STARTTLS on 587 from your laptop:
openssl s_client -starttls smtp -connect relay.example.com:587 -servername relay.example.com
Look for Verify return code: 0 (ok) and a certificate matching relay.example.com.
If you prefer DNS validation and wildcard flows, HostMyCode already has a related guide. The verification techniques carry over, even though you’re using a specific hostname cert: Add Let’s Encrypt wildcard SSL with Caddy and DNS.
Step 6 — Set up OpenDKIM signing
DKIM is what separates “accepted by the server” from “trusted by the inbox.” For a new relay IP, it matters immediately.
We’ll sign for example.com using selector relay.
sudo mkdir -p /etc/opendkim/keys/example.com
cd /etc/opendkim/keys/example.com
sudo opendkim-genkey -b 2048 -d example.com -s relay
sudo chown -R opendkim:opendkim /etc/opendkim
sudo chmod 0700 /etc/opendkim/keys/example.com
sudo chmod 0600 /etc/opendkim/keys/example.com/relay.private
Edit /etc/opendkim.conf and ensure these lines exist:
sudo nano /etc/opendkim.conf
Syslog yes
UMask 002
Mode sv
Canonicalization relaxed/simple
Socket local:/var/spool/postfix/opendkim/opendkim.sock
PidFile /run/opendkim/opendkim.pid
UserID opendkim
KeyTable /etc/opendkim/key.table
SigningTable /etc/opendkim/signing.table
TrustedHosts /etc/opendkim/trusted.hosts
Create the socket directory for Postfix chroot usage:
sudo mkdir -p /var/spool/postfix/opendkim
sudo chown opendkim:postfix /var/spool/postfix/opendkim
Create the mapping files:
sudo nano /etc/opendkim/key.table
relay._domainkey.example.com example.com:relay:/etc/opendkim/keys/example.com/relay.private
sudo nano /etc/opendkim/signing.table
*@example.com relay._domainkey.example.com
sudo nano /etc/opendkim/trusted.hosts
127.0.0.1
localhost
relay.example.com
Hook OpenDKIM into Postfix. Add to /etc/postfix/main.cf:
milter_default_action = accept
milter_protocol = 6
smtpd_milters = unix:/opendkim/opendkim.sock
non_smtpd_milters = $smtpd_milters
And tell Postfix where that socket lives by creating a symlink inside Postfix’s chrooted spool:
sudo mkdir -p /var/spool/postfix/opendkim
sudo chown opendkim:postfix /var/spool/postfix/opendkim
Restart both services:
sudo systemctl restart opendkim
sudo systemctl restart postfix
Get the DKIM DNS record:
sudo cat /etc/opendkim/keys/example.com/relay.txt
You’ll publish that TXT record in the next step.
Step 7 — DNS records: SPF, DKIM, DMARC, rDNS (deliverability lives here)
Do this work before you point production mail at the relay. Most “Postfix isn’t working” stories are really DNS identity problems.
7.1 Add an A record for the relay hostname
relay.example.com→ A → your VPS public IPv4
7.2 Set reverse DNS (PTR) for the VPS IP
Set the VPS IP’s PTR record to relay.example.com. This is usually done in your hosting control panel or by support.
If you’re running critical email, consider managed VPS hosting so rDNS, firewall, and mail hygiene don’t become a recurring chore.
Verify rDNS:
dig +short -x YOUR_VPS_IP
7.3 SPF record for your sending domain
SPF defines which servers may send for example.com. If you already have SPF for Google Workspace, Microsoft 365, or another sender, extend it.
Do not publish a second SPF record.
Example SPF that includes only your relay VPS IP:
example.com. TXT "v=spf1 ip4:YOUR_VPS_IP -all"
7.4 DKIM record from relay.txt
Create a TXT record for relay._domainkey.example.com with the value shown in relay.txt.
7.5 DMARC record (start with monitoring)
Start with p=none so you can collect reports first. Tighten the policy once you’re confident everything legitimate aligns.
_dmarc.example.com. TXT "v=DMARC1; p=none; rua=mailto:dmarc-reports@example.com; adkim=s; aspf=s"
Verify DNS from the VPS:
dig +short relay.example.com A
dig +short example.com TXT | tr -d '"'
dig +short relay._domainkey.example.com TXT | tr -d '"'
dig +short _dmarc.example.com TXT | tr -d '"'
If you want a careful DNS walkthrough (especially if you’re moving records between providers), this HostMyCode tutorial is a solid companion: Cloudflare DNS setup guide tutorial.
Step 8 — Firewall: allow submission, keep the rest tight
You need:
- Inbound 587/tcp from wherever WordPress runs (or from the internet if you can’t predict IPs)
- Inbound 25/tcp is optional for a pure relay (not receiving mail), but outbound delivery still uses 25 to remote MX servers
On Ubuntu/Debian with UFW:
sudo ufw allow 22/tcp
sudo ufw allow 587/tcp
# Optional: only if you expect inbound SMTP to this host
# sudo ufw allow 25/tcp
sudo ufw enable
sudo ufw status verbose
On Alma/Rocky with firewalld:
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-port=587/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
Common mistake: outbound 25 is blocked at the provider level. Some hosts do this by default.
When that happens, Postfix accepts the message and queues it. It then fails to reach remote MX servers. Your logs will show repeated “Connection timed out”.
Step 9 — Configure WordPress to use the relay (SMTP plugin)
On your WordPress site, install a reputable SMTP plugin (for example “WP Mail SMTP” or similar). Configure it to submit mail to your relay:
- SMTP Host:
relay.example.com - Encryption: STARTTLS
- SMTP Port: 587
- Authentication: On
- Username:
wp-relay - Password: (the password you set on the relay VPS)
- From Email:
no-reply@example.com(must match sender_login_maps) - From Name: your site name
Important: keep the From address consistent. Plugins that rotate or invent From addresses will hurt deliverability.
Your sender/login restriction may also reject mismatched From addresses.
If you split environments (production + staging), keep staging mail contained. Disable outbound email or use a different sender. This pairs well with: Create a staging copy of WordPress on the same VPS.
Step 10 — End-to-end tests: TLS, auth, queue, and headers
Don’t stop at “Test email sent.” Confirm the message was signed, aligned, and accepted by a real inbox provider.
10.1 Test SMTP AUTH from a terminal (recommended)
Install swaks (SMTP test tool) on your laptop or the WordPress server:
# Ubuntu/Debian
sudo apt -y install swaks
# Alma/Rocky
sudo dnf -y install epel-release
sudo dnf -y install swaks
Send a message:
swaks --to youraddress@gmail.com \
--from no-reply@example.com \
--server relay.example.com:587 \
--auth LOGIN \
--auth-user wp-relay \
--tls \
--header "Subject: Relay test" \
--body "If you got this, submission + delivery works."
Verify: swaks should show a successful 250 response after DATA and QUIT.
10.2 Check the mail queue
sudo postqueue -p
If messages sit in queue, inspect the reason:
sudo postcat -q QUEUE_ID | sed -n '1,80p'
sudo tail -n 200 /var/log/mail.log
On RHEL-family, logs may be in /var/log/maillog:
sudo tail -n 200 /var/log/maillog
10.3 Validate SPF/DKIM/DMARC alignment from the received headers
Open the email in Gmail (or another provider), view “Original message,” and check for:
- SPF: PASS, showing your VPS IP
- DKIM: PASS, selector
relay, domainexample.com - DMARC: PASS (policy none is fine while you’re monitoring)
If DKIM fails, the culprit is usually one of these:
- Wrong DKIM TXT record (extra quotes, broken line wrapping, or pasted with spaces)
- OpenDKIM socket path mismatch (Postfix can’t reach the milter)
- SigningTable/KeyTable typo (domain doesn’t match From)
Step 11 — Hardening: prevent abuse and keep logs useful
A public submission port will attract brute-force attempts. You don’t need to hide the service. You need to make abuse expensive and obvious.
11.1 Add Fail2ban for Postfix submission
If you already run Fail2ban, add a jail for Postfix SASL auth failures. If not:
# Ubuntu/Debian
sudo apt -y install fail2ban
# Alma/Rocky
sudo dnf -y install fail2ban
Create /etc/fail2ban/jail.d/postfix-submission.conf:
sudo nano /etc/fail2ban/jail.d/postfix-submission.conf
[postfix-sasl]
enabled = true
port = 587
filter = postfix-sasl
logpath = /var/log/mail.log
maxretry = 5
findtime = 10m
bantime = 1h
On RHEL-family, change logpath to /var/log/maillog.
Restart and verify:
sudo systemctl enable --now fail2ban
sudo systemctl restart fail2ban
sudo fail2ban-client status postfix-sasl
If you want a deeper Nginx-side WordPress protection pattern (similar thinking: ban repeated auth failures), see: Set up Fail2ban for WordPress login on Nginx.
11.2 Rate limits (basic but effective)
Add sane connection limits to Postfix (in main.cf):
smtpd_client_connection_rate_limit = 30
smtpd_client_message_rate_limit = 50
smtpd_client_event_limit_exceptions = $mynetworks
Reload Postfix:
sudo systemctl reload postfix
Step 12 — Operational checks and common failure modes
Mail is quiet until it isn’t. These quick checks save time when something starts backing up.
12.1 Quick health commands
sudo systemctl status postfix opendkim dovecot --no-pagersudo postqueue -p(queue should usually be empty)sudo tail -n 200 /var/log/mail.log(ormaillog)openssl s_client -starttls smtp -connect relay.example.com:587 -servername relay.example.com
12.2 If WordPress “sends” but nothing arrives
- Check Postfix logs for delivery deferrals. The message may be queued due to blocked outbound 25.
- Confirm your VPS provider allows outbound SMTP. If blocked, ask for unblocking or use a different relay IP.
- Verify SPF/DKIM/DMARC. Failing DKIM commonly causes spam placement, not outright loss.
12.3 If you see “Relay access denied”
- Your WordPress plugin isn’t authenticating, or it’s using port 25 instead of 587.
- Confirm
smtpd_sasl_auth_enable=yesand Dovecot auth socket settings.
12.4 If you get “Sender address rejected: not owned by user”
- Your From address doesn’t match
/etc/postfix/sender_login_maps. Update the map or fix the plugin’s From address.
Summary: a relay you control, plus deliverability you can prove
You now have a submission endpoint on your VPS with TLS + SMTP AUTH. You also have Postfix restrictions that block sender spoofing and DKIM signing via OpenDKIM.
Inbox providers also have what they expect: SPF, DMARC, and correct rDNS. If mail goes missing, you have logs and headers that show exactly where it failed.
If you run multiple sites or want a clean, scalable setup, start with a small HostMyCode VPS and keep it dedicated to mail. If you’d rather not deal with rDNS requests, mail logs, and abuse controls, managed VPS hosting is a sensible upgrade path.
If WordPress emails affect signups, orders, or account recovery, treat mail delivery like infrastructure. A small VPS is enough to run a secure relay with DKIM and useful logs. Host it on a HostMyCode VPS, or offload the upkeep to managed VPS hosting so you can focus on the site, not the queue.
FAQ
Do I need port 25 open inbound on the relay VPS?
Not for WordPress submission. WordPress should use port 587 with auth. Outbound delivery still uses port 25 to other mail servers, so outbound 25 must be allowed.
Can I use this relay for multiple WordPress sites and domains?
Yes, but add separate SMTP users (or separate sender maps) per site/domain. Also add DKIM keys and SigningTable entries per domain you send as.
Should I set DMARC to quarantine or reject immediately?
Start with p=none and watch reports for a week. Once you confirm all legitimate senders pass DKIM/SPF alignment, move to quarantine, then reject.
Why not just use an SMTP plugin with a third-party provider?
You can, and it’s often fine. A VPS relay makes sense when you want control, consistent behavior across sites, or you don’t want to depend on an external SMTP provider’s limits and policies.