Send email using Gmail SMTP in Laravel 5.*

Hi there, hope you are not in trouble with your application email sending feature. I am using mailtrap.io for email testing on the development server. It is good, but one problem is all email are going mailtrap’s Demo inbox, not to users individual email. And my client wants to check all email templates and functionality on the development server, with various emails and here I need to look for an alternative solution. By the way, I am using Laravel’s default email function.

So, here is the process that I follow and hope it will help you too. Don’t worry it’s simple.

Step – 1: Update the google account permission for allowing access less secure (third party) apps.

  • Go to your Google account dashboard by clicking on this link https://myaccount.google.com/security#connectedapps  or My Account button on Gmail
  • And take a look at the Sign-in & security -> Connected apps & sites -> Allow less secure apps settings.
  • You must turn the option “Allow less secure apps” ON.

Step – 2: First open .env file and update email setting parameters

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls

Look at the PORT and ENCRYPTION, I use 587 and tls (Transport Layer Security) respectively. Try to maintain this configuration, otherwise you will get the error. Though both are used as default in configuration (config/mail.php) file.

Step – 3:  If you get any exception error regarding OpenSSL certificate. Then you need to add following code to mail configuration file. Open mail.php file located in config/mail.php and add following code. In my case, I added at the bottom.

// send email locally – only for development server, do not use it on production server

‘stream’ => [
‘ssl’ => [
‘allow_self_signed’ => true,
‘verify_peer’ => false,
‘verify_peer_name’ => false,
],
],

Note: It is suggested, not to use this configuration on your production server, because of security purpose.

 

I am really glad if this information is helping anyone. So, let me know your feedback or any alternative way/solution you know for this issue.

I would glad to help you, please ping me at me[at]skpaul[dot]me if you need any support.

Advertisements