How to Send Email in Laravel 7.x with Mailtrap

Hey Artisan 

Hope you are doing well. Today i am going to discuss about laravel email. Now i will show you how to send email in laravel 7. Earlier i showed many tutorial about laravel email.

Markdown mail using mailable class, We sent mail using laravel event etc. Now i am going to show you how we can send email in laravel 7. I will show you step by step. Hope you guys will enjoy this laravel email tutorial.

Before sending laravel email, if you don't know how to send it, then first check this two tutorial before sending email. Cause you may face error.

Read also : Gmail configuration setup to send mail in Laravel

Read also : SMTP Server doesn't Work to Send Email in Laravel

 

In this tutorial, i am going to tell you how to send email using gmail smtp server configuration using laravel 7 mailable class. It is very easy to make and best way. you have to just follow few step and you will get simple mail send example in your laravel 7 application.

laravel-7-send-email

In this tutorial i am using mailtrap fake email testing system. Let's start our laravel 7 email sending tutorial.

Step 1: Open mailtrap.io

Open  mailtrap.io  in your browser and create an account. Here we need user id and user email to send email.

Step 2: Email Configuration.

Now having done this you will get below information. You have to put these credentials into your .env file.

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME= //username from your mailtrap credentials.
MAIL_PASSWORD= //password from your mailtrap credentials.
MAIL_ENCRYPTION=null

 

Step 3: Create a Mail class.

Now we have to create our email sending class to send email in laravel 7. Run below command to create it.

php artisan make:mail NewMail

 

after running this command you will find this in the following directory like app/Mail/NewMail.php. Now paste this below code in this NewMail file.

app/Mail/NewMail.php

namespace App\Mail;
   
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
  
class NewMail extends Mailable{

    use Queueable, SerializesModels;

    public $user;

    public function __construct($user){

        $this->user = $user;

    }

    public function build(){

        return $this->subject('This is Testing Mail')
                    ->view('email.test');
    }
}

 

Step 4: Create Blade View

After doing above things In this step, we have to find resources/views and create a new folder name email & create a new file test.blade.php and put the below code.

resources/views/email/test.blade.php

 

Step 5: Create  Route

We are in the last step in our tutorial. So create a route to send email in laravel 7 application. Paste this bbelow code to web.php.

routes/web.php

Route::get('test', function () {

    $user = [
        'name' => 'Mahedi Hasan',
        'info' => 'Laravel Developer'
    ];

    \Mail::to('[email protected]')->send(new \App\Mail\NewMail($user));

    dd("success");

});

 

Read also : Send Email with Laravel 6 using Markdown Mailable Class

Read also : Laravel 6 Sending Mail using Event Example from Scratch

 

Now time to check this laravel email sending tutorial, Open below url to your browser and check. 

http://127.0.0.1:8000/test

 

Hope it can help you. If you face any error then feel free to comment.