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.
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
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}"
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 [];
}
}
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";
}
}
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']);
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
Run the following command to start the Laravel development server
php artisan serve
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:
Creating an auto-generating slug using Laravel Livewire is a practical and efficient way to handle slugs for your applic...
Oct-27-2023
In this article, we will see how to create a validation rule in laravel 10. Here, we will learn about the laravel 1...
May-22-2023
In this article, we will see how to validate password and confirm password in react js. Sometimes we need to add th...
Sep-14-2022
In this tutorial we will learn php array functions with example. An array is a data structure that contains a group of e...
Apr-23-2021