Laravel 9 Send Email using Markdown Mailables Example
Teams applying “Laravel 9 Send Email using Markdown Mailables Example” in a production project can use negative capability to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi all,
In this tute, we will discuss laravel 9 send email using markdown mailables example. This post will give you a simple example of sending mail in laravel 9 using markdown mailables. let’s discuss about laravel 9 send mail with url link example. In this tutorial, laravel 9 sends mail using markdown structure view. This post will give you a simple example of step-by-step send mail using markdown component in laravel 9.
Laravel Markdown provides components, tables, email link, button, embed image etc. Markdown beautiful layout you can use with email template.
Step 1: Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-app
Step 2: Make Configuration
In the first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 9 will use those sender configurations for sending the email. So you can simply add as like following.
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=xyz123456
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
Step 3: Add Mail Class with Markdown
In this step, we will create a mail class SendMail for email sending. Here we will write code for which view will call and object of user. So let's run bellow the command.
php artisan make:mail SendMail --markdown=emails.demoMail
now, let's update code on SendMail.php file as bellow:
app/Mail/SendMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendMail extends Mailable
{
use Queueable, SerializesModels;
public $mailData;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($mailData)
{
$this->mailData = $mailData;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Mail from Nicesnippets.com')
->markdown('emails.demoMail');
}
}
Step 4: Add Controller
In this step, we will create SendMailController with index() method where we write code for sending mail to given email address. so first let's create controller by following command and update code on it.
php artisan make:controller SendMailController
Now, update code on SendMailController file.
app/Http/Controllers/SendMailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\SendMail;
use Mail;
class SendMailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'Send mail from Nicesnippets.com',
'url' => ''
];
Mail::to('[email protected]')->send(new SendMail($mailData));
dd("Email is sent successfully.");
}
}
Step 5: Add Routes
In this step, we need to create routes for list of sending email. so open your "routes/web.php" file and add following route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SendMailController;
/*
|--------------------------------------------------------------------------
| 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', [SendMailController::class, 'index']);
Step 6: Add Blade View
In this step, we will create blade view file and write email that we want to send. now we just write some dummy text. create bellow files on "emails" folder.
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:
All steps have been done, now you have to type the given command and hit enter to run the laravel app:
php artisan serve
Now, you have to open web browser, type the given URL and view the app output:
http://localhost:8000/send-mail
Output:
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial