In this tutorial we'll learn how we can send notification to our registered users.
Laravel provides us variety of support for sending notifications. You can see the more details in the official document.
Now let's see how we can implement notification system and send users to notification.
Note: Tested on Laravel 11.x.
At first we'll install a fresh Laravel application by invoking the below command in our terminal
composer create-project --prefer-dist laravel/laravel notification
or via Laravel installer
laravel new notification
Now we'll create a notification class where we send our users notification.
– Fire the below command in your terminal
php artisan make:notification UserNotify
– It'll create a file (UserNotify.php
) under app/Notifications.
– Open the file (app/Notifications/UserNotify.php
) and replace with the below code:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class UserNotify extends Notification
{
use Queueable;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->greeting('Hello User')
->line('Notification title : '.$this->data['title']) //Send with post title
->action('Visit Our Site', 'https://xdevspace.com')
->line('Thank you for using our Website!');
}
public function toArray($notifiable)
{
return [
//
];
}
}
And that's it we've successfully create our notification.
Now at first we've to make a controller where we can put our logic like when we want to send user notification etc.
– So, fire the below command to create a controller:
php artisan make:controller NotificationController
– It'll create a controller file (NotificationController.php
) under app/Http/Controllers.
– Open the file (app/Http/Controllers/NotificationController.php
) put the below code:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Notifications\UserNotify;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
class NotificationController extends Controller
{
public function sendNotification(Request $request): \Illuminate\Http\JsonResponse
{
//do your logic if any
//any additional data if you want to send in notification.
$data = [
'title' => 'Notification Title',
];
$subscribers = User::pluck('name', 'email')->toArray();
Notification::route('mail', $subscribers) //Sending mail to subscribers
->notify(new UserNotify($data)); //With new post
return response()->json([
'success' => 'Mail Send Successfully'
]);
}
}
Open the .env
file in the root directory of your Laravel application, and configure mail server details in it; Something like this:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=Add your email name here
MAIL_PASSWORD=Add your password here
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
Put the following route in your web.php
file (routes/web.php
).
Route::get('send-notification',[\App\Http\Controllers\NotificationController::class,'sendNotification'])->name('send.notification');
– And that's it. We'll done with our coding part.
Run artisan serve
command to start the application server for testing:
php artisan serve
– Now it's time to check the output. If we go to http://127.0.0.1:8000/send-notification
then mail be send to all users and we can see the below email body in our inbox.
More information about Laravel 11 Send Mail with Attachments.
All Comments