Blog

Configure Postfix to Use Gmail SMTP on Ubuntu

If you want to use a Gmail account as a free SMTP server on your Ubuntu-Linux server, you will find this article useful. This guide is tested with Ubuntu 12.04. If you face any issue, feel free to use comments-section below.

Relaying Postfix mails via smtp.gmail.com:

First, install all necessary packages:

sudo aptitude install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

If you do not have postfix installed before, postfix configuration wizard will ask you some questions. Just select your server as Internet Site and for FQDN use something like mail.example.com

Then open your postfix config file:

vim /etc/postfix/main.cf

and following lines to it:

relayhost = [smtp.gmail.com]: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

You might have noticed that we haven’t specified our Gmail username and password in above lines. They will go into a different file. Open/Create

vim /etc/postfix/sasl_passwd

And add following line:

[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD

If you want to use your Google App’s domain, please replace @gmail.com with your @domain.com

Fix permission and update postfix config to use sasl_passwd file:

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

Next, 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

Finally, reload postfix config for changes to take effect:

sudo /etc/init.d/postfix reload

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" you@example.com

To further verify, if mail sent from above command is actually sent via Gmail’s SMTP server, you can log into Gmail account USERNAME@gmail.com 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. This method will be useful if you have many sites on your server and want them all to send emails via Gmail’s SMTP server.

Alternatively, you can use a plugin like WP Mail SMTP so that mails from your particular WordPress site will be sent using Gmail’s SMTP server.

Please note that Gmail’s SMTP server has a limit of 500 emails per day. So use wisely! :-)

16 Comments…

 Share your views
  1. Excelent tutorial it work very well !!! Thank you so much.

  2. A great big thank you to you this worked brilliantly on my Ubuntu 12.04.2 LTS install

  3. I felt the need to say thank you since you were a big help – straightforward explanation that worked after the first try :)

  4. Excellent.. Thanks for saving time.

  5. Man ! I owe you big time :D thanks heaps , This is a lot simple and understandabale than any other thing I’ve found , It actually works ! KEEP UP THE GOOD WORK ! CHEERS !!!

  6. Avatar of Chris Walsh

    This was fantastically useful; thanks for taking the time to write it up.

  7. Excellent..thanks

  8. After configured everything and download the root cert from Thawte I am receiving the following error. Could you please help me?

    Action: failed
    Status: 5.7.0
    Remote-MTA: dns; smtp.gmail.com
    Diagnostic-Code: smtp; 530 5.7.0 Must issue a STARTTLS command first.
    f71sm25739776yha.8 – gsmtp

  9. Avatar of Bastian

    Anyone else facing an ‘Authenication Required’ error message when sticking to the tutorial (although setting up the password file worked flawlessly)?

  10. Worked Perfectly! Thanks!

Leave a Comment

  • Facebook
  • GitHub
  • Twitter
  • LinkedIn
  • Google

Your email address will not be published.

*

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

{ 2 Trackbacks }

  1. Using Postfix and free Mandrill email service for SMTP on Ubuntu Linux server (Pingback)
  2. Configuring postfix to send system mail to an external address (DynDNS) (Pingback)