Sending e-mails and settings on Team.
This commit is contained in:
41
app/Notifications/Channels/CoolifyEmailChannel.php
Normal file
41
app/Notifications/Channels/CoolifyEmailChannel.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Channels;
|
||||
|
||||
use Illuminate\Mail\Message;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class CoolifyEmailChannel
|
||||
{
|
||||
/**
|
||||
* Send the given notification.
|
||||
*/
|
||||
public function send(SendsCoolifyEmail $notifiable, Notification $notification): void
|
||||
{
|
||||
$this->bootConfigs($notifiable);
|
||||
$bcc = $notifiable->routeNotificationForCoolifyEmail();
|
||||
$mailMessage = $notification->toMail($notifiable);
|
||||
|
||||
Mail::send([], [], fn(Message $message) => $message
|
||||
->from('ask@me.com', 'My Coolify Instance')
|
||||
->bcc($bcc)
|
||||
->subject($mailMessage->subject)
|
||||
->html((string) $mailMessage->render())
|
||||
);
|
||||
}
|
||||
|
||||
private function bootConfigs($notifiable): void
|
||||
{
|
||||
config()->set('mail.mailers.smtp', [
|
||||
"transport" => "smtp",
|
||||
"host" => $notifiable->extra_attributes?->get('smtp_host'),
|
||||
"port" => $notifiable->extra_attributes?->get('smtp_port'),
|
||||
"encryption" => $notifiable->extra_attributes?->get('smtp_encryption'),
|
||||
"username" => $notifiable->extra_attributes?->get('smtp_username'),
|
||||
"password" => $notifiable->extra_attributes?->get('smtp_password'),
|
||||
"timeout" => $notifiable->extra_attributes?->get('smtp_timeout'),
|
||||
"local_domain" => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace App\Notifications\Channels;
|
||||
|
||||
use App\Jobs\SendMessageToDiscordJob;
|
||||
use App\Models\InstanceSettings;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class DiscordChannel
|
||||
@@ -11,14 +10,11 @@ class DiscordChannel
|
||||
/**
|
||||
* Send the given notification.
|
||||
*/
|
||||
public function send(object $notifiable, Notification $notification): void
|
||||
public function send(SendsDiscord $notifiable, Notification $notification): void
|
||||
{
|
||||
$message = $notification->toDiscord($notifiable);
|
||||
|
||||
$webhookUrl = data_get(
|
||||
InstanceSettings::get(),
|
||||
'extra_attributes.discord_webhook'
|
||||
);
|
||||
$webhookUrl = $notifiable->routeNotificationForDiscord();
|
||||
|
||||
dispatch(new SendMessageToDiscordJob($message, $webhookUrl));
|
||||
}
|
||||
|
||||
8
app/Notifications/Channels/SendsCoolifyEmail.php
Normal file
8
app/Notifications/Channels/SendsCoolifyEmail.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Channels;
|
||||
|
||||
interface SendsCoolifyEmail
|
||||
{
|
||||
public function routeNotificationForCoolifyEmail();
|
||||
}
|
||||
8
app/Notifications/Channels/SendsDiscord.php
Normal file
8
app/Notifications/Channels/SendsDiscord.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Channels;
|
||||
|
||||
interface SendsDiscord
|
||||
{
|
||||
public function routeNotificationForDiscord();
|
||||
}
|
||||
@@ -2,13 +2,14 @@
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Notifications\Channels\CoolifyEmailChannel;
|
||||
use App\Notifications\Channels\DiscordChannel;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class TestMessage extends Notification
|
||||
class DemoNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
@@ -27,7 +28,10 @@ class TestMessage extends Notification
|
||||
*/
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
return ['mail', DiscordChannel::class];
|
||||
$channels = [];
|
||||
$notifiable->extra_attributes?->get('smtp_active') && $channels[] = CoolifyEmailChannel::class;
|
||||
$notifiable->extra_attributes?->get('discord_active') && $channels[] = DiscordChannel::class;
|
||||
return $channels;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,33 +39,16 @@ class TestMessage extends Notification
|
||||
*/
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
{
|
||||
$smtp = [
|
||||
"transport" => "smtp",
|
||||
"host" => "mailpit",
|
||||
"port" => 1025,
|
||||
"encryption" => 'tls',
|
||||
"username" => null,
|
||||
"password" => null,
|
||||
"timeout" => null,
|
||||
"local_domain" => null,
|
||||
];
|
||||
config()->set('mail.mailers.smtp', $smtp);
|
||||
|
||||
\Illuminate\Support\Facades\Mail::mailer('smtp')
|
||||
->to('ask@me.com')
|
||||
->send(new \App\Mail\ExampleMail);
|
||||
|
||||
|
||||
|
||||
return (new MailMessage)
|
||||
->subject('Coolify demo notification')
|
||||
->line('Welcome to Coolify!')
|
||||
->error()
|
||||
->action('Go to dashboard', url('/'))
|
||||
->line('We need your attention for disk usage.');
|
||||
}
|
||||
|
||||
public function toDiscord(object $notifiable): string
|
||||
{
|
||||
ray('1111');
|
||||
return 'Welcome to Coolify! We need your attention for disk usage. [Go to dashboard]('.url('/').')';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user