Pavel Tashev
Pavel Tashev / Entrepreneur and Software Developer
Constantly learning new things. I love to be part of projects and StartUps that inspire me and improve the life of society.
Contact me via LinkedIn or Twitter
Isle of Man, UK and Bulgaria
Configure Postfix to Use Gmail SMTP on Ubuntu

Configure Postfix to Use Gmail SMTP on Ubuntu

Configure Postfix to Use Gmail SMTP on Ubuntu This guide will help you to use your Gmail account as a SMTP server on your Ubuntu server 14.04. Once configured, all emails from your server will be sent via Gmail. This method is useful if you have a web site on your server and you want it to send emails via Gmail (please take into the email sending limits of your google account).

This is also helpful because you will avoid the possibility to be included in the spam and block lists if you send your emails by your own.

The reason to write this post is because I had an issue with my Laravel 4 installation on my Ubuntu server with combination with Nginx (web service). It turns out that Nginx requires additional setup if you want to send emails. So, that’s why in this article I will explain how to set up your Gmail SMTP on Ubuntu and after that I will explain how to execute few configuration required by Laravel.

Before you begin, you need the following:

  • • Ubuntu installation
  • • SSH access to the server
  • • Configured Nginx installation

Postfix installation and configuration

Install all necessary packages if you don’t have Postfix installed:

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

During the installation just select your server as Internet Site and for FQDN use something like mail.example.com.

Then open your postfix config file:

nano /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

We now have to specify our Gmail username and password. Open/Create:

nano /etc/postfix/sasl_passwd

And add following line:

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

You can replace @gmail.com with your own domain name @domain.com.

Set the permissions below and update Postfix config to use sasl_passwd file:

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

Run the command below in order to validate certificates to avoid running into error.

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

Reload the Postfix config:

sudo /etc/init.d/postfix reload

Test Postfix and Gmail SMTP

If you have configured everything correctly, the following command should generate a test email:

echo "Postfix test" | mail -s "Test Postfix with Gmail SMTP" myemail@example.com

To further verify, if the mail sent from above command is actually sent via Gmail’s SMTP server, log into your Gmail account and open “Sent Mail” folder. The good thing here is that each email sent by Gmail has a copy in this folder.

Troubleshooting

Error: “SASL authentication failed; server smtp.gmail.com”

You need to unlock the captcha by visiting this page:
https://www.google.com/accounts/DisplayUnlockCaptcha

Laravel Setup

The configuration required by Laravel in order to send emails with Postfix is simple. Open:

nano app/config/mail.php

Edit the driver option to be sendmail:

/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
|
*/
'driver' => 'sendmail',

That’s from me for now. As always, any feedback in the comments is welcome!

Share me please:

Leave a Reply

r0Ux

Please type the text above: