Use temp config for mail testing (#912)

* Use temp config

* Change port when changing encryption

* Pint

* Use finally

* Pint please do your job next time

Co-authored-by: Boy132 <Boy132@users.noreply.github.com>

---------

Co-authored-by: Boy132 <Boy132@users.noreply.github.com>
This commit is contained in:
MartinOscar 2025-01-15 16:07:12 +01:00 committed by GitHub
parent e145fcdc56
commit fe43539ea7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -263,8 +263,38 @@ class Settings extends Page implements HasForms
->icon('tabler-send') ->icon('tabler-send')
->hidden(fn (Get $get) => $get('MAIL_MAILER') === 'log') ->hidden(fn (Get $get) => $get('MAIL_MAILER') === 'log')
->authorize(fn () => auth()->user()->can('update settings')) ->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 { 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) MailNotification::route('mail', auth()->user()->email)
->notify(new MailTested(auth()->user())); ->notify(new MailTested(auth()->user()));
@ -278,6 +308,8 @@ class Settings extends Page implements HasForms
->body($exception->getMessage()) ->body($exception->getMessage())
->danger() ->danger()
->send(); ->send();
} finally {
config($originalConfig);
} }
}) })
), ),
@ -321,8 +353,21 @@ class Settings extends Page implements HasForms
ToggleButtons::make('MAIL_ENCRYPTION') ToggleButtons::make('MAIL_ENCRYPTION')
->label('Encryption') ->label('Encryption')
->inline() ->inline()
->options(['tls' => 'TLS', 'ssl' => 'SSL', '' => 'None']) ->options([
->default(env('MAIL_ENCRYPTION', config('mail.mailers.smtp.encryption', 'tls'))), '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') Section::make('Mailgun Configuration')
->columns() ->columns()