/ Linux

Minimal Mailserver - From DigitalOcean to Gmail

For a long time I've hosted my site(s) at Hostpoint. But nowadays I feel like I've outgrown their offerings. Don't get me wrong, Hostpoint is still a very good choice if you want to host website using traditional web technologies such as html, php & mysql. But as soon as you want use something more modern, e.g. node.js, django etc. you're running up against a wall.

Anyway the reason that I'm telling you all this is, because for a few years I've had a email-redirect of my address "theo@w1nter.com" to my gmail address. That redirect was configured in the Hostpoint control panel, and I've never really given it much thought until recently when I tried to get a SSL certificate for my domain. I had to prove that I own the domain and the only available option was to receive and email at "admin@theowinter.ch". Which made me wonder..

How can I receive emails on a DigitalOcean VPS?

With no fancy Hostpoint control panel to save me, I started googling. And here's what I came up with.

Configure DNS Records

The first step is pretty straight forward. Log into your DigitalOcean account and click on DNS. Then add the following records for your domain.

  1. A-Record: [ mail ] [ your.server.ip ]
  2. MX-Record: [ mail.YourDomain.com ] [ 5 ]

DigitalOcean DNS Configuration
Everything else should already exist like in the screenshot above.

Install Postfix

Log into your server via SSH and run sudo apt-get install postfix. When asked, chose "Internet Site" and enter your domain name, e.g. "example.com". Next we're going to edit the config located at /etc/postfix/main.cf. Open it in vim, nano or whatever editor you prefer to use. Then scroll down to the end and add the following lines:

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

If you want to receive emails from multiple domains you can add more then one domain on the first line and separate them by a space. The second line describes the path to our virtual email account mapping.

So open the file /etc/postfix/virtual and you can write your redirect instructions. E.g.

admin@yourDomain.com you@gmail.com
yourName@yourDomain.com you@gmail.com

It's also possible to redirect emails to multiple locations by adding more email addresses on the same line. And if you want to redirect all the messages you can simply omit the part before the @.

# Simple redirect
admin@yourDomain.com you@gmail.com

# Multiple recipients
admin@yourDomain.com you@gmail.com me@gmail.com

# Catch all emails
@yourDomain.com you@gmail.com

When you're done, save (:wq in vim). Now it's time to update the Postfix lookup table. Run postmap /etc/postfix/virtual. Next Reload the Postfix configuration sudo service postfix reload. And we're done.

Test it

Be sure to test it by sending yourself an E-mail. But make sure that you're not sending an email from your gmail address via this redirect to your gmail address. Google notices duplicate emails and quietly ignores them, so you need to use some other provider or ask a friend to test it.

Want to test DigitalOcean? Use this link and you get $10 for free.

Theo Winter

Theo Winter

Hi I'm Theo. I'm a developer, traveller, photo enthusiast, google-addict, macOS user from Switzerland.

Read More