Laravel 10 Markdown | Laravel 10 Send Email using Markdown Mailables

Laravel 10, Markdown provides a simple and intuitive syntax for formatting text. By using Markdown, you can easily structure your email content, add headings, lists, links, and other formatting elements without writing raw HTML code.

You can define the structure and styling of your emails in Markdown files, keeping your codebase organized and maintaining a clear distinction between the email's presentation and its underlying functionality.

Step 1: Install Laravel App

composer create-project laravel/laravel example-app

Step 2: Database Configuration

.env 

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 3: Create Mailable Class with Markdown

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

now, let's update code on MyDemoMail.php file as bellow:

app/Mail/MyDemoMail.php

<?php
  
    namespace App\Mail;
      
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Mail\Mailable;
    use Illuminate\Mail\Mailables\Content;
    use Illuminate\Mail\Mailables\Envelope;
    use Illuminate\Queue\SerializesModels;
      
    class MyDemoMail extends Mailable
    {
        use Queueable, SerializesModels;
        public $mailData;
      
        /**
         * Create a new message instance.
         */
        public function __construct($mailData)
        {
            $this->mailData = $mailData;
        }
      
        /**
         * Get the message envelope.
         */
        public function envelope(): Envelope
        {
            return new Envelope(
                subject: 'Mail from ItSolutionStuff.com',
            );
        } 
      
        /**
         * Get the message content definition.
         */
        public function content(): Content
        {
            return new Content(
                markdown: 'emails.myDemoMail',
            );
        }
      
        /**
         * Get the attachments for the message.
         *
         * @return array
    
         */
        public function attachments(): array
        {
            return [];
        }
    }

Step 4: Create Controller

php artisan make:controller MailController

app/Http/Controllers/MailController.php

<?php
  
    namespace App\Http\Controllers;
       
    use Illuminate\Http\Request;
    use Mail;
    use App\Mail\MyDemoMail;
      
    class MailController extends Controller
    {
        /**
         * Write code on Method
         *
         * @return response()
         */
        public function index()
        {
            $mailData = [
                'title' => 'Mail from webthestuff.com',
                'url' => 'https://www.webthestuff.com'
            ];
             
            Mail::to('to_your_email@gmail.com')->send(new MyDemoMail($mailData));
             
            dd("Email is sent successfully.");
        }
    }

Step 5: Create Routes

routes/web.php 

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

Step 6: Create Blade View

resources/views/emails/demoMail.blade.php 

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

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

Run Laravel App:

php artisan serve

Now, Go to web browser, type the given URL and see the output:

http://localhost:8000/send-mail