Sending email is, in my opinion, a key feature of any project. So I want to tell you how Laravel 6/7 send mail using markdown mailable class . We send emails with the class that can be sent in Laravel 7/6. Basically, we use the markdown email template in Laravel 7/6.
Laravel Markdown offers components, spreadsheets, email links, buttons, embedded images, etc. Nice markdown design that you can use with the email template.
In this tutorial, I will explain how to Laravel 6/7 send mail using markdown mailable class . This is very easy and the best way. You only have to follow a few steps and get a simple example of sending emails in your Laravel 6 application.
Follow the next step in your Laravel 6 project.
Step 1: Set Mail Configuration
The first step is to add your Gmail SMTP settings like your username, password, etc. So open your ENV file and add your settings.
.env
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=itsolutionstuff@gmail.com MAIL_PASSWORD=mypassword MAIL_ENCRYPTION=tls
Step 2: Create Mailable Class with Markdown
Laravel 6 introduces a new class that can be broadcast so that we can easily use it as a Laravel event. You can reuse them anywhere in your Laravel application. So first create the Mailable class with the craft command and then trigger the following command:
php artisan make:mail MyDemoMail --markdown=emails.myDemoMail
Ok, now you can see a new file in your application folder (app / Mail / MyDemoMail.php). So open this file and enter the following code.
app/Mail/MyDemoMail.php
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; 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); } }
Step 3: Create Route
routes/web.php
Route::get('my-demo-mail','HomeController@myDemoMail');
Step 4: Create Controller Method
Now we insert myDemoMail () in the controller file "HomeController". We write the send code of the mail in this file. So if you haven't created a HomeController, create the HomeController.php file and enter the following code.
In the variable $ myEmail you can configure your own email to test the email.
app/Http/Controllers/HomeController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Mail\MyDemoMail; use Mail; class HomeController extends Controller { /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function myDemoMail() { $myEmail = 'aatmaninfotech@gmail.com'; $details = [ 'title' => 'Mail Demo from ItSolutionStuff.com', 'url' => 'https://www.itsolutionstuff.com' ]; Mail::to($myEmail)->send(new MyDemoMail($details)); dd("Mail Send Successfully"); } }
Step 5: Add View File
In the last step we create an email template file. So first create the "E-Mails" folder in your resource folder, create the myDemoMail.blade.php file and enter the following 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 can run our test example. Try it ...
You can run your project with the following command:
php artisan serve
Now open this URL:
http://localhost:8000/my-demo-mail
You will receive an email.
I hope this tutorial Laravel 6/7 send mail using markdown mailable class could help you .
Thanks…
0 comments:
Post a Comment
Please don't enter any spam link in the comment box.