coolify/app/Notifications/Channels/EmailChannel.php

104 lines
3.5 KiB
PHP
Raw Normal View History

2023-06-01 10:15:33 +00:00
<?php
namespace App\Notifications\Channels;
use App\Models\Team;
use App\Services\ConfigurationRepository;
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
{
private ConfigurationRepository $configRepository;
public function __construct(ConfigurationRepository $configRepository)
{
$this->configRepository = $configRepository;
}
public function send(SendsEmail $notifiable, Notification $notification): void
2023-06-01 10:15:33 +00:00
{
try {
$team = data_get($notifiable, 'id');
$members = Team::find($team)->members;
$mailerType = $this->bootConfigs($notifiable);
$recipients = $notifiable->getRecipients();
2024-01-07 22:32:54 +00:00
if (count($recipients) === 0) {
throw new Exception('No email recipients found');
}
foreach ($recipients as $recipient) {
// check if the recipient is part of the team
if (! $members->contains('email', $recipient)) {
$emailSettings = $notifiable->emailNotificationSettings;
data_set($emailSettings, 'smtp_password', '********');
data_set($emailSettings, 'resend_api_key', '********');
send_internal_notification(sprintf(
"Recipient is not part of the team: %s\nTeam: %s\nNotification: %s\nNotifiable: %s\nMailer Type: %s\nEmail Settings:\n%s",
$recipient,
$team,
get_class($notification),
get_class($notifiable),
$mailerType,
json_encode($emailSettings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
));
throw new Exception('Recipient is not part of the team');
}
}
2023-06-12 10:00:01 +00:00
$mailMessage = $notification->toMail($notifiable);
Mail::mailer($mailerType)->send(
[],
[],
fn (Message $message) => $message
2024-01-07 22:32:54 +00:00
->to($recipients)
->subject($mailMessage->subject)
2024-06-10 20:43:34 +00:00
->html((string) $mailMessage->render())
);
} catch (Exception $e) {
$error = $e->getMessage();
if ($error === 'No email settings found.') {
throw $e;
}
2023-10-18 12:22:09 +00:00
$message = "EmailChannel error: {$e->getMessage()}. Failed to send email to:";
2024-01-07 22:32:54 +00:00
if (isset($recipients)) {
$message .= implode(', ', $recipients);
2023-10-18 12:22:09 +00:00
}
2023-11-06 09:23:51 +00:00
if (isset($mailMessage)) {
$message .= " with subject: {$mailMessage->subject}";
}
2023-10-18 12:22:09 +00:00
send_internal_notification($message);
throw $e;
2023-06-01 10:15:33 +00:00
}
}
private function bootConfigs($notifiable): string
2023-06-01 10:15:33 +00:00
{
$emailSettings = $notifiable->emailNotificationSettings;
if ($emailSettings->use_instance_email_settings) {
2023-08-31 13:00:59 +00:00
$type = set_transanctional_email_settings();
if (blank($type)) {
2023-08-31 13:00:59 +00:00
throw new Exception('No email settings found.');
}
return $type;
2023-08-31 13:00:59 +00:00
}
$this->configRepository->updateMailConfig($emailSettings);
if ($emailSettings->resend_enabled) {
return 'resend';
}
if ($emailSettings->smtp_enabled) {
return 'smtp';
}
throw new Exception('No email settings found.');
2023-06-01 10:15:33 +00:00
}
}