Laravel 8 Send Email Example

Laravel 8 Sending email example. In this post, i will show you how to send email in laravel 8. In this example post, i will use this mailable class to send an email in laravel 8 using gmail smtp.

First of all, you need to know about laravel mailable class. Laravel 8 provide mail class for send email.

As well as you can use many SMTP drivers details like gmail, Mailgun, Postmark, Amazon SES, and sendmail in .env file for sending email in laravel 8.

Because sometime, need to send emails in laravel app. So, you will learn how to send emails from the Laravel 8 application using gmail and other smtp Mailtrap etc , server.

How to Send Email In Laravel 8 using Gmail SMTP

  • Step 1 – Install Laravel 8 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 8 App

In step 1, install or download laravel 8 application, so open terminal or command prompt and run the following command to install fresh laravel 8 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

Step 3 – Create Mailable Class

In step 3, create new mailable class for send email in laravel 8. 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 8 Send Email Example</title>
</head>
<body>
 <h1>This is first email in laravel 8</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

Leave a Comment