Home Laravel How to Send Mail by using Mailable Class in Laravel 8?

How to Send Mail by using Mailable Class in Laravel 8?

by Shahriar Sagor
How to Send Mail by using Mailable Class in Laravel
How to Send Mail by using Mailable Class in Laravel

Hello Dev’s Today, here I want to give you a short and also a simple example of how to send email by using markdown mailable class in laravel 8 application. So it is very easy to send any mail with markdown good mail template design in laravel 8 application. Here we know How to Send Mail by using Mailable Class in Laravel 8 application.

Markdown mailable is provide us beautiful email layout bydefualt in laravel 8 application. Here we can use their template design to sending any email in laravel 8 application.

So we will give you a very simple example of mailable class to send email in laravel 8 application. Here you need to follow bellow step and you will get simple mail send example in laravel 8.

Step 1: Mail Configuration:

In first step we need to add our gmail smtp configuration in .env file.

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=nicesnippets@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

Step 2: Create Mailable Class

In Laravel 8 provide mailable artisan command to create a mail file and also view file create and we can design custom mail template. So need to fire bellow command:

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

Ok good, Now we can see new file in our 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

Now in this step we will add bellow route in our route file

routes/web.php

Route::get('my-test-mail','MyTestController@myTestMail');

Step 4: Create Controller File

Now in this step we will create a controller file by use artisan command. so now fire bellow command:

php artisan make:controller MyTestController

Step 5: Add Controller Method

Now, we need to create controller file and need to 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 = 'nicesnippets@gmail.com';
    	Mail::to($myEmail)->send(new MyTestMail());

    	
    	dd("Mail Send Successfully");
    }
}

Read Also : How to create a text file for JSON and file download in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook.

close

You may also like