We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsOperating SystemsLinuxAdministration

How to Configure Postfix GMAIL SMTP Server on Your Ubuntu Server

If you want to use external SMTP server as your primary e-mail server, it is recommended to use the most popular e-mail service – Gmail – for this purpose. Gmail allows you to use its SMTP server as relay. This tutorial will explain how to configure Postfix to use Gmail as e-mail relay on Ubuntu 14.04 operating system.

Follow These Steps to Configure Postfix GMAIL SMTP Server on Your Ubuntu Server

First, update your operating system and install nano text editor:

sudo apt-get update
sudo apt-get install nano

<>Now you will have to install Postfix and required packages (mail utilities, ca certificates):

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

In most cases (especially if you are using OpenVZ template), Postfix will be installed on your operating system template by default. However, you will be asked some questions if it is not installed. You have to select Internet Site as server and type fully qualified domain name (FQDN) as your e-mail server, for example: mail.example.com.

Now if your installation passed without any issues or errors, you have to edit Postfix configuration file, which is located in /etc/postfix/main.cf:

sudo nano /etc/postfic/main.cf

Add [smtp.gmail.com]:587 as your relay host:

relayhost = [smtp.gmail.com]:587

Add the following lines to the configuration 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

You have not specified your gmail credentials yet, so now you have to create file where your gmail login credentials will be stored:

sudo touch /etc/postfix/sasl_passwd

Open newly created file with nano text editor:

sudo nano /etc/postfix/sasl_passwd

Add the following line:

[smtp.gmail.com]:587   GMAIL_USERNAME@gmail.com:GMAIL_PASSWORD

Fix file permissions and update Postfix configuration to use sasl_passwd file:

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

Next – validate your certificate in order to avoid any possible errors:

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

Finally, reload Postfix configuration for the changes to take effect:

sudo /etc/init.d/postfix reload

Your server is prepared to use Gmail service as your main SMTP service, try if it is working:

echo “Testing Gmail SMTP service” | mail -s “Test message from GMAIL” you@example.com

Gmail will refuse your connection by default due to security restrictions. Log in to your gmail account, where you will see a security notification about unauthorized access from unsecured server. Verify that you have issued this login and change security settings. You must visit the following link in order to do that:


And turn on unsecured software access.

Try to send the test e-mail once again – it should be sent without any issues. You may check your sent messages folder – all sent messages will be stored there.

NOTE: Do not forget that Gmail has a restriction of 500 e-mails per day.

TIP: Try to telnet smtp.gmail.com via 587 port if you are experiencing any issues. Contact your server administrator if you are unable to telnet it. Otherwise, it means that you have made a mistake during the configuration.



Rate this Tutorial:
No Comments

Other (3)

Popular Keywords