Laravel 9 Send Email Example

Laravel 9 send email example; Through this tutorial, i am going to show you how to send email in laravel 9 apps using gmail smtp.

Laravel 9 Send Email Example

Use the below given simple steps to send email using gmail smtp in laravel 9 apps:

  • Step 1 – Install Laravel 9 App
  • Step 2 – Configuration SMTP in .env
  • Step 3 – Create Mailable Class
  • Step 4 – Create Send Mail Function in Route File
  • Step 5 – Create Email Template
  • Step 6 – Run Development Server

Step 1 – Install Laravel 9 App

In step 1, install or download laravel 9 application, so open terminal or command prompt and run the following command to install fresh laravel 9 app:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Configuration SMTP in .env

In step 2, configure gmail, aws, etc driver smtp details in .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=Add your user name here
MAIL_PASSWORD=Add your password here
MAIL_ENCRYPTION=tls

Note that:- Make sure you have to enable google security setting form your gmail. So, go to Google account and click on ‘Account’. Once you are on the ‘Account’ page, click on ‘Security’. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set as ON.

Step 3 – Create Mailable Class

In step 3, create new mailable class for send email in laravel. So, run the following command on terminal:

php artisan make:mail NotifyMail

Then navigate to app/Mail directory and open NotifyMail.php file. And the following code into it:

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NotifyMail extends Mailable
{
    use Queueable, SerializesModels;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.demoMail');
    }
}

Note that, you will create an email template with any name. That you want to send. But most important thing do not forget to add createed email template on NotifyMail class. Like following:

    return $this->view('view.name');
    to
    return $this->view('emails.demoMail'); 

Step 4 – Create Send Mail Function in Route File

In this step, navigate routes directory and gopen /web.php, Then implement send email route function.

I have already implement this function. So, you can add the following routes for send email in web.php file:

use Mail;

Route::get('send-email', function () {
	  Mail::to('receiver-email-id')->send(new NotifyMail());

	  if (Mail::failures()) {
	      echo "Email not send";
	  }else{
	      echo "Email has been sent";
	 }

});

Step 5 – Create Email Template

In this step, create directory name emails inside resources/views directory.

After that, navigate inside this direcotry and create an demoMail.blade.php blade view file inside it. Then add the following email template code into it:

<!DOCTYPE html>
<html>
<head>
 <title>Laravel 9 Send Email Example</title>
</head>
<body>
 <h1>This is first email in laravel 9</h1>
 <p>Hello,  Laravel Developer</p>
</body>
</html> 

Step 6 – Start Development Server

In this step, open terminal and type PHP artisan serve command and hit enter to start developement server

php artisan serve

Now, fire the following url on browser:

http://127.0.0.1:8000/send-email

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.