2023-06-01 10:15:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Notifications\Channels;
|
|
|
|
|
2023-08-08 15:28:36 +00:00
|
|
|
use Exception;
|
2023-06-01 10:15:33 +00:00
|
|
|
use Illuminate\Mail\Message;
|
|
|
|
use Illuminate\Notifications\Notification;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
|
|
|
class EmailChannel
|
|
|
|
{
|
2023-08-31 11:10:39 +00:00
|
|
|
private bool $isResend = false;
|
2023-06-01 10:15:33 +00:00
|
|
|
public function send(SendsEmail $notifiable, Notification $notification): void
|
|
|
|
{
|
|
|
|
$this->bootConfigs($notifiable);
|
2023-07-28 08:55:26 +00:00
|
|
|
$recepients = $notifiable->getRecepients($notification);
|
2023-06-12 10:00:01 +00:00
|
|
|
|
2023-07-28 08:55:26 +00:00
|
|
|
if (count($recepients) === 0) {
|
2023-08-08 15:28:36 +00:00
|
|
|
throw new Exception('No email recipients found');
|
2023-06-01 10:15:33 +00:00
|
|
|
}
|
2023-07-28 08:55:26 +00:00
|
|
|
|
2023-06-01 10:15:33 +00:00
|
|
|
$mailMessage = $notification->toMail($notifiable);
|
2023-08-31 11:10:39 +00:00
|
|
|
if ($this->isResend) {
|
2023-08-31 13:00:59 +00:00
|
|
|
foreach ($recepients as $receipient) {
|
2023-08-31 11:10:39 +00:00
|
|
|
Mail::send(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
fn (Message $message) => $message
|
|
|
|
->from(
|
|
|
|
data_get($notifiable, 'smtp_from_address'),
|
|
|
|
data_get($notifiable, 'smtp_from_name'),
|
|
|
|
)
|
|
|
|
->to($receipient)
|
|
|
|
->subject($mailMessage->subject)
|
|
|
|
->html((string)$mailMessage->render())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Mail::send(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
fn (Message $message) => $message
|
|
|
|
->from(
|
|
|
|
data_get($notifiable, 'smtp_from_address'),
|
|
|
|
data_get($notifiable, 'smtp_from_name'),
|
|
|
|
)
|
|
|
|
->bcc($recepients)
|
|
|
|
->subject($mailMessage->subject)
|
|
|
|
->html((string)$mailMessage->render())
|
|
|
|
);
|
|
|
|
}
|
2023-06-01 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function bootConfigs($notifiable): void
|
|
|
|
{
|
2023-08-31 13:00:59 +00:00
|
|
|
if (data_get($notifiable, 'use_instance_email_settings')) {
|
|
|
|
$type = set_transanctional_email_settings();
|
|
|
|
if (!$type) {
|
|
|
|
throw new Exception('No email settings found.');
|
|
|
|
}
|
|
|
|
if ($type === 'resend') {
|
2023-08-31 11:10:39 +00:00
|
|
|
$this->isResend = true;
|
|
|
|
}
|
2023-08-31 13:00:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (data_get($notifiable, 'resend_enabled')) {
|
|
|
|
$this->isResend = true;
|
|
|
|
config()->set('mail.default', 'resend');
|
|
|
|
config()->set('resend.api_key', data_get($notifiable, 'resend_api_key'));
|
2023-08-31 11:10:39 +00:00
|
|
|
}
|
|
|
|
if (data_get($notifiable, 'smtp_enabled')) {
|
2023-08-21 09:11:51 +00: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-08-31 13:00:59 +00:00
|
|
|
"password" => data_get($notifiable, 'smtp_password'),
|
2023-08-21 09:11:51 +00:00
|
|
|
"timeout" => data_get($notifiable, 'smtp_timeout'),
|
|
|
|
"local_domain" => null,
|
|
|
|
]);
|
|
|
|
}
|
2023-06-01 10:15:33 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|