Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Send Email with Laravel9 using Markdown Mailable Class

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

Sending an email is one most important features of any project. Well, you heard it right, In this tutorial, you will learn how to send emails using Markdown Template with the help of Laravel Mail Example

Before diving into building a demo application, let’s clarify what we are building here.

The user will enter the name and email address, and after clicking on submit button, an email will be sent to the entered email address. Login to that email address, and there you can see the email.

  1. Create Laravel Application
  2. Setting Up Mail Configuration
  3. Define Markdown with Mailable Class
  4. Create & Configure the Controller
  5. Create a simple form
  6. Create a body for the mail
  7. Prepare Route
  8. Start Application

Step-1 Create Laravel Application

Installing the 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 OrderEmail

Step-2 Setting Up Mail Configuration

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

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD= (third party app password click hear)
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

Step-3 Define Markdown with Mailable Class

The Mailable Class inoculates 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 OrderMail --markdown=emails.OrderMail

Subsequently, you will see the above command has generated OrderMail, and also two files are generated:

  • app/Mail/OrderMail.php
  • resources/views/emails/OrderMail.blade.php

so head over to the app/Mail/OrderMail.php file and place the given below code.

app/Mail/OrderMail.php
body = $body;
    }
 
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.OrderMail')->with('body',$this->body);
    }
}

Step-4 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 the below command.

php artisan make:controller MailController

The OrderMail() function is going to be solely responsible for sending the mail with markdown and declaring it within the MailController.
Add the following code in the app/http/Controllers/MailController.php file.

app/http/Controllers/MailController.php 
;validate([
            'name'=>'required',
            'email'=>'required|email'
        ]);
        $email = $data['email'];
 
        $body = [
            'name'=>$data['name'],
            'url_a'=>'https://www.magespidersolutions.com/',
           'url_b'=>'https://www.magespidersolutions.com/tutorials/laravel',
        ];
 
        Mail::to($email)->send(new OrderMail($body));
        return back()->with('status','Mail sent successfully');;
    }
}

Step-5 Create a simple form

Moving forward toward our Laravel Mail Example, now we will create one form in this step that takes the name and email of the user.

Open resources/views/welcome.blade.php and add the below-mentioned code in the body section of the welcome page.

resources/views/welcome.blade.php

@if (session('status'))
{{ session('status') }}
; @endif

MagespiderSolutions Tutorial

;
@csrf
Enter Name&lt

Enter Email



Step 6: Create a Body for the Mail.

In step 3, we created a blade file named OrderMail.blade.php for a laravel email template. This file will be useful to write the design code. We will add the below code to that file.

resources/views/emails/OrderMail.blade.php
@component('mail::message')

Hello {{$body['name']}},

The email is a sample email for Laravel Tutorial: How to Send an Email using Laravel 9from @component('mail::button', ['url' => $body['url_a']]) Bacancy Technology @endcomponent

Visit @component('mail::button', ['url' => $body['url_b']]) Laravel Tutorials @endcomponent and learn more about the Laravel framework.

Happy coding!
Thanks,
{{ config('app.name') }}
; Laravel Team. @endcomponent

Step-7 Prepare Route

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

Web.php
Route::get('/', function () {
   return view('welcome');
});
 
Route::post('/sendOrderMail',[MailController::class,'sendMail'])->name('send.email');

Step-7 Create Laravel 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
Summary

We have completed this tutorial, I believe you would love this tutorial.

Happy Coding.!

The post Send Email with Laravel9 using Markdown Mailable Class appeared first on Magespider Solutions.



This post first appeared on Software & Mobile App Development, please read the originial post: here

Share the post

Send Email with Laravel9 using Markdown Mailable Class

×

Subscribe to Software & Mobile App Development

Get updates delivered right to your inbox!

Thank you for your subscription

×