How to setup email forwarding with Postfix on Ubuntu 18.04

 

It is easy to setup email forwarding so [email protected] will forward to another email address, for example [email protected] or [email protected]

 

Follow this simple guide to setup email forwarding using Postfix on Ubuntu 18.04 for both Apache and Nginx servers.

 

I use DigitalOcean to build my servers and host my websites running Nginx on Ubuntu 18.04 for almost all of my WordPress websites for speed and reliability. Nginx, in my opinion, is way better than Apache. However, setting up Postfix works exactly the same for both Nginx and Apache servers.

The basics are installing Postfix (it is already installed on both LAMP and LEMP setups on DigitalOcean) so if you are using either, you can skip this step. Open port 25, setup your DNS, add your domain to the Postfix config file, add the location of your alias file and add your domain and forwarding address to the alias file.

 

Ok lets start:

1. Install Postfix

sudo apt update
sudo apt-get install postfix

 

2. Configure DNS Settings

You need to create an A record and MX record in your domain network settings:

A Record = mail.yourdomain.com > Server IP > TTL 3600

MX Record = yourdomain.com > mail.yourdomain.com > Priority 10 > TTL 14400

Example below:

DNS Settings for Postfix Forwarding DigitalOcean

 

3. Edit the Postfix Config File

sudo nano /etc/postfix/main.cf

Add the following lines below the line “alias_database = hash:/etc/aliases”

virtual_alias_domains = localhost
virtual_alias_maps = hash:/etc/postfix/virtual

Add your domain to the “mydestination =” line.

The file should look like this:

Postfix main.cf File for Postfix Forwarding

 

4. Add your Forwarding Email Addresses to the Virtual Alias File

You will create the virtual postfix file using the nano command and add your email forwarding addresses.

nano /etc/postfix/virtual

[email protected] [email protected]

To catch all emails sent to your domain, just use @yourdomain.com [email protected]

Example below:

Email Forwarding Virtual Alias File

5. Open Port 25

ufw allow 25

 

6. Map Alias Domain File and Restart Postfix

sudo postmap /etc/postfix/virtual
sudo /etc/init.d/postfix reload

 

That is basically all that is to it. Now send an email to [email protected] and check to see if it lands in your forwarding email box. Make sure you don’t send the test email from the address you are forwarding to.

Remember if you don’t already have a DigitalOcean account, you can get $100 free credit using my referral link here.