lasthourcloud/app/Notifications/Channels/EmailChannel.php

54 lines
1.7 KiB
PHP
Raw Normal View History

2023-06-01 10:15:33 +00: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 10:00:01 +00:00
2023-06-20 17:08:43 +00:00
$bcc = $notifiable->routeNotificationForEmail('test_recipients');
2023-06-19 12:31:42 +00:00
if (count($bcc) === 0) {
if ($notifiable instanceof \App\Models\Team) {
$bcc = $notifiable->members()->pluck('email')->toArray();
2023-06-12 10:52:58 +00:00
}
2023-06-01 10:15:33 +00:00
}
$mailMessage = $notification->toMail($notifiable);
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
2023-06-20 17:08:43 +00:00
data_get($notifiable, 'smtp.from_address'),
data_get($notifiable, 'smtp.from_name'),
2023-06-01 10:15:33 +00:00
)
->bcc($bcc)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
}
private function bootConfigs($notifiable): void
{
2023-06-20 19:18:14 +00:00
$password = data_get($notifiable, 'smtp.password');
if ($password) $password = decrypt($password);
2023-06-01 10:15:33 +00:00
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
2023-06-20 17:08:43 +00:00
"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 19:18:14 +00:00
"password" => $password,
2023-06-20 17:08:43 +00:00
"timeout" => data_get($notifiable, 'smtp.timeout'),
2023-06-01 10:15:33 +00:00
"local_domain" => null,
]);
}
}