Deliver notifications in email

Hello, I am wondering if there is any way to extend the Filament Notification class to be able to send notifications in email in a way that is similar to ->toBroadcast($user) that is like ->toEmail($user). It doesn't need to be the most perfect email but my code is mostly based around Filament notifications so something simple to implement across the whole app is required.
Solution:
```php <?php namespace App\Notifications; ...
Jump to solution
12 Replies
Dennis Koch
Dennis Koch4mo ago
Look at implementation of toBroadcast() or toDatabase(). It's quite simple. You could extend Filament\Notification and add toMail(), then add a generic MailNotification that takes that data and sends mails.
Yiğit Kerem
Yiğit KeremOP4mo ago
Do I just create a new class than extends Filament\Notification? or does it need to be a plugin?
Dennis Koch
Dennis Koch4mo ago
No need to be a plugin. Just a normal class using PHP. And instead of using the Filament class use your new class then
Yiğit Kerem
Yiğit KeremOP4mo ago
Thank you so much! Is it okay if I keep this discussion open while I try to implement it for any issues that may arise or should I reopen if any help is required?
Dennis Koch
Dennis Koch4mo ago
Keep it open and come back if you need further assistance.
Yiğit Kerem
Yiğit KeremOP4mo ago
Hey, is there any way to accomplish this without having to change the namespaces? (Adding the modifier to the original Filament class) Not sure if that is possible in terms of how php itself works, obviously
Dennis Koch
Dennis Koch4mo ago
Laravel has a Macro feature that is used by the Notification class. So you can define your own method. Check this article: https://levelup.gitconnected.com/understanding-laravel-macros-e2f493484a38
Medium
Understanding Laravel Macros
Laravel macros allow us to add custom functionality to Laravel core components or classes. In other words, they allow us to extend Laravel…
Yiğit Kerem
Yiğit KeremOP4mo ago
Thank you so much!
Solution
Yiğit Kerem
Yiğit Kerem4mo ago
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class FilamentNotificationEmail extends Notification
{
use Queueable;

protected \Filament\Notifications\Notification $filament_notification;

/**
* Create a new notification instance.
*/
public function __construct(\Filament\Notifications\Notification $filament_notification)
{
$this->filament_notification = $filament_notification;
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{

$action = $this->filament_notification->getActions();
$action = $action[0] ?? null;

return (new MailMessage)
->subject($this->filament_notification->getTitle())
->greeting('Hello ' . $notifiable->name . ',')
->line($this->filament_notification->getBody())
->action($action ? $action->getLabel() : "Visit App", $action ? $action->getUrl() : env("APP_URL"));
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class FilamentNotificationEmail extends Notification
{
use Queueable;

protected \Filament\Notifications\Notification $filament_notification;

/**
* Create a new notification instance.
*/
public function __construct(\Filament\Notifications\Notification $filament_notification)
{
$this->filament_notification = $filament_notification;
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{

$action = $this->filament_notification->getActions();
$action = $action[0] ?? null;

return (new MailMessage)
->subject($this->filament_notification->getTitle())
->greeting('Hello ' . $notifiable->name . ',')
->line($this->filament_notification->getBody())
->action($action ? $action->getLabel() : "Visit App", $action ? $action->getUrl() : env("APP_URL"));
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
Yiğit Kerem
Yiğit KeremOP4mo ago
<?php

public function boot(): void
{
.
.
.

Notification::macro('toEmail', function (Collection|User $users): void {
if ($users instanceof User) {
$users->notify(new FilamentNotificationEmail($this));
} else {
foreach ($users as $user) {
$users->notify(new FilamentNotificationEmail($this));
}
}
});

}
<?php

public function boot(): void
{
.
.
.

Notification::macro('toEmail', function (Collection|User $users): void {
if ($users instanceof User) {
$users->notify(new FilamentNotificationEmail($this));
} else {
foreach ($users as $user) {
$users->notify(new FilamentNotificationEmail($this));
}
}
});

}
This is how I solved it for future reference for anybody who needs it. I'm not sure if it is the cleanest way of doing things but I would however believe such a simple addition to the upstream could be helpful in many cases. Is there any reason it is neglected?
Dennis Koch
Dennis Koch4mo ago
Thanks for sharing.
Is there any reason it is neglected?
I just think it's not that common. Filament Notifications are mostly meant to be inside Filament
Yiğit Kerem
Yiğit KeremOP4mo ago
Thank you for the awesome project and the help. Take care!
Want results from more Discord servers?
Add your server