How To Send Email Using Markdown Mailable Laravel 9

Websolutionstuff | Aug-05-2022 | Categories : Laravel

In this article, we will see how to send email using markdown mailable laravel 9. we will learn laravel 9 to send email using markdown mailables. 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.

So, let's see, send email in laravel 9 using markdown mailables

Step 1: Install Laravel 9

In this step, we will install laravel using the composer command.

composer create-project laravel/laravel email-example

 

 

Step 2: Setup .env file Configuration

Now, we will set up mail configuration in the .env file.

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

 

Step 3: Create Mailable Class With Markdown

In this step, we will create a mailable class with markdown using the laravel artisan command.

php artisan make:mail MarkdownSendMail --markdown=emails.markdownDemoMail

This command will create a new file app/Mail/MarkdownSendMail.php.

<? php

namespace App\Mail;
   
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
  
class MarkdownSendMail extends Mailable{

    use Queueable, SerializesModels;

    public $user;

    public function __construct($user){

        $this->user = $user;

    }

    public function build(){

        return $this->subject('This is markdown mail example')
                    ->markdown('emails.markdownDemoMail');
    }
}

 

 

Step 4: Create Controller

In this step, we will create a controller and send the mail.

php artisan make:controller SendMailController

app/Http/Controllers/SendMailController.php

<?php

namespace App\Http\Controllers;

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

class SendMailController extends Controller
{   
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {   
        $user = [
        'name' => 'Websolutionstuff',
        'info' => 'https://websolutionstuff.com/'
    ];

    \Mail::to('[email protected]')->send(new MarkdownSendMail($user));

    dd("Successfully send mail..!!");

    }
}

 

Step 5: Create Blade File

Let's create a file under resources/views/emails/markdownDemoMail.blade.php and add this code.

@component('mail::message')
# {{ $mailData['title'] }}
  
The body of your message.
  
@component('mail::button', ['url' => $mailData['info']])
Visit Our Website
@endcomponent
  
Thanks,

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

 

 

Step 6: Create Route

In this step, we will create a route for sending emails.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\SendMailController;
  
/*
|--------------------------------------------------------------------------
| 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('send-mail', [SendMailController::class, 'index']);

 


You might also like:

Recommended Post
Featured Post
How To Disable Past Date In jQuery Datepicker
How To Disable Past Date In jQ...

In this tutorial, we will see how to disable past dates in jquery datepicker. In the date picker, today's date...

Read More

Jun-18-2022

How to Upgrade PHP 8.0 to 8.1 in Ubuntu
How to Upgrade PHP 8.0 to 8.1...

Hey there! I recently needed to upgrade my PHP version from 8.0 to the latest 8.1 on my Ubuntu server. It's a smart...

Read More

Nov-03-2023

Next Previous Link Button Pagination in Laravel
Next Previous Link Button Pagi...

Today we will learn next previous link button pagination in laravel, Using paginate method you can easily create paginat...

Read More

Jun-14-2021

How To Upload Large CSV File Using Queue In Laravel 9
How To Upload Large CSV File U...

In this article, we will see how to upload a large CSV file using queue in laravel 9. Here we will learn large numb...

Read More

Sep-16-2022