How to Send Email in Laravel 5.6 using Gmail SMTP

yotube
0

Hi! Today let's see how to send email in laravel 5.6 using gmail smtp server. Since Laravel 5.3, the framework comes with the clean, simple API to send mails quickly via localhost or cloud based service. You can simply use the 'Mailable' class to build your emails.

These classes collect the data and pass it to the view. Any property set as public will be available in the view file. So all you have to do is to set the class property as public and access it via the blade template. You can also send customized data explicitly using the with() method. Please refer the laravel documentation for more details.

send email laravel gmail smtp server

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.

Here go the steps to send mail using gmail smtp server in laravel.

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_demo --prefer-dist

Here 'laravel_demo' is your project folder.

STEP-2) Next open up the '.env' file and configure the email settings. I'm going to use gmail server for sending the email. So we must provide the gmail smtp details here.

Gmail SMTP Settings:


Host Name: smtp.gmail.com
Smtp Port: 587(tls), 465(ssl)
Encryption: tls/ssl

Your '.env' file should look like this,


MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=your_gmail_id
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=ssl

STEP-3) Then generate the mailable class. Fire up the below command on the command prompt.


php artisan make:mail WelcomeUser

This will create 'WelcomeUser.php' file inside 'App\Mail' folder. We have created this to send welcome email to user. This class is going to contain only one property which is 'uname'. This member variable represents the user's name which will be passed from the controller.

WelcomeUser.php

<?php
namespace App\Mail;

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

class WelcomeUser 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.

welcome.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Welcome User</title>
</head>
<body>
<div>
<h2>Hi {{ $uname }}! Welcome to KodingMadeSimple.com<h2>
</div>
</body>
</html>

Above we have a very simple template that displays welcome message to the user.

STEP-5) Next generate a controller file with the below artisan command.


php artisan make:controller UserController

Open the 'UserController.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 WelcomeUser class.

UserController.php

<?php
...

use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeUser;

...

public function send_email()
{
$user_name = 'John Anderson';
$to = 'john.anderson@gmail.com'
Mail::to($to)->send(new WelcomeUser($user_name));
return 'Mail sent successfully';
}
?>

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


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

Done! Now start the artisan server.


php artisan serve

Open the browser and enter the url http://localhost:8000/user/sendmail and if everything went right you'll see the below screen.

laravel send email using mailable

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

Gmail Settings:

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

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.

Read Also:

You can even queue the emails. Just call the queue() method instead of send() with Mail::to. Likewise you can send emails in laravel via gmail smtp. I hope you find this post useful. Please share it on social media if you like it.

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top