coolify/app/Notifications/Channels/EmailChannel.php

55 lines
1.7 KiB
PHP
Raw Normal View History

2023-06-01 12:15:33 +02:00
<?php
namespace App\Notifications\Channels;
2023-08-08 17:28:36 +02:00
use Exception;
2023-06-01 12:15:33 +02:00
use Illuminate\Mail\Message;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Mail;
class EmailChannel
{
public function send(SendsEmail $notifiable, Notification $notification): void
{
$this->bootConfigs($notifiable);
2023-07-28 10:55:26 +02:00
ray($notification);
$recepients = $notifiable->getRecepients($notification);
2023-06-12 12:00:01 +02:00
2023-07-28 10:55:26 +02:00
if (count($recepients) === 0) {
2023-08-08 17:28:36 +02:00
throw new Exception('No email recipients found');
2023-06-01 12:15:33 +02:00
}
2023-07-28 10:55:26 +02:00
2023-06-01 12:15:33 +02:00
$mailMessage = $notification->toMail($notifiable);
Mail::send(
[],
[],
2023-08-11 20:48:52 +02:00
fn (Message $message) => $message
2023-06-01 12:15:33 +02:00
->from(
data_get($notifiable, 'smtp_from_address'),
data_get($notifiable, 'smtp_from_name'),
2023-06-01 12:15:33 +02:00
)
2023-07-28 10:55:26 +02:00
->bcc($recepients)
2023-06-01 12:15:33 +02:00
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
}
private function bootConfigs($notifiable): void
{
$password = data_get($notifiable, 'smtp_password');
2023-06-20 21:18:14 +02:00
if ($password) $password = decrypt($password);
2023-06-01 12:15:33 +02:00
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
"host" => data_get($notifiable, 'smtp_host'),
"port" => data_get($notifiable, 'smtp_port'),
"encryption" => data_get($notifiable, 'smtp_encryption'),
"username" => data_get($notifiable, 'smtp_username'),
2023-06-20 21:18:14 +02:00
"password" => $password,
"timeout" => data_get($notifiable, 'smtp_timeout'),
2023-06-01 12:15:33 +02:00
"local_domain" => null,
]);
}
}