Routing email through GMail on OSX

Like most geeks, I have scripts that I’ve written that I like to have run from cron on a regular basis. And since the running of these scripts might be in the middle of the night, I like for them to email their output to me so I know if they succeeded or failed. As such, I need an MTA on my computer that can actually deliver these emails to GMail. For me, this is trivial using Sendmail or SSMTP on a Linux box, but I can never remember how to do this using Postfix on OSX. So after having to Google everything to get this running once more, I’m going to commit the steps here for my future self to reference :)

The first thing we’re going to do is open an iTerm and sudo -i to become root. Then we’re going to vi /etc/postfix/main.cf and we’re going to add/set the following:

myhostname = smtp.gmail.com
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_sasl_mechanism_filter = plain
smtp_use_tls = yes

I have Two-Factor Auth (2FA) enabled on my Google account (you should too!), so for me, the next step is to log into Google and create an ‘app password’. Once I have that password in hand, we vi /etc/postfix/sasl_passwd and add:

[smtp.gmail.com]:587 username@gmail.com:app_passwd

Secure that file by running chmod 0600 /etc/postfix/sasl_passwd and then have Postfix hash it by doing postmap /etc/postfix/sasl_passwd. Finally, restart Postfix:

launchctl stop org.postfix.master
launchctl start org.postfix.master

And everything should work.