How to send email in laravel 8 (easy way)

send email in laravel 8

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 -:

  1. Install Laravel 8
  2. Create account on mailTrap and setup in project
  3. Create a blade file for email
  4. 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.

mail trap signup for email testing

Once you create the account and log in to your account, you will have something like the below image.

mailtrap after login laravel 8 email

after that click on the demo inbox then you have something like the below image.

mailtrap credential mail testing

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.

0 0 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x