Send Email Using Markdown Email Template in Laravel 8

Share this Article
Reading Time: 6 minutes
2,074 Views

Sending an email in web application is very common. Laravel provides the predefined Mail class used to send mail. We only need to use & configure it. The content of this article is very useful to understand the things that we need to send a normal email using SMTP.

Inside this article we will Send Mail Using Markdown Email Template in Laravel 8 by Gmail SMTP Server. We will use Gmail and it’s SMTP with Laravel 8.

Send Email in Laravel Using GMail SMTP with Custom Email template, Click here.

For email template we will use the laravel 8 provided markdown template layout for emails.

To get the complete concept of this article you need SMTP details.

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


SMTP Configuration with Laravel 8

To configure SMTP details, open up the file .env from application root. We will pass mail driver as gmail server, mail host, mail port, mail username, mail password.

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME="mygmail@gmail.com"
MAIL_PASSWORD="mypassword"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="mygmail@gmail.com"
MAIL_FROM_NAME="Online Web Tutor"
  

Make sure you have enabled google security setting form your gmail. go to Google account and click on “Account”. Once you are on the “Account” page, click on “Security“. Scroll down to the bottom and you will find “Less secure app access” settings. Set as ON.

Have a look into these images.


Configure Application Route

To configure application routes, open up the file /routes/web.php

use Illuminate\Support\Facades\Mail;

Route::get('send-mail', function () {
   
  $details = [
      'title' => 'Sample Title From Mail',
      'body' => 'This is sample content we have added for this test mail'
  ];
 
  Mail::to('send_to_email@gmail.com')->send(new \App\Mail\MyTestMail($details));
 
  dd("Email is Sent, please check your inbox.");
});

Create Mail Class with Markdown Email Template

Inside this we will create a mail class with markdown email template layout via artisan command which will be responsible for sending emails

Help Manual For Mail Class

Open project in terminal and type this artisan command.

$ php artisan help make:mail

While creating a mail class, we need to pass –markdown flag to generate mailable template file.

Example: Create Mail Class

$ php artisan make:mail MyTestMail --markdown=emails.sample-mail
  • –markdown, it is flag to create mailable template file as well
  • emails.sample-mail, it means we will create sample-mail.blade.php, a email template file using markdown inside /resources/views/emails folder. emails is the name of the folder.

When you run this command via terminal, application will creates a folder with the name of Mail inside app folder along with another folder called emails inside /resources/views folder. You should see a file /app/Mail/MyTestMail.php and sample-mail.blade.php inside emails folder.

Open file MyTestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class MyTestMail extends Mailable
{
    use Queueable, SerializesModels;

  	public $details; 
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.sample-mail');
    }
}

Mail Blade Template

Open sample-mail.blade.php

@component('mail::message')
# Introduction

The body of your message.
<h3>{{ $details['title'] }}</h3>
<h3>{{ $details['body'] }}</h3>

@component('mail::button', ['url' => ''])
Button Text
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent
  

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

Open up the URL – http://127.0.0.1/send-mail

We hope this article helped you to learn about Laravel 8 Send Mail using Gmail SMTP Server in a very detailed way.

To learn about Customization of Markdown Email template file, Click here.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here

Leave a Comment