Laravel 5.4 - send email using Markdown Mailables

Admin   PHP   56  2020-08-10 11:45:44

Some days ago release Laravel 5.4 framework new version with new features and many upgrade. Laravel also provide documentation for Laravel 5.4 on their website. There are several update in Laravel 5.4 like in collections, Markdown Mailables for send mail, factory helper, Bootstrappers etc.

In this tutorial we will learn how to send email using new feature Markdown Mailables in Laravel 5.4 version. Laravel 5.4 introduce new feature Markdown Mailables for Mail facade. Markdown Mailables provide us pre-built templates and components of mail notifications. they render beautiful, responsive HTML templates for the messages while also automatically generating a plain-text counterpart. So it's pretty fantastic new feature for us to responsive email template with proper build.

So today i am going to give you one example for send email using Markdown Mailables from scratch. So let's follow bellow step:

Step 1: Create Route

In this is step we need to create route one simple route for send mail. so open your routes/web.php file and add following route.

routes/web.php

Route::get('sendmail', '[email protected]');

Step 2: Create Controller

In this point, now we should create new controller as SendMailController in this path app/Http/Controllers/SendMailController.php. This controller will send mail using Mail facade, So run bellow command for generate new controller:

php artisan make:controller SendMailController

Ok, now put bellow content in controller file:

app/Http/Controllers/SendMailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Mail\OrderShipped;

use Mail;

class SendMailController extends Controller

{

/**

* Show the application sendMail.

*

* @return \Illuminate\Http\Response

*/

public function sendMail()

{

$content = [

'title'=> 'Itsolutionstuff.com mail',

'body'=> 'The body of your message.',

'button' => 'Click Here'

];

$receiverAddress = 'your email';

Mail::to($receiverAddress)->send(new OrderShipped($content));

dd('mail send successfully');

}

}

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/sendmail

I hope it can help you...