add SMTP mail tester

This commit is contained in:
ayan4m1 2018-09-15 19:43:22 -04:00
parent 2d469cc951
commit fd3e5fc73e
5 changed files with 82 additions and 2 deletions

View File

@ -2,6 +2,10 @@
namespace Pterodactyl\Http\Controllers\Admin\Settings; 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\View\View;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
@ -9,6 +13,7 @@ use Illuminate\Contracts\Console\Kernel;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Notifications\MailTested;
use Pterodactyl\Providers\SettingsServiceProvider; use Pterodactyl\Providers\SettingsServiceProvider;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
@ -90,7 +95,7 @@ class MailController extends Controller
public function update(MailSettingsFormRequest $request): RedirectResponse public function update(MailSettingsFormRequest $request): RedirectResponse
{ {
if ($this->config->get('mail.driver') !== 'smtp') { 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(); $values = $request->normalize();
@ -111,4 +116,39 @@ class MailController extends Controller
return redirect()->route('admin.settings.mail'); 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.');
}
} }

View File

@ -0,0 +1,32 @@
<?php
namespace Pterodactyl\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Pterodactyl\Models\User;
class MailTested extends Notification
{
/**
* @var \Pterodactyl\Models\User
*/
private $user;
public function __construct(User $user) {
$this->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!');
}
}

View File

@ -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_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.', '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.',
],
]; ];

View File

@ -98,7 +98,10 @@
</div> </div>
<div class="box-footer"> <div class="box-footer">
{{ csrf_field() }} {{ csrf_field() }}
<button type="submit" name="_method" value="PATCH" class="btn btn-sm btn-primary pull-right">Save</button> <div class="pull-right">
<a href="{{ route('admin.settings.mail.test') }}" class="btn btn-sm btn-success">Test</a>
<button type="submit" name="_method" value="PATCH" class="btn btn-sm btn-primary">Save</button>
</div>
</div> </div>
</form> </form>
@endif @endif

View File

@ -64,6 +64,7 @@ Route::group(['prefix' => 'databases'], function () {
Route::group(['prefix' => 'settings'], function () { Route::group(['prefix' => 'settings'], function () {
Route::get('/', 'Settings\IndexController@index')->name('admin.settings'); Route::get('/', 'Settings\IndexController@index')->name('admin.settings');
Route::get('/mail', 'Settings\MailController@index')->name('admin.settings.mail'); 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::get('/advanced', 'Settings\AdvancedController@index')->name('admin.settings.advanced');
Route::patch('/', 'Settings\IndexController@update'); Route::patch('/', 'Settings\IndexController@update');