Press "Enter" to skip to content

Configure postfix on FreeBSD to use Gmail as an SMTP relay

I wanted postfix on my server to send mail through Gmail so messages don’t get market as spam. Rather than setup all the interesting mail security options on my server and within my mail domain, I’d rather leave all that to the experts at Google.

The best article I found on the subject shows exactly how to do this on multiple platforms: various types of Linux and on FreeBSD. I’m reproducing the instructions below just for FreeBSD so I have this info handy for myself and anyone who might be visiting my FreeBSD-focused site.

First, install the postfix_sasl package. You need the sasl version for the TLS support. All commands below are run as root.

# pkg install postfix-sasl

Very important: if this is your first time installing postfix, follow the detail instructions given by the package installer if you want to permanently replace sendmail on your system.  You only need one active mail daemon.

Edit the postfix configuration file located at /usr/local/etc/postfix/main.cf and add the following under the relayhost section:

relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/usr/local/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/mail/certs/cacert.pem

Create a new file at /usr/local/etc/postfix/sasl_password for your Gmail username/password by entering one line into that file in the following format:

[smtp.gmail.com]:587    myemail@mydomain.com:MyP@$$w0rdHere

Fix permissions on this file:

# chmod 600 /usr/local/etc/postfix/sasl_passwd

Process the password file by issuing the following command:

# postmap /usr/local/etc/postfix/sasl_passwd

Now restart postfix and send a test message

# service postfix reload
# mail -s "Test subject" recipient@anywhere.com

That’s it, you’re done. It’s worthwhile to check the postfix logs at /var/log/maillog to see if there are any errors that you need to correct. Thanks for reading this article.

Kelly

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *