Laravel 7.x Markdown Templates Email Example

In this tutorial i will show you how to send email in laravel using markdown. Markdown mailable messages allow you to take advantage of the pre-built templates and components of mail notifications in your mailables.

Since the messages are written in markdown, Laravel is able to render beautiful, responsive HTML templates for the messages while also automatically generating a plain-text counterpart.

Laravel Markdown provides components, tables, email link, button, embed image etc. Markdown beautiful layout you can use with email template. In this tutorial i will discuss from scratch abou laravel markdown email template.

So in this laravel mailable example tutorial we will see laravel mail markdown components. So let's see how we can send email using markdown mailable class in laravel 7.
            

laravel-6-send-email-mailable

In this tutorial, i am going to tell you how to send simple email with gmail smtp configuration using laravel 7 mailable class.

It is very simple and best way. you have to just follow few step and you will get simple mail send example in your laravel 7 application.

Read More : How to Install Botman Chatbot in Laravel

Here we are share with you email send in laravel example when user register then after send one confirmation link on use email address

Step : 1 Create application

Install the new Laravel Project by the running following command.

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

 

Step : 2 Create route for send mail

Now we have to create our route, so to create route paste this following code

Route::get('sendemail', '[email protected]')->name('sendemail');

 

Step : 3 Create SendEmailController

Now, we are need to create SendEmailController.php file in app/Http/Controllers folder

 app/Http/Controllers/Auth/SendEmailController.php

namespace App\Http\Controllers;

use App\Mail\TestMail;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SendEmailController extends Controller
{
    public function ship(Request $request)
    {
        $valueArray = [
        	'name' => 'John',
        ];
        
        try {
            \Mail::to('[email protected]')->send(new TestMail($valueArray));
            echo 'Mail send successfully';
        } catch (\Exception $e) {
            echo 'Error - '.$e;
        }
    }
}

Step : 4 Create email markdown 

 

to create markdown mail just run below command.

php artisan make:mail TestEmail--markdown=emails.testmail

Here, we are create new one markdown email as well as email templete blade using following command.

 

After run above command you can see TestMail.php file automatice created in app/Mail folder and your email templet/blade file testmail.php is created in resources/views/emails folders

app/Mail/TestMail.php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class TestMail extends Mailable
{
	use Queueable, SerializesModels;
	
	public function __construct($content)
	{
		$this->content = $content;
	}

	public function build()
	{
		return $this->markdown('emails.testmail') //pass here your email blade file
	    	->with('content',$this->content);
	}
}

 

Now oprn your resources/views/emails/testmail.blade.php file and make changes look like.

@component('mail::message')
# Dear, {{$content['name']}}

You are receiving this email because we received a signup request for your this mail account.

@component('mail::button', ['url' => 'website.com')])
Click Here
@endcomponent

If you did not request a signup , no further action is required.

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

 

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

 

Now you can open bellow URL on your browser:

http://localhost:8000/sendemail

 

Hope it can help you.