Configure Laravel to use Postfix via Gmail SMTP

Configure Laravel to use Postfix via Gmail SMTP

Configure Laravel to use Postfix via Gmail SMTP

Sep 21, 2015 – 2 min read

Description

This guide will help you to configure Laravel to use Postfix via Gmail SMTP on your Ubuntu server 14.04. Once configured, all emails from your server will be sent via Gmail.

Before you begin, you need the following:

  • Ubuntu installation
  • SSH access to the server
  • Installed and configured NGINX

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 select your server as “Internet Site” and for FQDN set a sub-domain of your website. For example: mail.example.com. Then open Postfix config file:

nano /etc/postfix/main.cf

Add the following lines:

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

Save and Close.

We have to specify our Gmail username and password. Open or Create the following file:

touch /etc/postfix/sasl_passwd
nano /etc/postfix/sasl_passwd

and add the following line (replace USERNAME and PASSWORD with your gmail credentials):

[smtp.gmail.com]:587    [email protected]:PASSWORD

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 the certificates and avoid running into an error.

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

Reload Postfix:

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" [email protected]

To confirm that, log in to your Gmail account and open “Sent Mail” folder. You should see the test email.

Troubleshooting

If you get the following error “Error: SASL authentication failed; server smtp.gmail.com” during the test, you need to unlock the Google Captcha by visiting this page – https://www.google.com/accounts/DisplayUnlockCaptcha.

Laravel Setup

Open the mail config file which is located in Laravel’s config folder:

nano app/config/mail.php

Change the driver option to “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',

After we have configured Laravel to use Postfix, test the sending of emails via Laravel.

If you need assistance with the above or Laravel, contact me.

(The header image is taken from DigitalOcean website.)