diff --git a/app/Http/Controllers/Admin/Settings/MailController.php b/app/Http/Controllers/Admin/Settings/MailController.php index f0b4495a4..b1e0497d5 100644 --- a/app/Http/Controllers/Admin/Settings/MailController.php +++ b/app/Http/Controllers/Admin/Settings/MailController.php @@ -2,6 +2,10 @@ namespace Pterodactyl\Http\Controllers\Admin\Settings; +use Exception; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Notification; use Illuminate\View\View; use Illuminate\Http\RedirectResponse; use Prologue\Alerts\AlertsMessageBag; @@ -9,6 +13,7 @@ use Illuminate\Contracts\Console\Kernel; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Illuminate\Contracts\Encryption\Encrypter; +use Pterodactyl\Notifications\MailTested; use Pterodactyl\Providers\SettingsServiceProvider; use Illuminate\Contracts\Config\Repository as ConfigRepository; use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; @@ -90,7 +95,7 @@ class MailController extends Controller public function update(MailSettingsFormRequest $request): RedirectResponse { if ($this->config->get('mail.driver') !== 'smtp') { - throw new DisplayException('This feature is only available if SMTP is the selected email driver for the Panel.'); + throw $this->smtpNotSelectedException(); } $values = $request->normalize(); @@ -111,4 +116,39 @@ class MailController extends Controller return redirect()->route('admin.settings.mail'); } + + /** + * Submit a request to send a test mail message. + * + * @throws DisplayException + * @param Request $request + * @return \Illuminate\Http\RedirectResponse + */ + public function test(Request $request): RedirectResponse + { + if ($this->config->get('mail.driver') !== 'smtp') { + throw $this->smtpNotSelectedException(); + } + + try { + Log::debug('Sending test message to ' . $request->user()->email); + Notification::route('mail', $request->user()->email) + ->notify(new MailTested($request->user())); + } catch (Exception $exception) { + $this->alert->danger(trans('base.mail.test_failed'))->flash(); + return redirect()->route('admin.settings.mail'); + } + + $this->alert->success(trans('base.mail.test_succeeded'))->flash(); + return redirect()->route('admin.settings.mail'); + } + + /** + * Generate a display exception for non-SMTP configurations. + * + * @return DisplayException + */ + private function smtpNotSelectedException() { + return new DisplayException('This feature is only available if SMTP is the selected email driver for the Panel.'); + } } diff --git a/app/Notifications/MailTested.php b/app/Notifications/MailTested.php new file mode 100644 index 000000000..179666db3 --- /dev/null +++ b/app/Notifications/MailTested.php @@ -0,0 +1,32 @@ +user = $user; + } + + public function via() + { + return ['mail']; + } + + public function toMail() + { + return (new MailMessage) + ->subject('Pterodactyl Test Message') + ->greeting('Hello ' . $this->user->name . '!') + ->line('This is a test of the Pterodactyl mail system. You\'re good to go!'); + } +} diff --git a/resources/lang/en/base.php b/resources/lang/en/base.php index e2c661067..588d2fbe2 100644 --- a/resources/lang/en/base.php +++ b/resources/lang/en/base.php @@ -86,4 +86,8 @@ return [ '2fa_checkpoint_help' => 'Use the 2FA application on your phone to take a picture of the QR code to the left, or manually enter the code under it. Once you have done so, generate a token and enter it below.', '2fa_disable_error' => 'The 2FA token provided was not valid. Protection has not been disabled for this account.', ], + 'mail' => [ + 'test_succeeded' => 'The test message was sent successfully!', + 'test_failed' => 'Failed to send test message! Check your configuration and try again.', + ], ]; diff --git a/resources/themes/pterodactyl/admin/settings/mail.blade.php b/resources/themes/pterodactyl/admin/settings/mail.blade.php index 40403993f..f439634c6 100644 --- a/resources/themes/pterodactyl/admin/settings/mail.blade.php +++ b/resources/themes/pterodactyl/admin/settings/mail.blade.php @@ -98,7 +98,10 @@
@endif diff --git a/routes/admin.php b/routes/admin.php index 8e5482567..e9d78cdaa 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -64,6 +64,7 @@ Route::group(['prefix' => 'databases'], function () { Route::group(['prefix' => 'settings'], function () { Route::get('/', 'Settings\IndexController@index')->name('admin.settings'); Route::get('/mail', 'Settings\MailController@index')->name('admin.settings.mail'); + Route::get('/mail/test', 'Settings\MailController@test')->name('admin.settings.mail.test'); Route::get('/advanced', 'Settings\AdvancedController@index')->name('admin.settings.advanced'); Route::patch('/', 'Settings\IndexController@update');