Laravel 5.4 – send email using Markdown Mailables
In this post we will give you information about Laravel 5.4 – send email using Markdown Mailables. Hear we will give you detail about Laravel 5.4 – send email using Markdown MailablesAnd how to use it also give you demo for it if it is necessary.
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 AppHttpControllers;
use IlluminateHttpRequest;
use AppMailOrderShipped;
use Mail;
class SendMailController extends Controller
{
/**
* Show the application sendMail.
*
* @return IlluminateHttpResponse
*/
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 AppMail;
use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
use IlluminateContractsQueueShouldQueue;
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…
Hope this code and post will helped you for implement Laravel 5.4 – send email using Markdown Mailables. 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