How to send email in laravel 8 (easy way)
In this article I will show you how we can send email in laravel 8, this tutorial is basically for the beginner, who is learning laravel and want’s to send email via their laravel application.
We will send email in Laravel 8 for various purpose like, for the account verification, for order confirmation etc.
I will show you step by step guide to send email in laravel for the absolute beginner.
To send the email in laravel 8, we will follow the below steps -:
- Install Laravel 8
- Create account on mailTrap and setup in project
- Create a blade file for email
- Code to send email in Laravel
Install Laravel 8
To install laravel 8, please enter the below command in your vs code powershell or in your command panel, ignore if you already installed it.
composer create-project laravel/laravel send-email
the above command will create the project with named as send-email in your htdocs folder.
Create account on mailTrap and setup in project
Now we will create an account on mailtrap, to create an account on the mail trap click here. when you click on the link you will have something like the below image.
Once you create the account and log in to your account, you will have something like the below image.
after that click on the demo inbox then you have something like the below image.
Choose laravel 7+ from the integration dropdown, copy all mail credential.After that open your project look for .env file and paste all the code in .env file.
Create a blade file for email
Now we will create a blade file under the resources folder , create a folder named as email under that folder create a file named as test-email.blade.php
<p>received from {{$user}} email</p>
Code to send email in Laravel
Add the below code in your controller, and call the function testEmail function from your route
public function testEmail() { $user = 'codehunger'; \Mail::send('email.test-email', ['user' => $user], function ($m) use ($user) { $m->from('test@test.com', 'test title'); $m->to('receive@test.com')->subject('test subject'); }); }
After executing the above code you can go to your mailtrap and check the email.