Postfix using Gmail as a relay server

💡
Just need to forward emails from your Linux user account to another email? Add a ~/.forward file with the destination email (ie. frankie@example.com). Then chmod 644 the file.

If you have some Linux servers and want them to dispatch your emails (system, logging, monitoring, etc) to an external email address, you can use a proper SMTP service, like Gmail, for that.

Notice that this will not work for mass emails. The idea is to send a few emails per day to yourself. If you're relaying emails for actual users, you must set up a dedicated mail server or use an external service like Mailgun or Amazon Simple Email Service.

Copilots rendition of mail, now and then.

I'm assuming:

1) you want your Linux box to send emails from an existing Gmail account
2) you've created an "app specific password" for this service

Remove possible previous installations of sendmail, postfix, exim and qmail:

sudo apt-get remove packagename sendmail postfix exim qmail

Install postfix:

sudo apt-get install postfix
When presented with this screen, say that you want "No configuration"

Configure postfix with the values below:

sudo vim /etc/postfix/main.cf
# your preference may vary
inet_protocols = ipv4
inet_interfaces = loopback-only
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination

# your box hostname
myhostname = example-mail-box.wasteofserver.com
mydestination = $myhostname, localhost

# use Gmail with TLS as destination
relayhost = [smtp.gmail.com]:465
smtp_sasl_auth_enable = yes
smtp_use_tls = yes
smtp_tls_wrappermode = yes
smtp_tls_security_level = encrypt
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_password

# if something goes wrong you may want to uncomment these to check for logs
# on /var/log/mail.log and /var/log/mail.error
#debug_peer_list=smtp.gmail.com
#debug_peer_level=3

Minimum viable postfix configuration

Create a file with your Gmail account and access credentials:

sudo vim /etc/postfix/sasl/sasl_password

With the following content:

smtp.gmail.com example@gmail.com:your-app-password-here

And then convert it into a database file that postfix can read:

sudo postmap /etc/postfix/sasl/sasl_password

This command generates a /etc/postfix/sasl/sasl_password.db file that postfix can read

Restart the service:

sudo /etc/init.d/postfix restart

And now send an email using the command line to check if everything's working as it should:

echo "test email" | mailx -a "From: wasteofserver <no-reply@wasteofserver.com>" -s "this is a test email from wasteofserver" youremail@youraccount.com

Hopefully, you've got mail!

The logs should show something like this:

Jun 25 17:14:56 AEC1E9C0043: uid=0 from=<wasteofserver>
Jun 25 17:14:58 AEC1E9C0043: to=<you@gmail.com>, relay=smtp.gmail.com[173.194.76.109]:587, delay=2.2, delays=0.06/0.01/1.4/0.73, dsn=2.0.0, status=sent (250 2.0.0 OK  1624637698 v5sm11709310wml.26 - gsmt
Jun 25 17:14:58 AEC1E9C0043: removed

Notice Gmail as the relay server

Alternatively, you can also check your Gmail "sent folder". As the email is sent using your Gmail account (using SMTP), the email will also be there.


Edit: Thanks to u/kevdogger for pointing that in Arch Linux, the default is no longer B-tree maps but lmdb (Lightning Memory-Mapped Database) and, as such, you will need to change main.cf to point to the correct file.

The easiest way is to prefix the map with the actual type, something like this:

# different types of maps created by postmap, change your config file accordingly
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_password
smtp_sasl_password_maps = btree:/etc/postfix/sasl/sasl_password
smtp_sasl_password_maps = lmdb:/etc/postfix/sasl/sasl_password

postmap can create various types of maps, so adjust your configuration accordingly.


Last Christmas, my wife asked for walkie-talkies. I thought it would be one of those gifts that end up collecting dust, but decided to get her a high-quality set anyway. We now use them regularly—at least once a month—for just about everything. From road trips and hikes to beach outings, these walkie-talkies have become an essential part of our adventures.

When a product genuinely delivers on its promise, it makes all the difference. I can’t recommend these enough.