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 Multiple Database Connections
Laravel 9 Multiple Database Co...

In this tutorial, we will see laravel 9 multiple database connections. we will implement how to use laravel 9 multi...

Read More

Mar-04-2022

Laravel 8 Remove/Hide Columns While Export Data In Datatables
Laravel 8 Remove/Hide Columns...

In this article, we will see how to remove/hide columns while export data in datatables in laravel 8. When we are u...

Read More

Oct-13-2020

Change Date Format Using Carbon In Laravel
Change Date Format Using Carbo...

In this article, we will see a change date format using carbon in laravel. Many times we have requirements to chang...

Read More

Dec-15-2020

Carbon Add Hours In Laravel
Carbon Add Hours In Laravel

In this article, we will see examples of carbon add hours in laravel 8. Carbon provides many functions like ad...

Read More

Dec-09-2020