Laravel 5.4 - Send Email using Markdown Mailables Example
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', 'SendMailController@sendMail');
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');
}
}
Step 3: Generate Markdown Mailables
In this step we require to generate Markdown template by following bellow command. This command will generate Mail Class and view files.
php artisan make:mail OrderShipped --markdown=emails.orders.shipped
Ok, Now We have OrderShipped.php file will available on Mail folder, So one that file and copy bellow content and paste there.
app/Mail/OrderShipped.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class OrderShipped extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($content)
{
$this->content = $content;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.orders.shipped')
->with('content',$this->content);
}
}
Next we have also generated view files on following path and also replace content.
resources/views/emails/orders/shipped.blade.php
@component('mail::message')
# {{ $content['title'] }}
{{ $content['body'] }}
@component('mail::table')
| Laravel | Table | Example |
| ------------- |:-------------:| --------:|
| PHP | Centered | $100 |
| Laravel | Right-Aligned | $200 |
@endcomponent
@component('mail::button', ['url' => ''])
{{ $content['button'] }}
@endcomponent
Thanks,
{{ config('app.name') }}
@endcomponent
Now we have ready for mail template, we can also customize our template by following command:
php artisan vendor:publish --tag=laravel-mail
Now, you can see there are several files for email template on resources/views/vendor/mail. You can change your component layout.
Step 4: Configuration for email
In last step we require to configuration of email sender details, So open your .env file and change value in following variable:
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=tls
If you want to use sendgrid, gmail, mailgun, mandrill then bellow link can help you:
How to send mail using Sendgrid in laravel 5?
Laravel 5 mailchimp api integration from scratch with example
Mailgun setup with Laravel 5 example
How to set gmail configration for mail in Laravel?
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.