How to send mail using mailtrap in laravel 7

  396 views   4 months ago Laravel

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 utilizing mailable class, We sent mail utilizing 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 relish this laravel email tutorial.

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

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.

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

<!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. 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' => 'Harsukh Makwana',
        'info' => 'Laravel & Python Devloper'
    ];

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

    dd("success");

});

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

http://127.0.0.1:8000/test

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles