Send Mail using Gmail SMTP Server Laravel 8 Example.

10-Apr-2023

.

Admin

Send Mail using Gmail SMTP Server Laravel 8 Example.

Hi Friends,

It's simple example of laravel 8 sending mail gmail.

We will look at example of laravel 8 send email using google gmail. This post will give you simple example of gmail smtp send email in laravel 8.

I am going to show you example of laravel 8 send mail using gmail smtp.

You can create blade file design and also with dynamic information for mail layout.

In this tutorial, i will give you step by step instruction to send email using google gmail smtp server in laravel 8.

Let's start to example.

Step 1 : Add Configuration


You add send mail configuration with mail driver as gmail server, mail host, mail port, mail username, mail password so laravel 8 will use sender details on email.

env.php

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=yourmail@gmail.com

MAIL_PASSWORD=password

MAIL_ENCRYPTION=tls

MAIL_FROM_ADDRESS=yourmail@gmail.com

MAIL_FROM_NAME="${APP_NAME}"

First You have to enable google security setting form your gmail.

Second go to Google account and click on ‘Account’. Once you are on the ‘Account’ page, click on ‘Security’. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set as ON.

Step 2 : Create Mail

In this step we will create mail class MyDemoMail for email sending. Here we will write code for which view will call and object of user. So let's run bellow command.

php artisan make:mail MyDemoMail

app/Mail/MyDemoMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

class MyDemoMail extends Mailable

{

use Queueable, SerializesModels;

public $details;

/**

* Create a new message instance.

*

* @return void

*/

public function __construct($details)

{

$this->details = $details;

}

/**

* Build the message.

*

* @return $this

*/

public function build()

{

return $this->subject('Mail from Nicesnippets.com')

->view('emails.myDemoMail');

}

}

Step 3 : Create View Blade

In this step, we will create blade view file and write email that we want to send.

Now we just write some dummy text. create bellow files on "emails" folder.

resources/views/emails/myDemoMail.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Nicesnippets.com</title>

</head>

<body>

<h1>{{ $details['title'] }}</h1>

<p>{{ $details['body'] }}</p>

<p>Thank you</p>

</body>

</html>

Step 4 : Add Route

Now at last we will create "MyDemoMail" for sending our test email. so let's create bellow web route for testing send email.

routes/web.php

Route::get('send-mail', function () {

$details = [

'title' => 'Mail from Nicesnippets.com',

'body' => 'This is a test email using SMTP'

];

\Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\MyDemoMail($details));

dd("Mail Sent Successfully.");

});

Run Your Project:

php artisan serve

http://localhost:8000/send-mail

#Laravel 8

#Html

#PHP