How to Send Email in Laravel 9 Example Tutorial

Hello Artisan,

In this laravel 9 send email demo example, I will show you how to send laravel 9 email example. So if you want to see an example of laravel 9 send mail example step by step then this Laravel 9 send email tutorial is for you. This post will give you a complete step-by-step guide and example of sending an email in laravel 9 SMTP.

We already know as a Laravel developer that Laravel 9 provides their inbuilt mail configuration for sending emails. you can use several drivers for sending emails in laravel 9. you can use SMTP, Mailgun, Postmark, Amazon SES, and send an email. So we have to configure on the env file what driver you want to use for sending email in Laravel.

In this tutorial, I will give you a step-by-step guide to send emails in laravel 9. We can create blade file design and also with dynamic information for mail layout. so lets an example of how to send email in the Laravel 9 application.

 

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

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 9. Run the 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');
    }
}

 

Read also: Laravel 9 Markdown Send Email using Markdown Mailables

 

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 and 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 emails in laravel 9 application. 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 time to check this laravel email sending tutorial, Open the below URL to your browser and check. 

http://127.0.0.1:8000/test

 

Read also: Laravel 9 Login with Username Instead of Email Example

 

Hope it can help you. Hope this Laravel 9 send email from the localhost tutorial will help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js