In this tutorial you will learn about the Laravel 9 Send Email Example Tutorial and its application with practical example.
In this Laravel 9 Send Email Example Tutorial I will show you how to send email in laravel 9 using mailable and mailtrap. We will use Laravel 9 builtin mailable markdown class to send emails. In order to send email using mailtrap we will implement laravel 9 builtin mailable markdown class. In this post I’ll show you how to integrate mailtrap to send email in laravel 9 application.
Laravel 9 Send Email Example Tutorial
In this step by step tutorial you will understand how to send emails in Laravel 9 with the help of using mailable and mailtrap. I will also demonstrate you how to use Laravel 9 builtin mailable markdown class to send emails using Mailtrap. Please follow the instruction given below:
- Install Laravel 9 App
- Setup Database Credentials
- Configuration SMTP in .env
- Create Mailable Class
- Create Email Controller
- Add Email Send Route
- Create Directory And Blade View
- Run Development Server
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel lara8blog |
Make sure you have composer installed.
Setup Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=lara9blog DB_USERNAME=root DB_PASSWORD= |
Setting Up Mailtrap 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 .env file.
1 2 3 4 5 6 7 8 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=your_email@gmail.com MAIL_PASSWORD= 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”
Create Laravel Markdown 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 DemoEmail --markdown=Email.demoEmail |
Once the above command executed, it will create a new laravel mailable class DemoEmail.php in app/Mail/ directory. Open the DemoEmail.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 |
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class DemoEmail 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('Email.demoEmail') ->with('mailData', $this->mailData); } } |
Create Controller to Send and Email
Now, we will create a controller that will handle the logic to send email. Let’s create DemoEmailController Controller using following command:
1 |
php artisan make:controller DemoEmailController |
Once the above command executed, it will create a controller file DemoEmailController.php in app/Http/Controllers/ directory. Open the DemoEmailController.php file and put the following code in it.
Controllers/DemoEmailController.php
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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Mail; use App\Mail\DemoEmail; use Symfony\Component\HttpFoundation\Response; class DemoEmailController extends Controller { public function sendEmail() { $email = 'youremail@gmail.com'; $mailData = [ 'title' => 'Demo Email', 'url' => 'https://www.yoursite.com' ]; Mail::to($email)->send(new DemoEmail($mailData)); return response()->json([ 'message' => 'Email has been sent.' ], Response::HTTP_OK); } } |
In the DemoEmailController.php we have included DemoEmail Mailable class along with the Mail Facade and Http Response service. Later we have created controller method to send demo email from our laravel 8 application.
Create Route to Send Mail
After this, we need to add following route in “routes/web.php” to send email. Lets open “routes/web.php” file and add following route.
routes/web.php
1 2 |
use App\Http\Controllers\DemoEmailController; Route::get('/send-demo-email', [DemoEmailController::class, 'sendEmail']); |
Create Email Template
In this step, we will create view/blade file to setup email template. Open resources/views/Email/demoEmail.blade.php file, put the following code.
resources/views/Email/demoEmail.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 |
@component('mail::message') {{ $mailData['title'] }} Congratulations! You received demo email. @component('mail::button', ['url' => $mailData['url']]) Cheers! @endcomponent Thanks,<br> {{ config('app.name') }} @endcomponent |
Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command:
1 |
php artisan serve |
Send Email in Laravel
Now, open the following URL in browser to send email:
1 |
http://127.0.0.1:8000/send-demo-email |
Output:-
When you visit the link, you will receive the example email in your inbox.