An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
refactor(email): streamline email sending logic and improve configuration handling
Andras Bacsai2b046c4bc4d2
diff --git a/app/Notifications/Channels/EmailChannel.php b/app/Notifications/Channels/EmailChannel.phpindex 8287fd24bc..c9bf8d54a6 100644--- a/app/Notifications/Channels/EmailChannel.php+++ b/app/Notifications/Channels/EmailChannel.php@@ -2,102 +2,63 @@namespace App\Notifications\Channels;-use App\Models\Team;-use App\Services\ConfigurationRepository;-use Exception;-use Illuminate\Mail\Message;use Illuminate\Notifications\Notification;-use Illuminate\Support\Facades\Mail;+use Resend;class EmailChannel{- private ConfigurationRepository $configRepository;-- public function __construct(ConfigurationRepository $configRepository)- {- $this->configRepository = $configRepository;- }+ public function __construct() {}public function send(SendsEmail $notifiable, Notification $notification): void{- try {- $team = data_get($notifiable, 'id');- $members = Team::find($team)->members;- $mailerType = $this->bootConfigs($notifiable);-- $recipients = $notifiable->getRecipients();- 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');- }- }-- $mailMessage = $notification->toMail($notifiable);-- Mail::mailer($mailerType)->send(- [],- [],- fn (Message $message) => $message- ->to($recipients)- ->subject($mailMessage->subject)- ->html((string) $mailMessage->render())- );- } catch (Exception $e) {- $error = $e->getMessage();- if ($error === 'No email settings found.') {- throw $e;- }- $message = "EmailChannel error: {$e->getMessage()}. Failed to send email to:";- if (isset($recipients)) {- $message .= implode(', ', $recipients);- }- if (isset($mailMessage)) {- $message .= " with subject: {$mailMessage->subject}";- }- send_internal_notification($message);- throw $e;+ $useInstanceEmailSettings = $notifiable->emailNotificationSettings->use_instance_email_settings;+ if ($useInstanceEmailSettings) {+ $settings = instanceSettings();+ } else {+ $settings = $notifiable->emailNotificationSettings;}- }+ $isResendEnabled = $settings->resend_enabled;+ $isSmtpEnabled = $settings->smtp_enabled;+ $recipients = $notifiable->getRecipients();+ $mailMessage = $notification->toMail($notifiable);- private function bootConfigs($notifiable): string- {- $emailSettings = $notifiable->emailNotificationSettings;+ if ($isResendEnabled) {+ $resend = Resend::client($settings->resend_api_key);+ $resend->emails->send([+ 'from' => $settings->smtp_from_address,+ 'to' => $recipients,+ 'subject' => $mailMessage->subject,+ 'html' => (string) $mailMessage->render(),+ ]);+ } elseif ($isSmtpEnabled) {+ $encryption = match (strtolower($settings->smtp_encryption)) {+ 'starttls' => null,+ 'tls' => 'tls',+ 'none' => null,+ default => null,+ };- if ($emailSettings->use_instance_email_settings) {- $type = set_transanctional_email_settings();- if (blank($type)) {- throw new Exception('No email settings found.');- }+ $transport = new \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport(+ $settings->smtp_host,+ $settings->smtp_port,+ $encryption+ );+ $transport->setUsername($settings->smtp_username);+ $transport->setPassword($settings->smtp_password);- return $type;- }+ $mailer = new \Symfony\Component\Mailer\Mailer($transport);- $this->configRepository->updateMailConfig($emailSettings);+ $fromEmail = $settings->smtp_from_address ?? 'noreply@localhost';+ $fromName = $settings->smtp_from_name ?? 'System';+ $from = new \Symfony\Component\Mime\Address($fromEmail, $fromName);- if ($emailSettings->resend_enabled) {- return 'resend';- }+ $email = (new \Symfony\Component\Mime\Email)+ ->from($from)+ ->to(...$recipients)+ ->subject($mailMessage->subject)+ ->html((string) $mailMessage->render());- if ($emailSettings->smtp_enabled) {- return 'smtp';+ $mailer->send($email);}-- throw new Exception('No email settings found.');}}