coolify/app/Notifications/Channels/EmailChannel.php

68 lines
2.5 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;
2023-09-18 15:04:50 +02:00
use Log;
2023-06-01 12:15:33 +02:00
class EmailChannel
{
public function send(SendsEmail $notifiable, Notification $notification): void
{
try {
$this->bootConfigs($notifiable);
$recepients = $notifiable->getRecepients($notification);
if (count($recepients) === 0) {
throw new Exception('No email recipients found');
}
2023-06-12 12:00:01 +02:00
$mailMessage = $notification->toMail($notifiable);
Mail::send(
[],
[],
fn (Message $message) => $message
->to($recepients)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
} catch (Exception $e) {
ray($e->getMessage());
send_internal_notification("EmailChannel error: {$e->getMessage()}. Failed to send email to: " . implode(', ', $recepients) . " with subject: {$mailMessage->subject}");
throw $e;
2023-06-01 12:15:33 +02:00
}
}
private function bootConfigs($notifiable): void
{
2023-08-31 15:00:59 +02:00
if (data_get($notifiable, 'use_instance_email_settings')) {
$type = set_transanctional_email_settings();
if (!$type) {
throw new Exception('No email settings found.');
}
return;
}
2023-09-02 13:39:44 +02:00
config()->set('mail.from.address', data_get($notifiable, 'smtp_from_address'));
config()->set('mail.from.name', data_get($notifiable, 'smtp_from_name'));
2023-08-31 15:00:59 +02:00
if (data_get($notifiable, 'resend_enabled')) {
config()->set('mail.default', 'resend');
config()->set('resend.api_key', data_get($notifiable, 'resend_api_key'));
}
if (data_get($notifiable, 'smtp_enabled')) {
2023-08-21 11:11:51 +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-08-31 15:00:59 +02:00
"password" => data_get($notifiable, 'smtp_password'),
2023-08-21 11:11:51 +02:00
"timeout" => data_get($notifiable, 'smtp_timeout'),
"local_domain" => null,
]);
}
2023-06-01 12:15:33 +02:00
}
}