Using Google as a Postfix relay

Scenario: You're working from a home ISP connection or traveling, but would like to have your Mac send emails for jobs that have completed or other misc. notices without having to reconfigure each time. Since many ISPs block outbound SMTP servers on standard ports, forcing you to use their servers, this can be a headache.

This article make some assumptions:

I decided to use Google's App/SMTP server to get around this as it uses a non-standard SMTP port and requires authentication. There are tons of guides on how to do this online, but none of them seemed to work for my setup. So, without further ado, here's how to setup pretty much any linux system, leveraging Google servers as a relay via Postfix:

First edit /etc/postfix/main.cf and add this to the end of /etc/postfix/main.cf:

$ sudo vi /etc/postfix/main.cf

Note: the brackets in the below examples "[]" are required around the hostname. The significance is that it disables the Postfix MX lookup for that host, and defaults to the provided hostname as the nexthop for mail exchange. It's also a requirement if you specify only an IP address.

myhostname = _HOSTNAME_
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
smtp_sasl_mechanism_filter = plain

Also, edit this line, adjust to your preference, default is 1MB. I change mine to 100MB since I send large logfiles:

message_size_limit = 100000000

Edit/create /etc/postfix/sasl_passwd. You'll need to login to your Google account and create an app password. Spaces from this password are not required in this file:

$ sudo vi /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 user@domain:app_password

Modify the permissions of the password file to protect the contents:

$ sudo chmod 600 /etc/postfix/sasl_passwd

Then finally hash the password, this will create a sasl_passwd.db file:

$ sudo postmap /etc/postfix/sasl_passwd

Restart the postfix services for the change to take effect:

$ sudo systemctl stop postfix
$ sudo systemctl start postfix

And finally run a test:

$ date | mail -s "test 1 again” you@yourdomain.com

Check the queue for errors. Should report "Mail queue is empty” if everything went well.

$ date && postqueue -p

If you received the message, then you’re all set. Happy mailing!