In this tutorial you will learn about the How to Send Email in Laravel 8 with Markdown Template Example and its application with practical example.
In this tutorial we will learn how to send email in laravel 8 using mailable and mailtrap. We will use Laravel 8 builtin mailable markdown class to send emails. In order to send email using mailtrap we will implement laravel 8 builtin mailable markdown class. In this post I’ll show you how to integrate mailtrap to send email in laravel 8 application.
Laravel 8 provides builtin mailable markdown class to send emails. In Laravel 8 you can use mailtrap, smtp, Mailgun, Postmark, Amazon SES, and sendmail for sending emails. You are required to configure driver details on the .env file.
How to Send Email in Laravel 8 with Markdown Template Example
In this step by step tutorial you will understand how to send emails in Laravel 8 with the help of Mailtrap. We will explain to you how to use Laravel 8 builtin mailable markdown class to send emails using Mailtrap.
- Create Laravel Application
- Setting Up Mail Configuration
- Define Markdown with Mailable Class
- Create & Configure Controller
- Prepare Route
- Evoke Mail View
- Start Application
Create Laravel Application
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project laravel/laravel --prefer-dist laravel-markdown-send-email-example |
Setting Up Mail Configuration
In this step we will be setting smtp detail for mail configuration in Laravel, We will be using following Gmail SMTP details such as username, password inside the .envfile.
1 2 3 4 5 6 7 8 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=yoursite@gmail.com MAIL_PASSWORD=yourpassword MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=null MAIL_FROM_NAME="${APP_NAME}" |
If we are sending email from localhost using Gmail SMTP, then it requires you to turn on the “Allow less secure apps” on?. Go to the link provided:
1 |
https://www.google.com/settings/security/lesssecureapps |
Now, You need to turn on the option “Allow less secure apps”
Define Markdown with Mailable Class
The mailable class in Laravel used for sending emails. Now, lets create a new laravel mailable class using following artisan command:
1 |
php artisan make:mail SendDemoMail --markdown=emails.sendDemoMail |
Once the above command executed, it will create a new laravel mailable class SendDemoMail.php in app/Mail/ directory. Open the SendDemoMail.php file and put the following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class SendDemoMail extends Mailable { use Queueable, SerializesModels; public $maildata; /** * Create a new message instance. * * @return void */ public function __construct($maildata) { $this->maildata = $maildata; } /** * Build the message. * * @return $this */ public function build() { return $this->markdown('emails.sendDemoMail') ->with('maildata', $this->maildata); } } |
Create & Configure Controller
Now, we will create a controller that will handle the logic to send email. Let’s create ContactController Controller using following command:
1 |
php artisan make:controller ContactController |
Once the above command executed, it will create a controller file ContactController .php in app/Http/Controllers/ directory. Open the ContactController .php file and put the following code in it.
Controllers/ContactController .php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Mail\SendDemoMail; use Mail; class ContactController extends Controller { public function sendDemoMail() { $email = 'yoursite@gmail.com'; $maildata = [ 'title' => 'Laravel 8|7 Mail Sending Example with Markdown', 'url' => 'https://www.yoursite.com' ]; Mail::to($email)->send(new SendDemoMail($maildata)); dd("Mail has been sent successfully"); } } |
Create Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ContactController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('send-mail', [ContactController::class, 'sendDemoMail']); |
Create Email Template
In this step, we will create view/blade file to setup email template. Open resources/views/Email/sendDemoMail.blade.php file, put the following code.
resources/views/Email/sendDemoMail.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@component('mail::message') # {{ $maildata['title'] }} Your message body. @component('mail::button', ['url' => $maildata['url']]) Verify @endcomponent Thanks,<br> {{ config('app.name') }} @endcomponent |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/send-mail |