Gmail SMTP relay with Postfix

The following configuration has been performed in Debian but should apply in any Linux distro with Postfix installation. The task quite simple: use Gmail as SMTP relay for outgoing email traffic.

1) At first place I had to install the following packages and turn off sendmail:

apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
service sendmail stop

2) Then edit the configuration file at /etc/postfix/main.cf by adding the following options at the end of the file:

# Gmail SMTP relay
relayhost = [smtp.gmail.com]:587
smtp_use_tls=yes
smtp_sasl_auth_enable = yes 
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_sasl_security_options =

3) Create the authentication file at /etc/postfix/sasl_passwd and set the right permissions:

[smtp.gmail.com]:587    <username>@gmail.com:<password>
chmod 400 /etc/postfix/sasl_passwd

Make sure the file is owned by the user who runs the Postfix daemon.
4) Reload Postfix:

service postfix reload

5) Confirm the configuration is actually working by sending a test mail:

echo "Test mail from postfix" | mail -s "Test Postfix" pkritikakos@isbs.gr

The sender of the email should be the given Gmail account. Also, in the “Sent” folder of the Gmail account you should see the email you have sent with Postfix.

If your domain is using Gmail’s infrastructure for handling emails, you can replace @gmail.com with your domain and use the corresponding account details.

NOTE: This configuration will be sending any system message as “username@gmail.com” and therefore is not advised to be used in multi-user environment.

3 thoughts on “Gmail SMTP relay with Postfix

  1. This is more or less what I use too, for machines running in my home network to be able to send me stuff like logwatch output and cron job notifications. Just a small addition: when you restart postfix after you have finished with the configuration, it will hash /etc/postfix/sasl_passwd in the binary file /etc/postfix/sasl_passwd.db and use that one. You can then delete /etc/postfix/sasl_passwd, and not have your credentials in clear text any more.

  2. panoskrt

    Correct Marios, forgot to write that down. You can also hash the file before reloading and delete it immediatly: `postmap /etc/postfix/sasl_passwd`.

  3. anon564

    Unfortunately this doesn’t work on my Mac OS X. I get all kinds of weird error messages in my mail.log. I tried playing around with stuff like the certs, which my computer did not have, so I downloaded a SystemRootCertificates.keychain file I found on the internet but that didnt help much either. I get a new error message from google now complaining that the hostname doesnt match for the certificate. I spent a good couple hours trying to get this to work and I am just about ready to give up. A friend of mine had set up a mac with postfix using gmail as the smtp and it worked fine, but i have no idea how he did that.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s