Tuesday, February 16, 2016

Setting up Mail Forward on AWS with Postfix


I have recently moved all my personal domains across to AWS Route 53. One feature I really missed after the migration was the ability to mail forward. With my previous service provider, I had all emails coming into my achinthagunasekara.com domain forward to one email address. But AWS doesn't provide the direct support for forwarding emails like that.

This is the solution, I came up with to get around this issue. My domain is achinthagunasekara.com and you should change this to match yours.

First of all, launched a nano EC2 instance with an Ubuntu image. Nano instance is powerful enough for the amount of mail I get thought to this domain, but you should consider using a powerful instance, if you are expecting high volume of emails.

Then assign an elastic IP address to this instance.

On route 53, create a A Record pointing the subdomain mail.achinthagunasekara.com to the new elastic IP address.

Then create a MX record pointing all my incoming mail to mail.achinthagunasekara.com



Log into the newly created instance using SSH.

Use the package manager to install Postfix (I've used apt-get as my instance was running Ubuntu)

apt-get install postfix

Open the main postfix configuration file

vi /etc/postfix/main.cf 

Add the following lines to the file

virtual_alias_domains = achinthagunasekara.com
virtual_alias_maps = hash:/etc/postfix/virtual

Now create a file called /etc/postfix/virtual

vi /etc/postfix/virtual

Now to forward mail from admin@achinthagunasekara.com to me@mydomain.com, add the following like to the file.

admin@achinthagunasekara.com me@mydomain.com

To forward all mail coming to achinthagunasekara.com to me@mydomain.com, add the following like to the file.

@achinthagunasekara.com me@mydomain.com

Save and close the file.

Run the following command to finalize the configuration and restart Postfix.

postmap /etc/postfix/virtual
service postfix reload

Now we are nearly there. Next thing is to go back to your EC2 instance and modify the security group. Make sure you're allowing incoming connections on TCP port 25.


That's it. Now send an email to your domain and test it!

Debugging


Any errors would be logged to /var/log/mail.log and /var/log/mail.err files.

Tail these files, while sending an email to check for incoming connections and errors.

tail -f /var/log/mail.log