Laravel

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

  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.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button