Web Designing World | Logo

Hey Artisans

I hope you are doing well. Today in this blog I am going to write about Send Email in Laravel 7 with Mailtrap. Now I will show you how to send emails in laravel 7 by using mailtrap.

In this blog, I am going to tell you how to Send Email in Laravel 7 with Mailtrap mailable class. It is very easy to make and the best way. you have to just follow a few steps and you will get simple mail to send examples in your laravel 7 application.

Send Email in Laravel 7 with Mailtrap

In this tutorial, I am using a mail trap 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 a user id and the user email to send an email.

Step 2: Email Configuration.

Now having done this you will get the 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;

   // Web Designing World //
   // kishan kumar // Appfinz Technologies //

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

<!DOCTYPE html>
<html>
<head>
    <title>This Email generated by www.codechief.org</title>
</head>
<body>
    <h1>{{ $user['name'] }}</h1>
    <p>{{ $user['info'] }}</p>
    <p>Thank you</p>
</body>
</html> 

Step 5: Create  Route

We are in the last step in our tutorial. To create a route to send emails in laravel 7 applications. Paste this below code to web.php.

routes/web.php

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

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

    \Mail::to('mail@codechief.org')->send(new \App\Mail\NewMail($user));

    dd("success");

});

Now its is the time to check this Send Emails in Laravel 7 with Mailtrap, Open below URL to your browser and check. 

Leave a Reply

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