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
Laravel 9 Create Zip File And Download
Laravel 9 Create Zip File And...

In this article, we will see laravel 9 create a zip file and download it. Laravel provides ZipArchive class fo...

Read More

May-02-2022

How To File Upload In Laravel 10 Example
How To File Upload In Laravel...

In this article, we will see how to file upload in laravel 10 examples. Here, we will learn about the laravel...

Read More

Mar-15-2023

Laravel 8 CRUD Operation Example
Laravel 8 CRUD Operation Examp...

In this article, we will see the laravel 8 crud operation. As you know Laravel 8 has already been officially released an...

Read More

Sep-16-2020

How To Get Current Week Records In MySQL
How To Get Current Week Record...

In this tutorial, we will see how to get current week records in MySQL. Many times we need to get current week reco...

Read More

Feb-07-2022