Configure Ubuntu Server to Send Emails From Gmail Account


Once I got my ubuntu server up and running I want my server to send emails, and there are multiple ways to do it, but I want my emails to go directly into the inbox folder of the receiver not to the spam folder, and I want it done simply using a Gmail account.

Following this guide from the start will make you know how I’ve configured my ubuntu server to send emails from a Gmail account.

1. Remove Sendmail and Postfix:

To remove Postfix and Sendmail simply do the following commands:
sudo apt-get remove sendmail sendmail-bin postfix
sudo apt-get purge postfix exim4 sendmail sendmail-bin
sudo reboot


2. Relay Postfix mails via

Install all necessary packages:
sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

Postfix configuration wizard will ask you some questions. Just select your server as “Internet Site” and for FQDN use something like localhost.local or


3. Configure postfix:

Open your postfix config file:
nano /etc/postfix/

and change the following line:
relayhost =

relayhost = []:587

Then add the following lines to the end of the file:
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


4. Add Gmail username and password:

The Gmail username and password will go into a different file.
Open / Create:
nano /etc/postfix/sasl_passwd

And add following line:
Where is the required Gmail account, and PASSWORD is the Gmails account password
In case you have a Google Apps account, replace with


5. Fix permission and update postfix config to use sasl_passwd file:

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd


6. Validate certificates to avoid running into error:

Just run following command:
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem


7. Reload postfix config for changes to take effect:

sudo /etc/init.d/postfix reload


8. Testing 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 I often use Gmail when mail delivery is critical.

Tags: , , ,

One Response to “Configure Ubuntu Server to Send Emails From Gmail Account”

  1. edwardmcfellon says:

    Thanks for the tutorial. Worked like a charm. I dig the website layout as well!

Leave a Reply