YouTube Icon

Code Playground.

How to Send Email in Laravel 5.6 using Gmail SMTP


How to Send Email in Laravel 5.6 using Gmail SMTP

Today let's see how to use the gmail smtp server to send email in laravel 5.6. The platform comes with the clean, easy API since Laravel 5.3 to send mails easily via localhost or cloud-based service. You can simply build your emails using the ' Mailable ' class.

Laravel - Sending Email via Gmail SMTP

Laravel utilizes the config\mail.php file to store all the mail settings. Here is where you have to configure MAIL_DRIVER, MAIL_HOST, MAIL_PORT, etc.

But you don't have to directly make changes to this file. You can simply provide those details on the '.env' file found in the app root and laravel will automatically find the required settings.

Now below steps How to Send Email in Laravel 5.6 using Gmail SMTP

Step 1. First install the latest version of laravel which is 5.6 as of now. Open up the terminal and fire the below composer command to install it.

composer create-project laravel/laravel laravel_projet --prefer-dist

Here 'laravel_project' is your project folder.

Step 2. Next open up the '.env' file and configure the email settings.

Gmail SMTP Settings:

Host Name:
Smtp Port: 587(tls), 465(ssl)
Encryption: tls/ssl

Your '.env' file setting,


Step 3. Generate the mailable class. Fire up the below command on the command prompt.

php artisan make:mail MailUser

This will create 'MailUser.php' file inside 'App\Mail' folder.

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class MailUser extends Mailable
    use Queueable, SerializesModels;
    public $uname;

     * Create a new message instance.
     * @return void
    public function __construct($uname)
        $this->uname = $uname;

     * Build the message.
     * @return $this
    public function build()
        return $this->view('email.welcome');

Step 4. Next create the view file. This will contain the template for our email. Create 'email' folder inside 'views' and place 'welcome.blade.php' file inside it.

<!DOCTYPE html>
    <title>Welcome User</title>
  <h2>Hi crowdforgeeks<h2>

Step 5. Generate a controller file using artisan command.

php artisan make:controller UserCnr

Open the 'UserCnr.php' file inside 'App\Http\Controllers' folder and add send_mail() function to it. Also make sure to include the path for Mail Facade and MailUser class.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\MailUser;
class UserCnr extends Controller
	public function send_email()
	   $user_name = 'Name';
	   $to = 'to email address'
	   Mail::to($to)->send(new MailUser($user_name));
	   return 'Mail sent successfully';

Step 6. Finally add route for our UserCnr send_mail() function. Open 'routes\web.php' file and add the below route to it.

Route::get('/user/sendmail', 'UserCnr@send_email');

Now start the artisan server.

php artisan serve

Open the browser and enter the url http://localhost/laravel_project/user/sendmail

If you don't receive the email, don't worry. Do the following changes to your gmail settings.

Step 1. Login to your Google account and go to 'My Account' > 'Sign-in & Security'.

Step 2. Scroll down to the bottom and look for 'Allow less secure apps' option. If it is set 'OFF', then turn it 'ON'.

Now try it again and you'll receive the mail without any issues.

And if you like this tutorials please share it with your friends via Email or Social Media.