Set up email accounts that just auto-forward to another address

I set up an EC2 instance on Amazon AWS and installed a LAMP stack by following the tutorial here:
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-LAMP.html
I assigned an elastic (i.e. static) IP address to my instance, and pointed a domain name that I own at it (via DNS records handled by my domain registrar, which is not AWS). When I browse to my domain, I can see my index.html file - everything is working fine so far.

I want to set up some email addresses that just auto-forward to existing Gmail accounts. Imagine my domain is mydomain.com - what I want is to set up [email protected] and whenever anyone sends email to this address, I want it to be automatically forwarded to (for example) [email protected].

What is the simplest way to set this up? I wonder if AWS has a tutorial I can walk through.


Solution 1:

The simplest way is to not involve your server in mail at all. It's a low value service that has complexities around spam and delivery, with technologies like SPF, DKIM and others relevant.

I suggest you use a hosted email provider - there are many including Google for Business, Office 365, Fastmail, etc. Point your DNS MX records at that provider, configure it, and use IMAP / web mail to check the email. Those platforms also allow you to configure mail forwarding if you want to.

Update: I googled "free email forwarding" and found ImprovMX and ForwardMX (not free). I know nothing about them, but they would do what you want - set up your MX records and it gets forwarded to your email. Beware that both would be able to read your email. ImprovMX has no privacy policy. ForwardMX has a privacy policy and charges a relatively small fee for forwarding.

Free solution: If you want to set up mail forwarding from your server, which would only cost you for bandwidth, you can use this answer from Server Fault. It looks pretty easy.

Solution 2:

Thanks to another question that @Tim has linked to, I got pointed in the right direction and solved this problem.

That other question describes the same problem as I was having. The lead answer there (as of now anyway) suggests to do things using Postfix. Another answer suggests using Sendmail. I decided to try the Sendmail suggestion since it looked pretty straightforward and Sendmail was installed by default on my EC2 instance and Postfix wasn't. Things weren't quite as simple as that answer seems to suggest, but I got it working by doing the following:

  1. First things first, you need to ensure the DNS records are correct so that mail servers across the world know where to direct emails that are sent to a @mydomain.com address. In the DNS records for my domain, I have a MX record that points to mydomain.com. and then I have an A record that points to the elastic (i.e. static) IP address of my EC2 instance. That is telling mail servers to direct email for this domain to the IP address indicated by the A record for mydomain.com, i.e. to my EC2 instance.

Now Sendmail needs to be configured to listen for incoming email, and forward it as desired. I found a tutorial on how to achieve this here. The steps are:

  1. The system needs to know which domain(s) it is acting as a mail server for - otherwise Sendmail will not forward these emails. Edit /etc/mail/local-host-names and add the respective domain name(s):

    # local-host-names - include all aliases for your machine here.
    mydomain.com
    myotherdomain.com
    
  2. Edit /etc/mail/access to tell Sendmail to relay mail for your domain(s). This file is for security. Add:

    mydomain.com RELAY
    
  3. By default Sendmail isn't listening externally for incoming mail. In /etc/mail/sendmail.mc there is a line telling Sendmail to only listen on the IPv4 loopback address 127.0.0.1 and not on any other network devices. Remove the loopback address restriction to accept email from the internet.

    Change

    DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
    

    to

    DAEMON_OPTIONS(`Port=smtp, Name=MTA')dnl
    
  4. Now we need to set up the "virtual users", i.e. the email addresses to be auto-forwarded. The following line should be in sendmail.mc; if it's not there, add it:

    FEATURE(`virtusertable', `hash -o /etc/mail/virtusertable.db')dnl
    

    This tells Sendmail to look in the (compiled version of the) file virtusertable for addresses to be auto-forwarded, and to where they should be forwarded. In the virtusertable file, add the following:

    [email protected] [email protected]
    

    I believe you also need an additional line in sendmail.mc, to tell Sendmail which domain(s) the virtusertable applies to. So, add also this line:

    VIRTUSER_DOMAIN_FILE(`/etc/mail/virtual-domains')dnl
    

    And in virtual-domains, add the following:

    mydomain.com
    
  5. Sendmail doesn't read all these configuration files directly - they need to be compiled. Run make in the /etc/mail directory. You need to have the sendmail-cf package installed for this to work.

  6. Restart Sendmail: sudo service sendmail restart

At this point I thought everything should work... but it didn't. I ignored it for a couple of days, considering that I might try again using Postfix, before I remembered something... AWS applies security rules to block/allow certain network traffic. Mail servers listen for new mail via SMTP, but I hadn't set a rule to allow incoming traffic on port 25. So in the AWS Management Console, ensure you open inbound TCP port 25 in the security group that is applied to the EC2 instance. Once I did that, everything started to work as desired immediately. Email sent to [email protected] was now being delivered (via Sendmail on my EC2 instance) to [email protected].