Laravel-9 Tutorial – Laravel 9 Markdown Example: Send Email using Markdown

Today, In this Laravel 9 Markdown tutorial. I would like to share with you the quintessential method of sending markdown email in Laravel application.

Sending a mail with markdown is no tough job rather its a no-brainer. Well, you heard it right. This comprehensive tutorial will explain everything step by step regarding laravel markdown mail templates, and yes we will clear the concepts of laravel markdown mail settings too.

There is nothing which Laravel Markdown doesn’t provide. Be it tables, components, embed an image, email link, button, and many more. You order anything, and it will bring on to your table.

This tutorial will teach you with profoundness about how to easily send uncomplicated email using GMAIL SMPT settings within the framework of Laravel. And yes we will lay the foundation with Laravel mailable class.

So get ready to enhance the impetus of sending simple mail using Laravel application.

Create Laravel Application

Installing laravel application is easy, run the following command you can skip this step if you have already installed the app.

composer create-project laravel/laravel --prefer-dist laravel-markdown-send-email-example

Get into the application root:

cd laravel-markdown-send-email-example

Setting Up Mail Configuration

Implied the mail configuration in Laravel, define the following Gmail SMTP details such as username, password inside the .env file.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=positronx@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

If you are using MAMP local server in macOs; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in .env file.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Please note: If you are getting any error related to gmail authentication, so you need to follow the below steps.

If we are sending email from localhost using Gmail SMTP, then it requires you to turn on the “Allow less secure apps” on?

Go to following link.

https://www.google.com/settings/security/lesssecureapps

Next, You need to turn the option “Allow less secure apps” ON.

Allow less secure apps

But remember this service won’t be visible if your 2-Step Verification is turned on, so turn it off first.

Define Markdown with Mailable Class

The mailable class inoculate profoundness in Laravel, it lets you use Mail features throughout the laravel project.

Run the following command to begin this step by creating the Mailable class

php artisan make:mail SendDemoMail --markdown=emails.sendDemoMail

Subsequently, you will see the above command has generated SendDemoMail class, so head over to app/Mail/SendDemoMail.php file and place the given below code.


namespace AppMail;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
class SendDemoMail extends Mailable
{
    use Queueable, SerializesModels;
    public $maildata;
    
    public function __construct($maildata)
    {
        $this->maildata = $maildata;
    }
    
    public function build()
    {
        return $this->markdown('emails.sendDemoMail')
                ->with('maildata', $this->maildata);
    }
}

Create & Configure Controller

Create the controller, where we conjugate all the logic that is required to send the mail using Markdown. So, first, create the controller using below command.

php artisan make:controller ContactController

The sendDemoMail() function is going to be solely responsible for sending the mail with markdown, declare it within the Contact controller.

Add the following code in app/Http/Controllers/ContactController.php file.


namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppMailSendDemoMail;
use Mail;
class ContactController extends Controller
{
    public function sendDemoMail()
    {
        $email = 'positronx@gmail.com';
   
        $maildata = [
            'title' => 'Laravel Mail Sending Example with Markdown',
            'url' => 'https://www.positronx.io'
        ];
  
        Mail::to($email)->send(new SendDemoMail($maildata));
   
        dd("Mail has been sent successfully");
    }
}

Prepare Route

Bind the controller with the route to make the request to send the mail with markdown, add the code in routes/web.php file.


use IlluminateSupportFacadesRoute;
use AppHttpControllersContactController;


Route::get('send-mail', [ContactController::class, 'sendDemoMail']);

Evoke Mail View

Almost reached the final step, just create an email template. Head over to the resources/views/emails folder and generate sendDemoMail.blade.php file and place all the code within to send the mail.

@component('mail::message')

Your message body.
@component('mail::button', ['url' => $maildata['url']])
Verify
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent

Start Application

Let us find out how to send an email with markdown in laravel, first start the application:

php artisan serve
http://localhost:8000/send-mail

Eventually, here is the output you can check on your mail box.

Send Email using Markdown in Laravel

Summary

We have completed this tutorial, i believe you would love this tutorial and it will help you in your subsequent laravel web development voyage.

Leave a Reply

Your email address will not be published. Required fields are marked *