Hello Devs,


In this tutorial, we are going to learn how to send mail using markdown example in laravel 7.

Follow this step by step guide given below:



Step: 1


 Install Laravel 7

composer create-project --prefer-dist laravel/laravel blog

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourgoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls



Step 2: 


Create Mailable Class with Markdown

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

app/Mail/MyTestMail.php

<?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;
    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->markdown('emails.myTestMail')
                    ->with('details', $this->details);
    }
}



Step 3: 


Create Route routes/web.php

Route::get('my-Test-mail','HomeController@myTestMail');



Step 4: 


Create Controller app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\MyTestMail;
use Mail;

class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function myTestMail()
    {
        $myEmail = 'your_receiver_email@gmail.com';
   
        $details = [
            'title' => 'Mail Test from Nicesnippets.com',
            'url' => 'https://www.nicesnippets.com'
        ];
  
        Mail::to($myEmail)->send(new MyTestMail($details));
   
        dd("Mail Send Successfully");
    }
}



Step 5: 


Create View File resources/views/emails/myTestMail.blade.php

@component('mail::message')
# {{ $details['title'] }}
  
The body of your message. 
   
@component('mail::button', ['url' => $details['url']])
Button Text
@endcomponent
   
Thanks,

{{ config('app.name') }}
@endcomponent


Run this command:

php artisan serve

Open this URL:

http://localhost:8000/send-mail


I hope this example helps you.