Running your own email server is one of the most challenging—and rewarding—self-hosting projects you can tackle. Unlike other services, email requires proper DNS configuration, security hardening, and reputation management to ensure your messages actually reach their destination. But once you’ve got it working, you’ll have complete control over your communications, better privacy, and the satisfaction of truly owning your digital identity.
In this guide, I’ll walk you through setting up a complete email server stack using Postfix (SMTP), Dovecot (IMAP), and Roundcube (webmail). This is a production-ready configuration that includes spam filtering, SSL/TLS encryption, and modern authentication. By the end, you’ll have a fully functional email server capable of sending and receiving mail for your domain.
Why Self-Host Email?
Before we dive in, let’s address the elephant in the room: self-hosted email is hard. Major providers like Gmail and Outlook have made it increasingly difficult for independent mail servers to compete. Spam filters are aggressive, IP reputation matters more than ever, and one misconfiguration can land all your emails in the spam folder.
So why bother?
Privacy and ownership. Your emails aren’t scanned for advertising, mined for data, or subject to third-party terms of service. You control the encryption, the storage, and the access logs.
Learning experience. Email is fundamental internet infrastructure. Understanding how SMTP, IMAP, DNS records, and spam filtering work together gives you deep insight into how the internet actually functions.
Custom domains and unlimited aliases. Create as many email addresses as you want for your domain. Want hello@, contact@, noreply@, and secret-project@? Go for it.
No vendor lock-in. Your email data lives on your server, in open formats, backed up however you want.
That said, if you just want “Gmail but self-hosted,” consider using a service like Fastmail or Migadu with your custom domain. Self-hosting email makes sense if you value control and are willing to invest time in maintenance.
Prerequisites
Before starting, make sure you have:
- A server with at least 2 GB RAM and 20 GB storage (a small VPS or homelab server will work)
- Ubuntu 22.04 or 24.04 (this guide uses Ubuntu; adapt for other distros)
- A domain name with access to DNS settings
- A static IP address (most VPS providers include this; residential IPs often get blocked)
- Port 25 unblocked by your provider (critical for receiving email; some VPS providers block it by default)
- Root or sudo access to your server
You’ll also need basic familiarity with the Linux command line and text editors like nano or vim.
Architecture Overview
Here’s what we’re building:
- Postfix: The Mail Transfer Agent (MTA) that handles sending and receiving emails via SMTP
- Dovecot: The IMAP/POP3 server that stores and retrieves emails for your mail client
- Roundcube: A modern webmail interface so you can access email from any browser
- SpamAssassin: Spam filtering to keep your inbox clean
- OpenDKIM: DomainKeys Identified Mail for email authentication
- Let’s Encrypt: Free SSL/TLS certificates for encrypted connections
All components will run on a single server, though you can separate them later if needed.
Step 1: DNS Configuration
Proper DNS setup is critical for email. Get this wrong and your mail won’t deliver. You need to configure these records at your domain registrar or DNS provider:
A Record
Points your mail server’s hostname to its IP address.
mail.yourdomain.com. A 203.0.113.10
Replace yourdomain.com with your actual domain and 203.0.113.10 with your server’s public IP.
MX Record
Tells other mail servers where to deliver email for your domain.
yourdomain.com. MX 10 mail.yourdomain.com.
The 10 is the priority (lower numbers = higher priority). If you only have one mail server, the number doesn’t matter much.
PTR Record (Reverse DNS)
This maps your IP address back to your hostname. It’s set by your hosting provider, not your DNS registrar. Most VPS control panels let you configure it. Set it to mail.yourdomain.com.
To verify:
| |
You should see mail.yourdomain.com in the answer.
SPF Record
Sender Policy Framework tells receiving servers which IPs are allowed to send email for your domain.
yourdomain.com. TXT "v=spf1 mx ~all"
This says “only servers listed in the MX record can send mail for this domain.”
DKIM Record
We’ll generate the DKIM key later, but you’ll add a TXT record that looks like:
default._domainkey.yourdomain.com. TXT "v=DKIM1; k=rsa; p=MIGfMA0GCS..."
DMARC Record
DMARC tells receivers what to do if SPF or DKIM checks fail.
_dmarc.yourdomain.com. TXT "v=DMARC1; p=quarantine; rua=mailto:dmarc@yourdomain.com"
This quarantines suspicious emails and sends reports to dmarc@yourdomain.com.
DNS propagation can take up to 48 hours, but usually completes in a few minutes to hours. Use dig to check your records:
| |
Step 2: Install and Configure Postfix
Postfix is the workhorse that sends and receives email. Let’s install and configure it.
Installation
| |
During installation, select “Internet Site” and enter your domain name (e.g., yourdomain.com).
Basic Configuration
Edit /etc/postfix/main.cf:
| |
Update or add these settings:
| |
Replace yourdomain.com with your actual domain.
Create Mail Users
Create a system user for email:
| |
Set a strong password. This user will authenticate via SMTP/IMAP.
Restart Postfix:
| |
Step 3: Install and Configure Dovecot
Dovecot handles IMAP (retrieving email) and provides SASL authentication for Postfix.
Installation
| |
Configuration
Edit /etc/dovecot/dovecot.conf:
| |
Ensure this line is uncommented:
| |
Edit /etc/dovecot/conf.d/10-mail.conf:
| |
Set the mail location:
| |
Edit /etc/dovecot/conf.d/10-auth.conf:
| |
Disable plaintext auth (we’ll use TLS):
| |
Edit /etc/dovecot/conf.d/10-master.conf to enable authentication for Postfix:
| |
Find the service auth section and update:
| |
Edit /etc/dovecot/conf.d/10-ssl.conf:
| |
Configure SSL:
| |
Restart Dovecot:
| |
Step 4: Obtain SSL/TLS Certificates
Install Certbot:
| |
Get a certificate:
| |
Follow the prompts. Certbot will automatically renew certificates via a systemd timer.
After obtaining certificates, restart Postfix and Dovecot:
| |
Step 5: Install and Configure OpenDKIM
DKIM signs outgoing emails so recipients can verify they came from your server.
Installation
| |
Configuration
Edit /etc/opendkim.conf:
| |
Add or update:
| |
Create the key directory:
| |
View the public key:
| |
Copy the value inside the parentheses (starting with v=DKIM1...) and add it to your DNS as a TXT record at default._domainkey.yourdomain.com.
Edit /etc/default/opendkim:
| |
Add:
| |
Restart OpenDKIM:
| |
Update Postfix to use DKIM. Edit /etc/postfix/main.cf:
| |
Add:
| |
Restart Postfix:
| |
Step 6: Install SpamAssassin
SpamAssassin filters incoming spam.
| |
Edit /etc/default/spamassassin:
| |
Enable the daemon:
| |
Start SpamAssassin:
| |
Integrate with Postfix by editing /etc/postfix/master.cf:
| |
Add this line:
| |
And at the end:
| |
Restart Postfix:
| |
Step 7: Install Roundcube Webmail
Roundcube provides a modern webmail interface.
Install Dependencies
| |
Set Up the Database
| |
Inside the MySQL prompt:
| |
Download and Install Roundcube
| |
Configure Apache
Create a virtual host:
| |
Add:
| |
Enable the site:
| |
Run the Installer
Visit http://mail.yourdomain.com/installer in your browser. Follow the wizard:
- Check dependencies (should all be green)
- Configure database: host
localhost, databaseroundcube, userroundcube, password from earlier - Configure IMAP: host
localhost, port143(or993for SSL) - Configure SMTP: host
localhost, port25(or587for submission)
After setup, delete the installer:
| |
Access Roundcube at http://mail.yourdomain.com and log in with your mail user credentials.
Step 8: Testing Your Email Server
Send a Test Email
Use the mail command:
| |
Check your Gmail (or other provider) inbox. If it arrives, your outbound mail works!
Receive a Test Email
Send an email to mailuser@yourdomain.com from an external account. Check /home/mailuser/Maildir/new/ for the message file.
Check Email Headers
Look at the headers of received emails to verify DKIM signatures and SPF pass.
Use Mail Tester
Visit mail-tester.com, send an email to the provided address, and check your score. Aim for 10/10. Common issues:
- Missing or invalid SPF/DKIM/DMARC records
- PTR record not set
- IP on a blacklist (check MXToolbox)
Security Hardening
Firewall Rules
| |
Fail2Ban
Protect against brute-force attacks:
| |
Create /etc/fail2ban/jail.local:
| |
Restart Fail2Ban:
| |
Disable Root Login
Edit /etc/ssh/sshd_config:
| |
Set:
| |
Restart SSH:
| |
Maintenance and Troubleshooting
Check Logs
- Postfix:
/var/log/mail.log - Dovecot:
/var/log/mail.log - Apache:
/var/log/apache2/
Monitor IP Reputation
Regularly check your server’s IP on blacklists:
| |
(Reverse your IP: 10.113.0.203 becomes 203.0.113.10)
Backups
Backup these directories regularly:
/home/mailuser/Maildir/(emails)/etc/postfix//etc/dovecot//var/www/html/roundcube/config/- MySQL database:
mysqldump roundcube > roundcube_backup.sql
Performance Optimization
For a small server (under 100 users), the default settings work fine. For larger deployments:
- Use Redis for caching in Roundcube
- Enable Postfix queue management with multiple delivery agents
- Tune Dovecot IMAP connections limits in
10-master.conf
Consider a dedicated mini PC if hosting at home for better reliability.
Alternatives and Comparisons
If this setup feels too complex, consider:
- Mail-in-a-Box: Automated Ubuntu email server setup
- Mailu: Dockerized email stack
- iRedMail: Full-featured open-source mail server solution
- Managed providers: Fastmail, Migadu, or ProtonMail with custom domains
Each has trade-offs between control, complexity, and convenience.
Final Thoughts
Congratulations! You now have a fully functional, self-hosted email server. This is no small achievement—email is genuinely hard to get right. But now you control your communications, understand how email infrastructure works, and have a foundation you can build on.
Keep monitoring your logs, maintain your IP reputation, and stay on top of security updates. Email is a long-term commitment, but for many self-hosters, it’s the ultimate declaration of digital independence.
If you run into issues, the Postfix and Dovecot documentation is excellent, and communities like Reddit’s r/selfhosted are helpful. Good luck, and welcome to the world of independent email!