Last Updated: February 25, 2016
·
4.379K
· Lior Zimmerman

Forward email from yourdomain.com to any address with an Amazon ec2 micro instance

If you are frustrated by the price email services (godaddy, google, etc) charge you (Google charges 5$ per GoogleApps account per month!) - this guide will show you how to forward emails sent to @yourdomain.com to your gmail account in few minutes!

All you need to do, is setup a micro ec2 instance on Amazon (which costs few bucks a month, and sometimes its even free for a whole year).

Starting an ec2 micro instance is a fairly easy task.
Read about it here.

Few points worth mentioning:

  • Choose a linux image (I prefer ubuntu, but it doesn't matter, choose one that you are comfortable with)
  • Create a security group that allows connections from all IPs on port 25 (SMTP) as well as port 22 (SSH)
  • Don't forget to create a key-value pair and download the pem file to a secured location

Once you have the ec2 micro instance up and running, login via ssh (usually ssh -i [pem file] ubuntu@[ec2-address])

Next step - install Postfix.
Postfix is an MTA (similar to sendmail in linux) and has the ability to some very cool stuff. For example, as an SMTP server, it can add automatic signatures to your emails, send copies to a 3rd party and more...

To install postfix on ubuntu:

sudo apt-get install postfix

Then, edit the following config file:

vi /etc/postfix/main.cf

add/edit these lines:

virtual_alias_domains = yourdomain.com 
# virtual_alias_domains = yourdomain.com example.com moredomains.com ..
virtual_alias_maps = hash:/etc/postfix/virtual

Notice that you can tell postfix to manage several domains!
Next, edit the following file:

vi /etc/postfix/virtual

add forwarding addresses as necessary:

lior@yourdomain.com     lior123@gmail.com

(mails arriving to lior@yourdomain.com will be forwarded to lior123@gmail.com)
You can also specify several addresses:

lior@yourdomain.com     lior123@gmail.com,lior@mail.huji.ac.il

And also create a catch-all address:

@yourdomain.com    lior123@gmail.com

Once you are done, restart the postfix service:

sudo postmap /etc/postfix/virtual
sudo service postfix reload

Now you need to edit the MX records of your domain to point to your ec2 micro instance. In GoDaddy, read this short article on how to do it.

Few important points worth mentioning:

  • Google has a strict policy against spammers. Spammers usually use fake addresses to send their messages, So if you don't change your MX records to point to your newly formed email server, it will not deliver your mail (it won't even arrive to the spam folder).

  • Even if you change the MX records, mail sent to you may arrive to the spam folder at first. Simply mark them as "not spam"

  • In order to be able to send mail from your @yourdomain.com address and not from your @gmail, you'll need to validate your new address at Gmail. (Settings -> Accounts/Import -> Send mail as -> Add another email address you own).
    And for that, you'll need to enable SMTP authentication for postfix.

Note: default configuration makes postfix run in a chrooted environment. The guide above doesn't assume that, so you'll end up having authentication errors in your logs identical to this one:

warning: SASL authentication failure: cannot connect to saslauthd server: No such file or directory

if you have this error in the /var/log/mail.log file , then read this github post on how to fix it.

Final note:

Let's add some mailboxes! In order to add a mailbox user, simply add a user to linux (almost) the usual way:

sudo adduser --shell /sbin/nologin username
sudo passwd username

That's it!
Now with close to 0$ we have an email account with our domain name, and as much mailboxes as we like :-)