Laravel 9 Send Email using Markdown Example

10-Apr-2023

.

Admin

Laravel 9 Send Email using Markdown Example

Hello Friends,

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

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

In this article, I am going to tell you how to send a simple email with gmail smtp configuration using laravel 9 mailable class. It is a very simple and the best way.

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

Step 1: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Step 2: Mail Configuration

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

.env

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=your email

MAIL_PASSWORD=your password

MAIL_ENCRYPTION=tls

Step 3: Add Mailable Class with Markdown

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

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

Now you can see a 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 4: Add Route

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

routes/web.php

<?php

use App\Http\Controllers\HomeController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

Step 5: Add Controller

php artisan make:controller HomeController

Now, we will add myDemoMail() in the "HomeController" Controller file, in this file we will write the code of mail send, so if you haven't created HomeController then create the 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 6: Add Blade File

In the last step, we will create email template file, so first create the "emails" folder in your resources folder and create the 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

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/my-demo-mail

It will help you...

#Laravel 9