diff --git a/bootstrap/helpers/notifications.php b/bootstrap/helpers/notifications.php new file mode 100644 index 000000000..3b1eb758b --- /dev/null +++ b/bootstrap/helpers/notifications.php @@ -0,0 +1,87 @@ +smtp_enabled || $settings->resend_enabled; +} + +function send_internal_notification(string $message): void +{ + try { + $team = Team::find(0); + $team?->notify(new GeneralNotification($message)); + } catch (\Throwable $e) { + ray($e->getMessage()); + } +} + +function send_user_an_email(MailMessage $mail, string $email, ?string $cc = null): void +{ + $settings = instanceSettings(); + $type = set_transanctional_email_settings($settings); + if (! $type) { + throw new Exception('No email settings found.'); + } + if ($cc) { + Mail::send( + [], + [], + fn (Message $message) => $message + ->to($email) + ->replyTo($email) + ->cc($cc) + ->subject($mail->subject) + ->html((string) $mail->render()) + ); + } else { + Mail::send( + [], + [], + fn (Message $message) => $message + ->to($email) + ->subject($mail->subject) + ->html((string) $mail->render()) + ); + } +} + +function set_transanctional_email_settings(?InstanceSettings $settings = null): ?string // +{ + if (! $settings) { + $settings = instanceSettings(); + } + config()->set('mail.from.address', data_get($settings, 'smtp_from_address')); + config()->set('mail.from.name', data_get($settings, 'smtp_from_name')); + if (data_get($settings, 'resend_enabled')) { + config()->set('mail.default', 'resend'); + config()->set('resend.api_key', data_get($settings, 'resend_api_key')); + + return 'resend'; + } + if (data_get($settings, 'smtp_enabled')) { + config()->set('mail.default', 'smtp'); + config()->set('mail.mailers.smtp', [ + 'transport' => 'smtp', + 'host' => data_get($settings, 'smtp_host'), + 'port' => data_get($settings, 'smtp_port'), + 'encryption' => data_get($settings, 'smtp_encryption'), + 'username' => data_get($settings, 'smtp_username'), + 'password' => data_get($settings, 'smtp_password'), + 'timeout' => data_get($settings, 'smtp_timeout'), + 'local_domain' => null, + ]); + + return 'smtp'; + } + + return null; +}