Custom Domain Mail Forwarding

5 minute read 2016-01-14 Ashkan Kiani -

I recently managed to snag kiani.io, which, presumably, is the domain on which this blog is located. As a result, I was pretty excited to have emails working on this domain because root@kiani.io looks pretty sweet.

The thing that made me finally pull the trigger was when I was coming back from the airport and I made a friend. I wanted to exchange business cards, but didn't have one of my own. As I was designing my business card, I decided I wanted root@kiani.io to be prominent on the card. So here is how I managed to do it with a combination of AWS Route 53, Docker, Digital Ocean, and Gmail.

edit (2019-07-08)

I did set this up on Digital Ocean, but it's not required; this isn't a sponsored post. Any cloud server will do. These days, I use Arch Linux on my Digital Ocean droplet.

Also, this takes very little memory. After running for 3 months from my last restart, it uses around 56MiB for docker daemon and <10MiB for the daemon itself, so it may qualify for the free tier on AWS.

Forwarding Mail: Setting up on Digital Ocean

Start a droplet on digital ocean and install docker. Depending on the OS you use, that's as simple as sudo apt-get install docker on Ubuntu Server.

Now we're going to use the work of this docker image (simple-mail-forwarder) and simply do the following command.

docker run -p 25:25 \
	--name mail-server \
	-d \
	-e SMF_CONFIG='from@customdomain.com:to@targetemail.com:password' \
	zixia/simple-mail-forwarder

Breaking this down:

  • -p 25:25: the server listens on port 25 (though I think you could set this to 587 if you wanted as it supports TLS, which would require changing to 587:25).
  • --name mail-server: I named the image mail-server so that I could easily refer to it.
  • -d: Daemonize the image and run it in the background.
  • -e SMF_CONFIG='from@customdomain.com:to@targetemail.com:password': This is the only configuration required. It forwards mail from from@customdomain.com to to@targetemail.com with a password of password.
    • NOTE: The password is not strictly neccessary, if you do not specify :password it will be randomly generated. i.e. from@domain:to@target is a valid input.
    • You can specify multiple forwarding emails by separating them by semicolons. test@example.com:to@target:password;from2@domain:to@target.
    • You can use a bare domain name as a fallback. @example.com:target@gmail.com will send everything targetting example.com to target@gmail.com
    • You can forward to multiple emails: from@example.com:A@gmail.com|B@gmail.com|C@gmail.com will send to A@gmail.com, B@gmail.com, C@gmail.com
  • zixia/simple-mail-forwarder: Finally the image name.

Now we have a working mail server, seriously, it was that easy.

DNS Records: Route 53 is primo.

Now we need to allow the world to discover what we have done. I used Route 53 because it is incredibly straightforward to use, just create an AWS account and try it.

You can do this without Route 53, as well, all that matters are the DNS records:

name type value
mail.example.com A x.x.x.x
example.com MX 10 mail.example.com.
example.com TXT "v=spf1 mx ~all"

I suspect you could also use a CNAME record here if you already have an A record for your domain with the IP specified.

The important parts here are that the domain name your emails will be sent from are example.com. You could specify a subdomain as well (i.e. dev.example.com), and the mail server is located at mail.example.com where x.x.x.x is the IP address of your droplet. Using mail is up to you. I think it's nicer than smtp.

That should be it, and Route 53 propagates records pretty quickly. You can check the status on whatsmydns.

Optional: Sending emails from your custom domain on Gmail

I followed the instructions on this github for another forwarder that show how to send mail from your Gmail account as the custom domain. I've replicated these instructions below for convenience.

  1. Go to "Settings" and then "Accounts and Import"
  2. Click on "Add another email address you own"
  3. Enter the full email address: from@customdomain.com and click "Next Step"
  4. Set up the SMTP server:
    • Set the SMTP server to mail.customdomain.com and the port to 25.
    • Set the username to the full email address (not just the user part): from@customdomain.com
    • Enter your password for this account
    • Select "Secure connection using TLS"
    • Click "Add Account"
  5. Click on the verify link as it is sent. This might get directed to spam.

There you go. Bingo bango. Now you've got a working custom domain email!


published in programming and tagged mail forwarding , mail and devops