Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Admin/Settings/MailController.php
10284 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Admin\Settings;
4
5
use Illuminate\View\View;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Illuminate\Contracts\Console\Kernel;
9
use Pterodactyl\Notifications\MailTested;
10
use Illuminate\Support\Facades\Notification;
11
use Pterodactyl\Exceptions\DisplayException;
12
use Pterodactyl\Http\Controllers\Controller;
13
use Illuminate\Contracts\Encryption\Encrypter;
14
use Pterodactyl\Providers\SettingsServiceProvider;
15
use Illuminate\Contracts\Config\Repository as ConfigRepository;
16
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
17
use Pterodactyl\Http\Requests\Admin\Settings\MailSettingsFormRequest;
18
19
class MailController extends Controller
20
{
21
/**
22
* MailController constructor.
23
*/
24
public function __construct(
25
private ConfigRepository $config,
26
private Encrypter $encrypter,
27
private Kernel $kernel,
28
private SettingsRepositoryInterface $settings,
29
) {
30
}
31
32
/**
33
* Render UI for editing mail settings. This UI should only display if
34
* the server is configured to send mail using SMTP.
35
*/
36
public function index(): View
37
{
38
return view('admin.settings.mail', [
39
'disabled' => $this->config->get('mail.default') !== 'smtp',
40
]);
41
}
42
43
/**
44
* Handle request to update SMTP mail settings.
45
*
46
* @throws DisplayException
47
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
48
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
49
*/
50
public function update(MailSettingsFormRequest $request): Response
51
{
52
if ($this->config->get('mail.default') !== 'smtp') {
53
throw new DisplayException('This feature is only available if SMTP is the selected email driver for the Panel.');
54
}
55
56
$values = $request->normalize();
57
if (array_get($values, 'mail:mailers:smtp:password') === '!e') {
58
$values['mail:mailers:smtp:password'] = '';
59
}
60
61
foreach ($values as $key => $value) {
62
if (in_array($key, SettingsServiceProvider::getEncryptedKeys()) && !empty($value)) {
63
$value = $this->encrypter->encrypt($value);
64
}
65
66
$this->settings->set('settings::' . $key, $value);
67
}
68
69
$this->kernel->call('queue:restart');
70
71
return response('', 204);
72
}
73
74
/**
75
* Submit a request to send a test mail message.
76
*/
77
public function test(Request $request): Response
78
{
79
try {
80
Notification::route('mail', $request->user()->email)
81
->notify(new MailTested($request->user()));
82
} catch (\Exception $exception) {
83
return response($exception->getMessage(), 500);
84
}
85
86
return response('', 204);
87
}
88
}
89
90