Laravel 8 Send Email using Markdown Example

Laravel 8 , Laravel 7 , Laravel , Laravel 6

Nicesnippets

187

23-04-2021


Laravel 8 Send Email using Markdown Example

Hello Friends,

In this article, I will show you how to send mail using markdown in laravel 8 application. We will learn laravel 8 send email using markdown. I am going to show you example of laravel 8 send markdown email. it's simple example of laravel 8 mail send markdown. you can understand a concept of laravel 8 send mail using markdown.

Laravel Markdown provides components, tables, email link, button, embed image etc. Markdown beautiful layout you can use with email template.

In this Article, i am going to tell you how to send simple email with gmail smtp configuration using laravel 8 mailable class. It is very simple and best way.

Here i will give you full example for laravel 8 send email using markdown example. So let's see the bellow example :

Step 1: Set Mail Configuration


In first step, We have to add your gmail smtp configuration like your username, password etc, so open your .env file and add your configration.

.env

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=itsolutionstuff@gmail.com

MAIL_PASSWORD=mypassword

MAIL_ENCRYPTION=tls

Step 2 : Create Mailable Class with Markdown

In this step, You have to create new mailable class that way we can use simply like laravel event, you can re-use anywhere in your laravel application. So first create Mailable class using artisan command, so run bellow command:

php artisan make:mail MyDemoMail --markdown=emails.myDemoMail

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

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->markdown('emails.myDemoMail')

->with('details', $this->details);

}

}

Step 3 : Add Route

In this step, we will add new route for out testing mail so open your web route file and add bellow route.

routes/web.php

use App\Http\Controllers\HomeController;

// Email

Route::get('my-demo-mail', [HomeController::class, 'myDemoMail']);

Step 4 : Create Controller File

Now, we will add myDemoMail() in "HomeController" Controller file, in this file we will write code of mail send, so if you haven't created HomeController then create HomeController.php file and put bellow code.

In $myEmail variable, you can set your own email for testing mail.

app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Mail\MyDemoMail;

use Mail;

class HomeController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function myDemoMail()

{

$myEmail = 'abc@abc.com';

$details = [

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

'url' => 'https://www.nicesnippets.com'

];

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

dd("Mail Send Successfully");

}

}

Step 5: Add View File

In last step, we will create email template file, so first create "emails" folder in your resources folder and create myDemoMail.blade.php file and put bellow code.

resources/views/emails/myDemoMail.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

Now we are ready to run our send email using markdown example with laravel 8 so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

localhost:8000/my-demo-mail

It will help you....