onlinecode

Send Email with Laravel 7/6 using Markdown Mailable Class

Send Email with Laravel 7/6 using Markdown Mailable Class

In this post we will give you information about Send Email with Laravel 7/6 using Markdown Mailable Class. Hear we will give you detail about Send Email with Laravel 7/6 using Markdown Mailable ClassAnd how to use it also give you demo for it if it is necessary.

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/6 app. we will send mail using mailable class in laravel 7/6. basically we will use Markdown email template in laravel 7/6.

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

In this tutorial, i am going to tell you how to send simple email with gmail smtp configuration using laravel 6 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 6 application.

Follow bellow step in your laravel 6 project.

Step 1: Set Mail Configuration

In first step you have to add your gmail smtp configuration like your username, password etc, so open your .env file and add your configration.

.env

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=onlinecode@gmail.com

MAIL_PASSWORD=mypassword

MAIL_ENCRYPTION=tls

If you don’t know how to do configuration or found any error about gmail authentication then you can follow this link : How to set gmail configration for mail in Laravel?.

Step 2: Create Mailable Class with Markdown

Laravel 6 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 MyDemoMail --markdown=emails.myDemoMail

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

app/Mail/MyDemoMail.php

<?php

namespace AppMail;

use IlluminateBusQueueable;

use IlluminateContractsQueueShouldQueue;

use IlluminateMailMailable;

use IlluminateQueueSerializesModels;

class MyDemoMail 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.myDemoMail')

->with('details', $this->details);

}

}

Also see:How to set gmail configration for mail in Laravel?

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-demo-mail','HomeController@myDemoMail');

Step 4: Create Controller Method

Now, we will add myDemoMail() 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 AppHttpControllers;

use IlluminateHttpRequest;

use AppMailMyDemoMail;

use Mail;

class HomeController extends Controller

{

/**

* Show the application dashboard.

*

* @return IlluminateContractsSupportRenderable

*/

public function myDemoMail()

{

$myEmail = 'aatmaninfotech@gmail.com';

$details = [

'title' => 'Mail Demo from ItSolutionStuff.com',

'url' => 'https://www.onlinecode'

];

Mail::to($myEmail)->send(new MyDemoMail($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 myDemoMail.blade.php file and put bellow code.

resources/views/emails/myDemoMail.blade.php

@component('mail::message')

# {{ $details['title'] }}

The body of your message.

@component('mail::button', ['url' => $details['url']])

Button Text

@endcomponent

Thanks,<br>

{{ config('app.name') }}

@endcomponent

Ok, now you ready to run our test example, so check it…

You can run your project by using following command:

php artisan serve

Now open this url:

Also see:How to Send Mail in Laravel 6?

http://localhost:8000/my-demo-mail

You will get email like as bellow:

I hope it can help you…

Hope this code and post will helped you for implement Send Email with Laravel 7/6 using Markdown Mailable Class. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version