coolify/app/Notifications/Channels/TransactionalEmailChannel.php

69 lines
2.0 KiB
PHP
Raw Normal View History

2023-06-12 12:00:01 +02:00
<?php
namespace App\Notifications\Channels;
use App\Models\InstanceSettings;
use App\Models\User;
2023-08-31 15:00:59 +02:00
use Exception;
2023-06-12 12:00:01 +02:00
use Illuminate\Mail\Message;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Mail;
2023-08-31 15:00:59 +02:00
use Log;
2023-06-12 12:00:01 +02:00
class TransactionalEmailChannel
{
2023-08-31 15:00:59 +02:00
private bool $isResend = false;
2023-06-12 12:00:01 +02:00
public function send(User $notifiable, Notification $notification): void
{
2023-06-13 10:51:58 +02:00
$settings = InstanceSettings::get();
2023-08-31 15:00:59 +02:00
if (!data_get($settings, 'smtp_enabled') && !data_get($settings, 'resend_enabled')) {
Log::info('SMTP/Resend not enabled');
2023-06-13 10:51:58 +02:00
return;
}
2023-06-12 12:00:01 +02:00
$email = $notifiable->email;
if (!$email) {
return;
}
2023-06-20 19:08:43 +02:00
$this->bootConfigs();
2023-06-12 12:00:01 +02:00
$mailMessage = $notification->toMail($notifiable);
2023-08-31 15:00:59 +02:00
if ($this->isResend) {
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
data_get($settings, 'smtp_from_address'),
data_get($settings, 'smtp_from_name'),
)
->to($email)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
} else {
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
data_get($settings, 'smtp_from_address'),
data_get($settings, 'smtp_from_name'),
)
->bcc($email)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
}
2023-06-12 12:00:01 +02:00
}
2023-06-20 19:08:43 +02:00
private function bootConfigs(): void
2023-06-12 12:00:01 +02:00
{
2023-08-31 15:00:59 +02:00
$type = set_transanctional_email_settings();
if (!$type) {
throw new Exception('No email settings found.');
}
if ($type === 'resend') {
$this->isResend = true;
}
2023-06-12 12:00:01 +02:00
}
}