Configuring an Ubuntu Linux server to route outbound emails through Gmail SMTP Relay using Postfix

Here’s another one to file under “For My Own Future Reference,” even though I’m phasing out directly managing clients’ VPS hosting. (What a pain that has become in the 2020s!)

I manage a few web servers, and handling SMTP locally with Postfix is no longer viable thanks to the ridiculous lengths receiving mail servers now have to go to in filtering the nonstop firehose barrage of spam.

Most of the time these are WordPress sites, so I have the client use a plugin like Gravity SMTP to route their mail through their chosen mail hosting provider (typically Microsoft 365 or Google Workspace, more affectionately known as Outlook and Gmail, respectively).

But I have a couple of staging/testing servers where I’ve just given up on having outbound email at all. Except that can still be a pain sometimes, like when clients lose their passwords. (How do people lose their passwords so much? Why haven’t we solved this yet in 2026? And don’t even say “two-factor authentication” or “passkeys.”)

I’ve finally gotten fed up with this situation, and I happen to have a bit of spare time this week to tidy up some of the loose ends that have been annoying me all year, so I decided I needed to find a way to do Gmail SMTP Relay at the OS level on these servers.

Fortunately, I found an excellent tutorial on the matter, so (at the risk of link rot) I won’t bother repeating all of that here. Even though my servers are running Ubuntu 26.04, the tutorial still works.

Except, not quite. After I followed the steps, relay was working perfectly when I sent messages to other domains, but it was failing on emails sent to my own domain. tl;dr The server was still trying to handle those messages locally.

I realized the problem was that the mydestination parameter in the /etc/postfix/main.cf file included my domain. There may be other, better, solutions to this problem, but in my case the simplest thing to do was to edit that line so the only value it contains is localhost. I did that, restarted Postfix, and… it works!