Laravel 10 Send Mail using Markdown Template

Websolutionstuff | Dec-04-2023 | Categories : Laravel PHP

Hey everyone! Ever wondered how to make your Laravel emails look sleek and professional? In this guide, I'll walk you through the simple steps of sending emails using Markdown templates in Laravel 10. Let's make your email communication stand out!

In this post, we will explore how to send Markdown mail in Laravel 10. The article presents a straightforward example of sending markdown-formatted emails in Laravel 10, demonstrating the process of sending emails using mailables.

Laravel 10's Markdown feature comes with built-in, predefined mail templates and components for crafting stylish emails, including tables, links, buttons, and embedded images. Upgrade your email communication with the versatility of Laravel 10 Markdown.

So, let's see sending markdown emails in Laravel 8, Laravel 9, and Laravel 10.

Step 1: Install Laravel 10

If you haven't already, start by creating a new Laravel project using Composer:

composer create-project laravel/laravel your-project-name
cd your-project-name

 

Step 2: Add Mail Configuration

Configure mail configuration in the .env file.

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

 

Step 3: Generate the Mailable Class with Markdown

 Run the following Artisan command to generate a new Mailable class. This class will use Markdown for email formatting:

php artisan make:mail TestMail --markdown=emails.testMail

After running the command, navigate to the app/Mail directory in your Laravel project. You will find a new file named TestMail.php. Open this file to customize your Mailable class.

<?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 TestMail extends Mailable
{
    use Queueable, SerializesModels;
    public $details;

    public function __construct($details)
    {
        $this->details = $details;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'This E-mail from Websolutionstuff',
        );
    } 
  
    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            markdown: 'emails.testMail',
        );
    }
  
    /**
     * Get the attachments for the message.
     *
     * @return array

     */
    public function attachments(): array
    {
        return [];
    }
}

 

 

Step 4: Create TestMailController

Execute the following command to generate a new controller:

php artisan make:controller TestMailController

app/Http/Controllers/TestMailController.php

<?php

namespace App\Http\Controllers;

use App\Mail\TestMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class TestMailController extends Controller
{
    public function sendMail()
    {
        $details = [
            'title' => 'This E-Mail from Websolutionstuff',
            'url' => 'https://www.websolutionstuff.com'
        ];

        // Customize the logic for sending the email
        $recipient = '[email protected]';

        // Send the email using the Mailable class
        Mail::to($recipient)->send(new TestMail($details));

        return "Test email sent to $recipient";
    }
}

 

Step 5: Define a Route

Open the routes/web.php file and define a route that maps to the sendMail method in your controller.

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

 

Step 6: Design the Markdown Template

The Markdown template associated with this Mailable class is located at resources/views/emails/testMail.blade.php. Open this file to design your email using Markdown syntax.

@component('mail::message')
    # Hello! {{ $details['title'] }}

    This is a test email with Markdown.

    @component('mail::button', ['url' => $details['url']])
    Visit Our Website
    @endcomponent

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

 

Step 7: Run the Laravel Server

Run the following command to start the Laravel development server

php artisan serve

 

Conclusion:

And there you have it! Sending beautifully crafted emails using Markdown templates in Laravel 10 has never been easier. Elevate your email communication with styled and professional-looking messages, enhancing the overall user experience.

Happy coding!

 


You might also like:

Recommended Post
Featured Post
How To Merge Two Collections In Laravel
How To Merge Two Collections I...

In this article, we will see how to merge two collections in laravel 8 or laravel 9. The Illuminate\Support\Co...

Read More

Jul-18-2022

Laravel 9 Datatables Filter with Dropdown
Laravel 9 Datatables Filter wi...

In this article, we will see laravel 9 datatables filter with dropdown. Here we will add datatables...

Read More

Mar-12-2022

Laravel 9 Form Collective Example
Laravel 9 Form Collective Exam...

In this article, we will see laravel 9 form collective example. Here, we will learn how to use collective form and...

Read More

Jan-20-2023

Laravel 9 Import Export CSV/EXCEL File Example
Laravel 9 Import Export CSV/EX...

In this tutorial, I will give you laravel 9 import export csv and excel file example. We will simply create import...

Read More

Feb-19-2022