Skip to content

Commit fd3e5fc

Browse files
committed
add SMTP mail tester
1 parent 2d469cc commit fd3e5fc

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

app/Http/Controllers/Admin/Settings/MailController.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
namespace Pterodactyl\Http\Controllers\Admin\Settings;
44

5+
use Exception;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\Log;
8+
use Illuminate\Support\Facades\Notification;
59
use Illuminate\View\View;
610
use Illuminate\Http\RedirectResponse;
711
use Prologue\Alerts\AlertsMessageBag;
812
use Illuminate\Contracts\Console\Kernel;
913
use Pterodactyl\Exceptions\DisplayException;
1014
use Pterodactyl\Http\Controllers\Controller;
1115
use Illuminate\Contracts\Encryption\Encrypter;
16+
use Pterodactyl\Notifications\MailTested;
1217
use Pterodactyl\Providers\SettingsServiceProvider;
1318
use Illuminate\Contracts\Config\Repository as ConfigRepository;
1419
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
@@ -90,7 +95,7 @@ public function index(): View
9095
public function update(MailSettingsFormRequest $request): RedirectResponse
9196
{
9297
if ($this->config->get('mail.driver') !== 'smtp') {
93-
throw new DisplayException('This feature is only available if SMTP is the selected email driver for the Panel.');
98+
throw $this->smtpNotSelectedException();
9499
}
95100

96101
$values = $request->normalize();
@@ -111,4 +116,39 @@ public function update(MailSettingsFormRequest $request): RedirectResponse
111116

112117
return redirect()->route('admin.settings.mail');
113118
}
119+
120+
/**
121+
* Submit a request to send a test mail message.
122+
*
123+
* @throws DisplayException
124+
* @param Request $request
125+
* @return \Illuminate\Http\RedirectResponse
126+
*/
127+
public function test(Request $request): RedirectResponse
128+
{
129+
if ($this->config->get('mail.driver') !== 'smtp') {
130+
throw $this->smtpNotSelectedException();
131+
}
132+
133+
try {
134+
Log::debug('Sending test message to ' . $request->user()->email);
135+
Notification::route('mail', $request->user()->email)
136+
->notify(new MailTested($request->user()));
137+
} catch (Exception $exception) {
138+
$this->alert->danger(trans('base.mail.test_failed'))->flash();
139+
return redirect()->route('admin.settings.mail');
140+
}
141+
142+
$this->alert->success(trans('base.mail.test_succeeded'))->flash();
143+
return redirect()->route('admin.settings.mail');
144+
}
145+
146+
/**
147+
* Generate a display exception for non-SMTP configurations.
148+
*
149+
* @return DisplayException
150+
*/
151+
private function smtpNotSelectedException() {
152+
return new DisplayException('This feature is only available if SMTP is the selected email driver for the Panel.');
153+
}
114154
}

app/Notifications/MailTested.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Pterodactyl\Notifications;
4+
5+
use Illuminate\Notifications\Messages\MailMessage;
6+
use Illuminate\Notifications\Notification;
7+
use Pterodactyl\Models\User;
8+
9+
class MailTested extends Notification
10+
{
11+
/**
12+
* @var \Pterodactyl\Models\User
13+
*/
14+
private $user;
15+
16+
public function __construct(User $user) {
17+
$this->user = $user;
18+
}
19+
20+
public function via()
21+
{
22+
return ['mail'];
23+
}
24+
25+
public function toMail()
26+
{
27+
return (new MailMessage)
28+
->subject('Pterodactyl Test Message')
29+
->greeting('Hello ' . $this->user->name . '!')
30+
->line('This is a test of the Pterodactyl mail system. You\'re good to go!');
31+
}
32+
}

resources/lang/en/base.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,8 @@
8686
'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.',
8787
'2fa_disable_error' => 'The 2FA token provided was not valid. Protection has not been disabled for this account.',
8888
],
89+
'mail' => [
90+
'test_succeeded' => 'The test message was sent successfully!',
91+
'test_failed' => 'Failed to send test message! Check your configuration and try again.',
92+
],
8993
];

resources/themes/pterodactyl/admin/settings/mail.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@
9898
</div>
9999
<div class="box-footer">
100100
{{ csrf_field() }}
101-
<button type="submit" name="_method" value="PATCH" class="btn btn-sm btn-primary pull-right">Save</button>
101+
<div class="pull-right">
102+
<a href="{{ route('admin.settings.mail.test') }}" class="btn btn-sm btn-success">Test</a>
103+
<button type="submit" name="_method" value="PATCH" class="btn btn-sm btn-primary">Save</button>
104+
</div>
102105
</div>
103106
</form>
104107
@endif

routes/admin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
Route::group(['prefix' => 'settings'], function () {
6565
Route::get('/', 'Settings\IndexController@index')->name('admin.settings');
6666
Route::get('/mail', 'Settings\MailController@index')->name('admin.settings.mail');
67+
Route::get('/mail/test', 'Settings\MailController@test')->name('admin.settings.mail.test');
6768
Route::get('/advanced', 'Settings\AdvancedController@index')->name('admin.settings.advanced');
6869

6970
Route::patch('/', 'Settings\IndexController@update');

0 commit comments

Comments
 (0)