57 lines
2.0 KiB
PHP
Raw Normal View History

2023-06-01 12:15:33 +02:00
<?php
namespace App\Notifications\Channels;
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-06-12 12:00:01 +02:00
$is_test_notification = $notification instanceof \App\Notifications\TestNotification;
if ($is_test_notification) {
2023-06-06 17:50:13 +02:00
$bcc = $notifiable->routeNotificationForEmail('smtp_test_recipients');
2023-06-12 12:52:58 +02:00
if (count($bcc) === 0) {
if ($notifiable instanceof \App\Models\Team) {
$bcc = $notifiable->members()->pluck('email')->toArray();
}
}
2023-06-01 12:15:33 +02:00
} else {
$bcc = $notifiable->routeNotificationForEmail();
}
$mailMessage = $notification->toMail($notifiable);
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
2023-06-06 17:50:13 +02:00
$notifiable->extra_attributes?->get('smtp_from_address'),
$notifiable->extra_attributes?->get('smtp_from_name')
2023-06-01 12:15:33 +02:00
)
->bcc($bcc)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
}
private function bootConfigs($notifiable): void
{
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
2023-06-01 13:24:20 +02:00
"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'),
2023-06-01 12:15:33 +02:00
"local_domain" => null,
]);
}
}