Setting up postfix to use Gmail SMTP under Ubuntu

If you want to use a gmail account as a free SMTP server on your Ubuntu server, you will find this article useful. This guide is tested with Ubuntu 12.04 (updated for 15.10).

Required Packages

First, install all necessary packages:

# sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

If this is the first time install of postfix, the configuration wizard will run on install and asks you a couple of questions. Simply select your server as Internet Site and for FQDN use something like Replace with your domain

Setup Postfix

Open your postfix config file:

# vi /etc/postfix/

add the following lines to it:

relayhost = []:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

Open or create the sasl_passwd file to include your Gmail username and password:

# vi /etc/postfix/sasl_passwd

And add following line:


If you want to use your Google App’s domain, please replace with your

Fix permission and update postfix config to use sasl_passwd file:

# chmod 0400 /etc/postfix/sasl_passwd
# postmap /etc/postfix/sasl_passwd

Next, validate certificates to avoid running into error. Just run following command:

For Ubuntu 12.04, use:

# cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | tee -a /etc/postfix/cacert.pem

For Ubuntu 14.04, 15.10, use:

# cat /etc/ssl/certs/thawte_Primary_Root_CA.pem | tee -a /etc/postfix/cacert.pem

Finally, reload postfix config for changes to take effect:

# /etc/init.d/postfix reload


Check if mails are sent via Gmail SMTP server

If you have configured everything correctly, following command should generate a test mail from your server to your mailbox.

# echo "Test mail from postfix" | mail -s "Test Postfix"

To further verify, if mail sent from above command is actually sent via Gmail’s SMTP server, you can log into Gmail account with PASSWORD and check “Sent Mail” folder in that Gmail account. By default, Gmail always keeps a copy of mail being sent through its web-interface as well as SMTP server. This logging is one strong reason that we often use Gmail when mail delivery is critical.

Once configured, all emails from your server will be sent via Gmail.