How to Send Email Notification in Laravel 11

Hello, laravel web developers! In this article, we'll see how to send email notifications in laravel 11. Here, we'll use the laravel 11 notification class to send notifications. Laravel provides support for sending notifications across a variety of delivery channels, including email, SMS, and Slack.

In Laravel, each notification is represented by a single class that is typically stored in the app/Notifications directory.

it will be created for you when you run the make:notification Artisan command

Laravel 11 Send Email Notifications

laravel 11 send notifications

 

Step 1: Install Laravel 11 Application

In this step, we'll install the laravel 11 application using the following command.

composer create-project laravel/laravel laravel-11-example

 

Step 2: Create Migration

Next, we'll create a migration to add the due date to the user's table using the following command.

php artisan make:migration add_due_date_column_to_users_table

Migration:

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->date('due_date')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        
    }
};

Then, we'll migrate the migration using the following command.

php artisan migrate

 

 

Step 3: Update Model

Then, we'll update the User.php model. In this model, we'll add an Illuminate\Notifications\Notifiable class for sending notifications.

app/Models/User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'due_date'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }
}

 

Step 4: Create Notification Class

Next, we'll create a notification class using the following command.

php artisan make:notification Invoice

app/Notifications/Invoice.php

<?php
  
namespace App\Notifications;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
  
class Invoice extends Notification
{
    use Queueable;
  
    private $messages;
  
    /**
     * Create a new notification instance.
     */
    public function __construct($messages)
    {
        $this->messages = $messages;
    }
  
    /**
     * Get the notification's delivery channels.
     *
     * @return array
     */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }
  
    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
                    ->line($this->messages['hello'])
                    ->line($this->messages['invoice_message'])
                    ->line('Thank you for choosing our application!');
    }
  
    /**
     * Get the array representation of the notification.
     *
     * @return array
     */
    public function toArray(object $notifiable): array
    {
        return [
              
        ];
    }
}

Then, we'll set the email configuration into the .env file.

.env

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=123456
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=from@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

 

 

Step 5: Define Route

Now, we'll define the routes into the web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\UserController;
    
Route::get('send-invoice-notification', [UserController::class, 'index']);

 

Step 6: Create Controller

Then, we'll create a UserController.php file and use the Invoice notification.

app/Http/Controllers/UserController.php

<?php
   
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
use App\Notifications\Invoice;
    
class UserController extends Controller
{   
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user = User::find(1);
  
        $messages["hello"] = "Hello {$user->name}";
        $messages["invoice_message"] = "Your invoice amount ${$user->amount} due date is {$user->due_date}; please review the attached payment details.";
          
        $user->notify(new Invoice($messages));
  
        dd('Invoice send successfully');
    }
}

 

Step 7: Run the Laravel 11 Application

Now, run the laravel 11 application using the following command.

php artisan serve

 


You might also like :

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS