Laravel 6 Send Mail using Mailable Class

Admin   Laravel   128  2021-03-17 07:20:05

Hi Dev,

Today, I want to give you example of how to send email using markdown mailable class in laravel 6. It is very easy to send mail with markdown good mail template design in laravel 6.

markdown mailable provide beutiful email layout by defualt in laravel 6. we can use their template design to sending email in laravel 6.

We will give you very simple example of mailable class to send email in laravel 6 application.You have to follow bellow step and you will get simple mail send example in laravel 6.

Step 1: Mail Configuration

In first step you can to add your gmail smtp configuration in .env file.

.env

 

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

[email protected]

MAIL_PASSWORD=mypassword

MAIL_ENCRYPTION=tls

Step 2: Create Mailable Class

Laravel 6 provide mailable artisan command to create mail file and view file create and you can design custom mail template. so fire bellow command:

 

php artisan make:mail MyTestMail --markdown=emails.myDemo

Ok, Now you can see new file in your app(app/Mail/MyTestMail.php) folder. So, open that file and put bellow code.

 

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

class MyTestMail extends Mailable

{

use Queueable, SerializesModels;

/**

* Create a new message instance.

*

* @return void

*/

public function __construct()

{

//

}

/**

* Build the message.

*

* @return $this

*/

public function build()

{

return $this->markdown('emails.myDemo');

}

}

Step 3: Add Route

In this step you will add bellow route in your route file

routes/web.php

 

Route::get('my-test-mail','[email protected]');

Step 4: Create Controller File

In this step we will create controller file to use artisan command. so fire bellow command:

 

php artisan make:controller MyTestController

Step 5: Add Controller Method

Now, we will create controller file and add myTestMail() method in "MyTestController" or put bellow code in MyTestController.

app/Http/Controllers/MyTestController.php

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Mail;

use App\Mail\MyTestMail;

class MyTestController extends Controller

{

public function myTestMail()

{

$myEmail = '[email protected]';

Mail::to($myEmail)->send(new MyTestMail());

dd("Mail Send Successfully");

}

}

It will help you....