setting up an email server on Ubuntu is a great way to manage your own business or project emails (e.g., yourname@yourdomain.com).

Below I’ll show you a clear step-by-step guide for setting up a complete email server using Postfix (SMTP) and Dovecot (IMAP/POP3) — the most common and stable open-source combo.


🧩 1. Requirements

Before starting, make sure you have:

  • 🖥️ Ubuntu Server (22.04 or newer recommended)

  • 🌐 A domain name (e.g., example.com)

  • 📮 DNS records:

    • A record → points mail.example.com → your server’s IP

    • MX record → points example.commail.example.com

    • PTR record (reverse DNS) → your IP → mail.example.com

  • 🔒 Optional but recommended:

    • SPF, DKIM, and DMARC records for better deliverability

    • A valid SSL certificate (from Let’s Encrypt)


⚙️ 2. Install Required Packages

First, update your system:

sudo apt update && sudo apt upgrade -y

Then install Postfix and Dovecot:

sudo apt install postfix dovecot-core dovecot-imapd dovecot-pop3d -y

During Postfix installation, you’ll be prompted:

  • Mail configuration type: choose Internet Site

  • System mail name: enter your domain name (e.g., example.com)


📨 3. Configure Postfix (SMTP)

Open the main configuration file:

sudo nano /etc/postfix/main.cf

Make sure it contains (adjust your domain):

myhostname = mail.example.com
mydomain = example.com
myorigin = /etc/mailname
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
relayhost =
inet_interfaces = all
inet_protocols = ipv4
home_mailbox = Maildir/
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)

Save and exit (Ctrl+O, Enter, Ctrl+X), then restart Postfix:

sudo systemctl restart postfix
sudo systemctl enable postfix

📥 4. Configure Dovecot (IMAP/POP3)

Edit the main config:

sudo nano /etc/dovecot/dovecot.conf

Add or check:

protocols = imap pop3 lmtp

Now enable Maildir format:

sudo nano /etc/dovecot/conf.d/10-mail.conf

Find this line:

mail_location = maildir:~/Maildir

Uncomment it (remove the # if needed).

Restart Dovecot:

sudo systemctl restart dovecot
sudo systemctl enable dovecot

👤 5. Create a Mail User

Create a user account for your email:

sudo adduser user1

Then create their mail directory:

sudo mkdir -p /home/user1/Maildir
sudo chown -R user1:user1 /home/user1/Maildir
sudo chmod -R 700 /home/user1/Maildir

🔐 6. (Optional) Enable SSL/TLS Encryption

Install Let’s Encrypt:

sudo apt install certbot -y
sudo certbot certonly --standalone -d mail.example.com

Then configure Postfix and Dovecot to use the certificate:

  • Postfix SSL config: /etc/postfix/main.cf

  • Dovecot SSL config: /etc/dovecot/conf.d/10-ssl.conf


🧪 7. Test Your Email Server

Send a test email:

echo "This is a test email" | mail -s "Test" user1@example.com

Check mail log:

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

Access mailbox via IMAP:

Use an email client (Thunderbird, Outlook, etc.)

  • Incoming mail (IMAP): mail.example.com, Port 993 (SSL)

  • Outgoing mail (SMTP): mail.example.com, Port 587 (TLS)

  • Username: your Linux username

  • Password: your user password


🧱 8. (Optional but Recommended)

For better email deliverability:

  • Add SPF, DKIM, and DMARC DNS records

  • Use opendkim and opendmarc packages

  • Install SpamAssassin for spam filtering


✅ Summary

Component Software Function
SMTP Postfix Sends outgoing mail
IMAP/POP3 Dovecot Receives and syncs mail
Security Let’s Encrypt + TLS Encrypts connections
Anti-Spam SpamAssassin (optional) Filters junk mail
DNS A, MX, SPF, DKIM, DMARC Enables domain-based email delivery
How to setup email server on ubuntu ?
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *