How to Send Email in Laravel 9 ?

Today now in this post i will show you How to Send Email in Laravel 9 ? Here in this quick example, we let’s see a tutorial about laravel 9 send email . if you want to know about laravel 9 send mail example then i hope you are in the right place. Now in this post i will give you a very simple example of sending an email in laravel 9 smtp. If you have any question about laravel 9 send mail smtp example then here I will give a very simple example also with a solution.

We know Laravel 9 provides an inbuilt mail configuration for sending the emails. We can use the several drivers for sending the email in laravel 9. We can use smtp, Mailgun, Postmark, Amazon SES, and also send email. We need to configure on the env file what the driver we want to use.

Now in this tutorial, I will give you an example with step by step instructions to send emails in laravel 9 application. We can create the blade file for design and also with the dynamic information for the mail layout. So let’s start and see step by step guide and then send an email to our requirement.

Step 1: Install Laravel 9

This is the first step. Now this step is not needed if you have already install laravel 9 application. However, if you don’t have then need to created the laravel application, then we may to go ahead and then need to execute the below command:

composer create-project laravel/laravel example-app
Step 2: Make Configuration

Now in this step, you need to add the send mail configuration also with mail driver, mail host, mail port, mail username, mail password. So in laravel 9 we will will use the those sender configuration for sending the email. So we can simply add as like as following.

.env

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Step 3: Create Mail Class

Now in this step i will create the mail class DemoMail for the email sending. Here i will write the code for which view will call and the object of user. So let’s run the bellow command.

php artisan make:mail DemoMail

now, let’s need to update code on DemoMail.php file as like as bellow:

app/Mail/DemoMail.php

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

Now in this step, i will create the MailController also with index() method where i will write code for the sending mail to given email address. So at first let’s create the controller by the following command and update the code on it.

php artisan make:controller MailController

Now, need to update code on MailController file.

app/Http/Controllers/MailController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Mail;
use App\Mail\DemoMail;
  
class MailController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $mailData = [
            'title' => 'Mail from codingspoint.com',
            'body' => 'This is for testing email using smtp.'
        ];
         
        Mail::to('your_email@gmail.com')->send(new DemoMail($mailData));
           
        dd("Email is sent successfully.");
    }
}
Step 5: Create Routes

Now in this step, we need to create the routes for list of sending email. So need to open our “routes/web.php” file and then add the following route.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\MailController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('send-mail', [MailController::class, 'index']);
Step 6: Create Blade View

Now in this step, i will create the blade view file and then write email that i want to send. So now i just write the some dummy text. Now create the bellow files on “emails” folder.

resources/views/emails/demoMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>codingspont.com</title>
</head>
<body>
    <h1>{{ $mailData['title'] }}</h1>
    <p>{{ $mailData['body'] }}</p>
  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
     
    <p>Thank you</p>
</body>
</html>
Run Laravel App:

That’s good all required steps have been completed , now we need to type the given below command and then hit enter to run the Laravel application:

php artisan serve

Read Also: How To Implement Laravel 9 form validation ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →