In this video we are going to learn about how to send email using gmail in laravel 8.
So lets see how can we send an email using gmail in laravel 8.
First of all lets create new controller.
So switch to command prompt and run the command.

php artisan make:controller MailController

Now create mail class.
For that run the following command.

php artisan make:mail TestMail

Now switch to project and make configuration for email.
So go to the .env file gmail account details as following.

MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=testemail@gmail.com
MAIL_PASSWORD=12345678
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=testemail@gmail.com
MAIL_FROM_NAME=\"${APP_NAME}\"

Now go to the gmail and just click on Account icon.
Now just click on security.
Then scroll the page from here.
Make less secure app access on.
Alright now switch to the command prompt and re-run the application.
Then just type command.

php artisan serve

Now switch to the project and just go to the app directory and then mail.
From here just open TestMail.php and here write the following code.



public $details;

public function __construct($details)
{
$this->details = $details;
}

public function build()
{
return $this->subject('Test Mail From Surfside Media')->view('emails.TestMail');
}


Now lets create a view.
Go to the views directory and here lets create new folder emails.
Inside this emails directory lets create new file TestMail.blade.php and write the following code.


<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\">
<title>Test Mail</title>
</head>
<body>
<h1>{{ $details['title'] }}</h1>
<p>{{ $details['body'] }}</p>
<p>Thank you</p>
</body>
</html>



Now switch to project and just open MailController.
Inside the MailController create a function.


public function sendEmail()
{
$details = [
'title' => 'Mail from Surfside Media',
'body' => 'This is for testing email using gmail'.
];
\\Mail::to('your_receiver_email@gmail.com')->send(new TestMail($details));
return \"Email is Sent.\";
}


Also import Mail so befor the class just write the following.


use App\\Mail\\TestMail;


Now lets create route for this function.
So go to the web.php and here just create a new route.

Route::get('/send-mail',[MailController::class,'sendEmail']);

Alright now lets check.
So switch to the browser.
Just go to the / send-mail.
Email sent.
Now Lets check email into gmail.
So to the my gmail account.
Now just refresh the page.
you can see the mail. Lets open it.
Here you can see the title and message.
So in this way you can send email using gmail in laravel 8.