For a lot of people that want to use Gmail-like features with a custom domain, G-Suite seems to be a perfect solution. You just configure your MX records to point to Google’s server, either use their free service or pay for a subscription, and you get “Gmail” with your own domain. That’s all fine and dandy, but you miss out on several features. Perhaps it’s the business nature of G-Suite, or the fact that Google doesn’t use your sweet-sweet data in the same way, but it’s well documented that G-Suite users lose out on some of the more convenient features that a standard personal Google account offers. Features that I’ve grown to enjoy, and have a difficult time getting rid of (though I’ve considered it several times).
So, how do you use a personal domain with Gmail if you don’t want to use G-Suite? The solution I’ve gone with is not the simplest, and is a bit hacky, but works, and works well.
In the world of email, Postfix is a modern standard in mail servers, and in my opinion, better than the old standard-bearer, Sendmail. It’s feature rich, standards complaint, and at the base of it, simply works. And in my use-case, a very flexible and reliable mail-relay with a MySQL backend.
Sending an email to any of the domains that I owns will first end up on one of 3 servers – a primary server that does the heavy lifting on routing emails, its backup, and a last-ditch server that simply forwards all emails to my Gmail account if the first two can’t route. We’ll focus on the primary server here since I don’t want to get into MySQL replication and backups right now. Postfix on the primary server will look at the “to” address, and see if it’s in the MySQL database, and what addresses (yes, multiple can be assigned) are associated with it. If there’s no addresses, it’ll send a bounce to the sender. If it matches, however, it’ll forward the email onto the next server, be it Gmail or another email server (it can even be a local account).
Why do it this way, rather than hard coding a single address? After learning what one of my co-workers did with his domain, I emulated him. I can quickly and easily generate a new email address for any service I sign up for or anyone I meet. It lets me do sorting rules based on the “to” address, see if a service I signed up for sold or leaked my email address, and lets me revoke addresses if need be.
Linode has an excellent article on configuring Postfix to work with MySQL, though I personally skipped the Dovecot part of the article. Additionally, I deviated from the article a bit with a few changes to my MySQL query from Postfix.
query = SELECT destination FROM virtual_aliases WHERE source='%s' AND valid=1
That query lets me revoke addresses without deleting them, and if need be, re-activate them.
Finally, to manage this whole thing from the web without having to log into MySQL every time I want to add or delete an email alias, I wrote a management page. It’s not the best written, but I do have it up on GitHub. If you want to do this Postfix-MySQL thing yourself, and would like a management page, go ahead and steal my code.
So, that handles the receiving end, but doesn’t make Gmail send using your domain. Well, humble reader, Google has a help article for this very thing, and made sending using your own domain relatively easy. You will need to make sure Postfix can allow connections as a SMTP server, as Google has to confirm you own the domain.