1

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 info@mydomain.com and whenever anyone sends email to this address, I want it to be automatically forwarded to (for example) mydomain@gmail.com.

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

osullic
  • 131
  • 6

2 Answers2

2

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.

Tim
  • 31,888
  • 7
  • 52
  • 78
  • Thanks. I was afraid this might be the case. Google (for example) charges $5/user/month or something like that. For a business, it's an easy cost to absorb, but for a personal website, $60/year seems like an awful lot of money to just get an email address working... – osullic May 18 '18 at 08:54
  • You can set up an email server on your server, and it's probably not particularly even particularly difficult. It just opens you up to spam and increased traffic. Maybe someone will give you another answer. See also my edited answer for another option. – Tim May 18 '18 at 20:02
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:

    info@mydomain.com mydomain@gmail.com
    

    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 info@mydomain.com was now being delivered (via Sendmail on my EC2 instance) to mydomain@gmail.com.

osullic
  • 131
  • 6
  • Update... This solution was working well for me, but I started to experience difficulty forwarding email to Hotmail/Outlook addresses. Microsoft explains (vaguely) why [here](https://sendersupport.olc.protection.outlook.com/pm/policies.aspx). Basically, I needed to implement reverse DNS, SPF, DKIM and DMARC. I tried following some guides [[1](https://www.rackaid.com/blog/email-dns-records/)] [[2](https://www.linuxbabe.com/mail-server/setting-up-dkim-and-spf)] [[3](https://www.web-workers.ch/index.php/2019/10/21/how-to-configure-dkim-spf-dmarc-on-sendmail-for-multiple-domains-on-centos-7/)]... – osullic May 04 '20 at 22:47
  • ...but I didn't manage to integrate it successfully with sendmail and that was a struggle too far. I signed up for AWS SES instead (which is free anyway at the level at which I send emails from my web app), and they have some good [documentation](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/) that I followed to get everything up and running successfully. Emails are now being successfully delivered to Hotmail/Outlook again. – osullic May 04 '20 at 22:48