Mail servers

How to set up your own mail server with Postfix and DKIM (Ubuntu)

ZeroPhantom team 2026-11-29 7 دقيقة قراءة

We run our own sending server, and this is the setup we use. It covers a send-only server: Postfix sends the mail, and OpenDKIM signs it so Gmail and Outlook can check it really came from you. You'll need a VPS with port 25 open (many cloud providers block it, so ask first) and a domain you control.

1. Install the packages

apt update
apt install -y postfix opendkim opendkim-tools mailutils

When Postfix asks, choose Internet Site and enter your domain, for example example.com.

2. Create the DKIM key

mkdir -p /etc/opendkim/keys/example.com
opendkim-genkey -b 2048 -s mail -d example.com -D /etc/opendkim/keys/example.com
chown -R opendkim:opendkim /etc/opendkim

"mail" is the selector. It becomes part of the DNS name: mail._domainkey.example.com.

3. Tell OpenDKIM what to sign

In /etc/opendkim.conf set:

Socket          inet:8891@localhost
KeyTable        /etc/opendkim/key.table
SigningTable    refile:/etc/opendkim/signing.table
InternalHosts   /etc/opendkim/trusted.hosts

Then create the three files:

# /etc/opendkim/key.table
mail._domainkey.example.com example.com:mail:/etc/opendkim/keys/example.com/mail.private

# /etc/opendkim/signing.table
*@example.com mail._domainkey.example.com

# /etc/opendkim/trusted.hosts
127.0.0.1
localhost

4. Connect Postfix to OpenDKIM

postconf -e "milter_default_action = accept"
postconf -e "smtpd_milters = inet:localhost:8891"
postconf -e "non_smtpd_milters = inet:localhost:8891"
systemctl restart opendkim postfix

5. Add the DNS records

TypeNameValue
Amailyour server IP
TXT@v=spf1 ip4:YOUR.IP -all
TXTmail._domainkeythe text inside mail.txt (starts with v=DKIM1)
TXT_dmarcv=DMARC1; p=none; rua=mailto:you@example.com

If you use Cloudflare, set the A record for mail to "DNS only" (grey cloud). Mail can't go through Cloudflare's proxy.

6. Reverse DNS: the step people forget

Your server IP needs a PTR record that points back to mail.example.com. You can't set this in your own DNS. Your hosting provider sets it, so open a ticket and ask. Without it, Gmail is likely to reject or spam your mail. More on that in what a PTR record is.

7. Check it works

opendkim-testkey -d example.com -s mail -vvv      # should end with "key OK"
echo "test" | mail -s "hello" you@gmail.com
tail -f /var/log/mail.log                           # look for status=sent

Then send a message to our free mail tester. It shows SPF, DKIM, DMARC, reverse DNS and blacklist results in one report with a score out of 10.

Before you send in bulk

A new IP has no reputation. Start with a few dozen emails a day to people who will open them, and grow over a few weeks. Check the blacklists now and then. When it's ready, our AI SMTP Mailer can send through it at a safe pace.