Let’s assume you already have bought a domain – in this example we’re using tsmean.com – and you want to forward mail. You can replace all tsmean.com in this tutorial with yourdomain.com. So for example when sending an email to email@example.com it should forward it to firstname.lastname@example.org. How can we achieve that? We can set up a mail forwarding server using an ec2 instance and postfix. To get started, create your ec2 instance, in this tutorial we’ll use Ubuntu. You can also reuse an existing instance, for example where you host some websites.
Step 1 – Open ports
You need to have port 25 open for emails to arrive at the server. Head over to the security group of the instance and open port 25:
Step 2 – DNS
We need two DNS records. One is the MX record:
Now we need to create the entry for the mailserver mail.tsmean.com. This is an A record pointing to the elastic IP:
So what this means is that:
mail sent to email@example.com -> mail.tsmean.com -> elastic IP
-> denotes “is resolved to”.
Step 3 – Install Postfix
On your EC2 instance, run
sudo apt-get install postfix
During the installation, choose “Internet Site”. For the mail choose yourdomain.com, not mail.yourdomain.com:
Step 4 – Set Postfix up
Append the following lines to /etc/postfix/main.cf:
... virtual_alias_domains = tsmean.com virtual_alias_maps = hash:/etc/postfix/virtual
And in the /etc/postfix/virtual (you’ll have to create it), insert:
if you want to forward all mails or use
to forward a specific mail.
Step 5 – Apply the mapping
in the terminal.
Step 6 – Restart Postfix
sudo /etc/init.d/postfix reload
Result should look like this:
Step 7 – Test
Send an email to firstname.lastname@example.org. Check also your Spam & Junk mail folders!