Laravel 7 Send Mail Using Markdown Example

  876 views   6 months ago Laravel

Today, markdown laravel 7 mail is our main topic. i explained simply step by step markdown laravel 7 email. you can see laravel 7 markdown components. let’s discuss about laravel 7 send email mailable. Alright, let’s dive into the steps.

I will exmplain how to send mail using markdown example in laravel 7.we will show example of send mail using markdown in laravel 7.Sending email is a primary feature of each project i think. So i would like to share with you how to send mail using markdown mailable class in laravel 7 app. we will send mail using mailable class in laravel 7. basically we will use Markdown email template in laravel 7

Laravel Markdown provides components, tables, email link, button, embed image etc. Markdown beautiful layout you can use with email template.

I am going to tell you how to send simple email with gmail smtp configuration using laravel 7 mailable class. It is very simple and best way. you have to just follow few step and you will get simple mail send example in your laravel 7 application.

Follow bellow step of send mail using markdown example in laravel.

Step: 1 Install Laravel 7

In this step, if you haven't laravel 7 application setup then we have to get fresh laravel 7 application. So run bellow command and get clean fresh laravel 7 application

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

After you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 7 will use those sender details on email. So you can simply add as like following.

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourgoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls

Step 2: Create Mailable Class with Markdown

Laravel 7 introduce new mailable class that way we can use simply like laravel event, you can re-use anywhere in your laravel application. So first create Mailable class using artisan command, so fire bellow command:

php artisan make:mail MyTestMail --markdown=emails.myTestMail

Now you can see new file in your app(app/Mail/MyTestMail.php) folder. So, open that file and put bellow code.

app/Mail/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.myTestMail')
                    ->with('details', $this->details);
    }
}

Step 3: Create Route

In this step, we will add new route for out testing mail so open your web route file and add bellow route.

routes/web.php

Route::get('my-Test-mail','HomeController@myTestMail');

Step 4: Create Controller Method

Now, we will add myTestMail() in "HomeController" Controller file, in this file we will write code of mail send, so if you haven't created HomeController then create HomeController.php file and put bellow code.

In $myEmail variable, you can set your own email for testing mail.

app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\MyTestMail;
use Mail;

class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function myTestMail()
    {
        $myEmail = 'your_receiver_email@gmail.com';
   
        $details = [
            'title' => 'Mail Test from LaravelCode.com',
            'url' => 'https://www.laravelcode.com'
        ];
  
        Mail::to($myEmail)->send(new MyTestMail($details));
   
        dd("Mail Send Successfully");
    }
}

Step 5: Add View File

In last step, we will create email template file, so first create "emails" folder in your resources folder and create myTestMail.blade.php file and put bellow code.

resources/views/emails/myTestMail.blade.php

@component('mail::message')
# {{ $details['title'] }}
  
The body of your message. 
   
@component('mail::button', ['url' => $details['url']])
Button Text
@endcomponent
   
Thanks,

{{ config('app.name') }}
@endcomponent
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles