diff --git a/app/Filament/Admin/Pages/Settings.php b/app/Filament/Admin/Pages/Settings.php index 4332388c9..97b287f14 100644 --- a/app/Filament/Admin/Pages/Settings.php +++ b/app/Filament/Admin/Pages/Settings.php @@ -263,8 +263,38 @@ class Settings extends Page implements HasForms ->icon('tabler-send') ->hidden(fn (Get $get) => $get('MAIL_MAILER') === 'log') ->authorize(fn () => auth()->user()->can('update settings')) - ->action(function () { + ->action(function (Get $get) { + // Store original mail configuration + $originalConfig = [ + 'mail.default' => config('mail.default'), + 'mail.mailers.smtp.host' => config('mail.mailers.smtp.host'), + 'mail.mailers.smtp.port' => config('mail.mailers.smtp.port'), + 'mail.mailers.smtp.username' => config('mail.mailers.smtp.username'), + 'mail.mailers.smtp.password' => config('mail.mailers.smtp.password'), + 'mail.mailers.smtp.encryption' => config('mail.mailers.smtp.encryption'), + 'mail.from.address' => config('mail.from.address'), + 'mail.from.name' => config('mail.from.name'), + 'services.mailgun.domain' => config('services.mailgun.domain'), + 'services.mailgun.secret' => config('services.mailgun.secret'), + 'services.mailgun.endpoint' => config('services.mailgun.endpoint'), + ]; + try { + // Update mail configuration dynamically + config([ + 'mail.default' => $get('MAIL_MAILER'), + 'mail.mailers.smtp.host' => $get('MAIL_HOST'), + 'mail.mailers.smtp.port' => $get('MAIL_PORT'), + 'mail.mailers.smtp.username' => $get('MAIL_USERNAME'), + 'mail.mailers.smtp.password' => $get('MAIL_PASSWORD'), + 'mail.mailers.smtp.encryption' => $get('MAIL_ENCRYPTION'), + 'mail.from.address' => $get('MAIL_FROM_ADDRESS'), + 'mail.from.name' => $get('MAIL_FROM_NAME'), + 'services.mailgun.domain' => $get('MAILGUN_DOMAIN'), + 'services.mailgun.secret' => $get('MAILGUN_SECRET'), + 'services.mailgun.endpoint' => $get('MAILGUN_ENDPOINT'), + ]); + MailNotification::route('mail', auth()->user()->email) ->notify(new MailTested(auth()->user())); @@ -278,6 +308,8 @@ class Settings extends Page implements HasForms ->body($exception->getMessage()) ->danger() ->send(); + } finally { + config($originalConfig); } }) ), @@ -321,8 +353,21 @@ class Settings extends Page implements HasForms ToggleButtons::make('MAIL_ENCRYPTION') ->label('Encryption') ->inline() - ->options(['tls' => 'TLS', 'ssl' => 'SSL', '' => 'None']) - ->default(env('MAIL_ENCRYPTION', config('mail.mailers.smtp.encryption', 'tls'))), + ->options([ + 'tls' => 'TLS', + 'ssl' => 'SSL', + '' => 'None', + ]) + ->default(env('MAIL_ENCRYPTION', config('mail.mailers.smtp.encryption', 'tls'))) + ->live() + ->afterStateUpdated(function ($state, Set $set) { + $port = match ($state) { + 'tls' => 587, + 'ssl' => 465, + default => 25, + }; + $set('MAIL_PORT', $port); + }), ]), Section::make('Mailgun Configuration') ->columns()