How to Set Up a Mail Server on Your VPS Without Getting Blacklisted

How to Set Up a Mail Server on Your VPS Without Getting Blacklisted

Running a mail server on your VPS sounds simple: install Postfix, point a domain at it. In practice, most self-hosted mail servers fail at the same place. Their mail lands in spam, or never arrives at all, because the server has no reputation and the DNS records that prove it is legitimate are missing. If you want to set up mail server VPS hosting that actually delivers, you need the software, the DNS records, and the patience to test before you send real mail. This guide covers all three, and when to use a relay instead.

Mail Server on Your VPS: The Parts You Need

A working mail server has more moving parts than a web server. Postfix receives and sends mail. Dovecot stores mail and serves it to your mail client over IMAP. OpenDKIM signs outgoing mail so receivers can verify it came from you. You also need a domain with access to its DNS records, a VPS with a static IP address, and a valid TLS certificate. A certificate from Let’s Encrypt works fine, and you can get one the same way you would for a website, as described in our guide on pointing a new domain to your VPS and setting up free HTTPS. Use a subdomain such as mail.example.com for the mail server, and keep the main domain for your website. A small, dedicated VPS with at least 1 GB of RAM is enough for a personal or small-team mail server, and a dedicated box keeps a compromised web app from turning your IP into a spam source.

Check Port 25 Before You Install Anything

Port 25 is the port mail servers use to talk to each other. Many VPS providers block outbound port 25 by default to stop spam, and some block it permanently on cheaper plans. If your provider blocks it, no configuration will deliver your mail. Test it first:

telnet gmail-smtp-in.l.google.com 25

If the connection fails, ask your provider to open outbound port 25. Some open it after a support ticket and a short waiting period. Others never open it. If yours refuses, move to a provider that allows it or send through a relay, covered at the end of this guide. Also confirm that ports 587 and 993 are open: 587 is what your mail client uses to submit mail, and 993 is IMAP over TLS. Lock the rest down with a firewall. Our guide to setting up UFW on Ubuntu Server 24.04 shows the exact rules to use.

Install Postfix and Dovecot

On Ubuntu or Debian, install both packages and the DKIM tools in one pass:

sudo apt update
sudo apt install postfix dovecot-imapd dovecot-pop3d opendkim opendkim-tools

During the Postfix install, pick “Internet Site” and enter your mail domain, for example example.com. This sets myhostname and mydomain in the main configuration file. Open /etc/postfix/main.cf and check these lines:

myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

Set smtpd_tls_cert_file and smtpd_tls_key_file to your Let’s Encrypt certificate paths, and set smtpd_tls_security_level to may so Postfix offers TLS when the other side supports it. Then configure Dovecot to use Maildir storage and to authenticate against system users or a virtual user database. For a single user, system users are fine. For several mailboxes, virtual users keep things cleaner. Start both services:

sudo systemctl restart postfix dovecot
sudo systemctl status postfix dovecot

A clean status with no errors is the only acceptable result at this stage. If Postfix fails to start, read the log with journalctl -u postfix.

Set Up SPF, DKIM, and DMARC

These three DNS records are what keep your mail out of spam folders. Without them, receiving servers have no reason to trust you, and many will reject your mail outright. SPF tells the world which servers may send mail for your domain. Add a TXT record:

v=spf1 mx a:mail.example.com -all

The -all at the end means servers not listed here are not allowed to send for your domain. Use ~all while testing, then switch to -all once you are sure the record is right. DKIM adds a cryptographic signature to every outgoing message. Generate a key pair with opendkim-genkey, publish the public key as a TXT record, and point OpenDKIM at the private key:

default._domainkey.example.com. TXT "v=DKIM1; k=rsa; p=YOUR_PUBLIC_KEY_HERE"

DMARC ties SPF and DKIM together and tells receivers what to do when a message fails both checks. Start with a reporting-only policy so you can see failures without losing mail:

_dmarc.example.com. TXT "v=DMARC1; p=none; rua=mailto:[email protected]"

After a week or two of clean reports, move the policy to p=quarantine, and later to p=reject if the reports stay clean. Records take time to spread through DNS, so wait at least an hour before you test. A full walkthrough of the sending side is in our guide on setting up email sending from your VPS without landing in spam.

Reverse DNS and TLS

Reverse DNS, or PTR, maps your VPS IP address back to your mail hostname. Gmail and other large providers check it, and a missing or mismatched PTR record is one of the fastest ways to get your mail rejected. Set the PTR record to mail.example.com, matching the hostname in your Postfix configuration exactly. You set PTR records in your VPS provider’s control panel, not in your domain’s DNS zone, because the provider owns the IP address block. Our guide to reverse DNS and PTR records on a VPS covers where to find the setting on common providers.

TLS matters on both sides of the connection. Postfix should offer TLS for incoming mail and use it for outgoing mail when the other server supports it. Dovecot should only accept IMAP connections over TLS. Disable plain-text authentication on port 143 and force clients to use port 993.

Test Before You Send Real Mail

Never send your first real message to a customer or a mailing list. Send test mail to an address you control at Gmail and Outlook, and check where it lands. Then run your server through an independent mail tester, which scores your SPF, DKIM, DMARC, reverse DNS, and TLS setup and flags what is broken. Fix every warning it reports before you send anything important.

Also test the receiving side. Send a message from Gmail to your new mailbox and confirm Dovecot delivers it and your mail client can read it over IMAP. Check the Postfix logs while you do this:

sudo tail -f /var/log/mail.log

Look for status=sent on outgoing mail and clean delivery lines on incoming mail. Anything with status=bounced or status=deferred needs reading, not ignoring. A growing queue of deferred mail is often the first sign of a blacklist problem.

Keep the Server Clean After Launch

Getting mail delivered once is not the finish line. Reputation decays fast if the server starts sending mail it should not. Keep Postfix and Dovecot updated, watch the mail queue daily for the first month, and set up alerts for sudden spikes in outgoing volume. A spike almost always means something on the server is compromised or a script is abusing your mail setup. If your VPS sends mail you did not expect, work through the steps in our guide on finding the source of spam sent from your VPS and stopping it before your IP lands on a blacklist.

Check your IP against common blacklists once a week at first, then monthly. If you do land on one, fix the cause first, then request removal through the blacklist’s delisting page. Keep DMARC reports coming in and read them. They show who is sending mail that claims to be from your domain, including spammers trying to spoof you.

When a Relay Makes More Sense

Running your own mail server is real work. You maintain the software, watch the reputation, read DMARC reports, and fix deliverability problems that appear with no warning. For a personal domain or a small team that sends a modest volume, that work is manageable. For a business that sends order confirmations, password resets, and invoices, it is usually the wrong trade. A transactional relay handles the reputation, the warm-up, and the deliverability monitoring for you, and your application sends through it over an authenticated connection. You still need SPF, DKIM, and DMARC records for your domain, but the relay’s servers do the sending. Check official pricing before you pick one, since plans change and free tiers come with sending limits a growing business will hit.

The honest rule is simple. If you enjoy server work and your mail volume is low, self-host and keep the setup in this guide maintained. If mail is a utility your business depends on and you do not want to babysit a server, use a relay and spend the time on your product instead. Either way, set up SPF, DKIM, DMARC, reverse DNS, and TLS first, test with a mail tester, and only then send mail that matters.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.