diff --git a/js/src/admin/components/MailPage.js b/js/src/admin/components/MailPage.js index 127834309d..afbebc2d7a 100644 --- a/js/src/admin/components/MailPage.js +++ b/js/src/admin/components/MailPage.js @@ -11,6 +11,7 @@ export default class MailPage extends Page { super.init(); this.saving = false; + this.sendingTest = false; this.refresh(); } @@ -28,7 +29,7 @@ export default class MailPage extends Page { app .request({ method: 'GET', - url: app.forum.attribute('apiUrl') + '/mail-settings', + url: app.forum.attribute('apiUrl') + '/mail/settings', }) .then((response) => { this.driverFields = response['data']['attributes']['fields']; @@ -121,11 +122,27 @@ export default class MailPage extends Page { ], })} - {Button.component({ - type: 'submit', - className: 'Button Button--primary', - children: app.translator.trans('core.admin.email.submit_button'), - disabled: !this.changed(), +
+ {Button.component({ + type: 'submit', + className: 'Button Button--primary', + children: app.translator.trans('core.admin.email.submit_button'), + disabled: !this.changed(), + })} +
+ + {FieldSet.component({ + label: app.translator.trans('core.admin.email.send_test_mail_heading'), + className: 'MailPage-MailSettings', + children: [ +
{app.translator.trans('core.admin.email.send_test_mail_text', { email: app.session.user.email() })}
, + Button.component({ + className: 'Button Button--primary', + children: app.translator.trans('core.admin.email.send_test_mail_button'), + disabled: this.sendingTest || this.changed(), + onclick: () => this.sendTestEmail(), + }), + ], })} @@ -149,10 +166,34 @@ export default class MailPage extends Page { return this.fields.some((key) => this.values[key]() !== app.data.settings[key]); } + sendTestEmail() { + if (this.saving || this.sendingTest) return; + + this.sendingTest = true; + app.alerts.dismiss(this.testEmailSuccessAlert); + + app + .request({ + method: 'POST', + url: app.forum.attribute('apiUrl') + '/mail/test', + }) + .then((response) => { + this.sendingTest = false; + app.alerts.show( + (this.testEmailSuccessAlert = new Alert({ type: 'success', children: app.translator.trans('core.admin.email.send_test_mail_success') })) + ); + }) + .catch((error) => { + this.sendingTest = false; + m.redraw(); + throw error; + }); + } + onsubmit(e) { e.preventDefault(); - if (this.saving) return; + if (this.saving || this.sendingTest) return; this.saving = true; app.alerts.dismiss(this.successAlert); diff --git a/src/Api/Controller/SendTestMailController.php b/src/Api/Controller/SendTestMailController.php new file mode 100644 index 0000000000..b3b15e929b --- /dev/null +++ b/src/Api/Controller/SendTestMailController.php @@ -0,0 +1,53 @@ +container = $container; + $this->mailer = $mailer; + $this->translator = $translator; + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + $actor = $request->getAttribute('actor'); + $this->assertAdmin($actor); + + $body = $this->translator->trans('core.email.send_test.body', ['{username}' => $actor->username]); + + $this->mailer->raw($body, function (Message $message) use ($actor) { + $message->to($actor->email); + $message->subject($this->translator->trans('core.email.send_test.subject')); + }); + + return new EmptyResponse(); + } +} diff --git a/src/Api/routes.php b/src/Api/routes.php index ea591427f0..778da8bfdd 100644 --- a/src/Api/routes.php +++ b/src/Api/routes.php @@ -309,8 +309,15 @@ // List available mail drivers, available fields and validation status $map->get( - '/mail-settings', + '/mail/settings', 'mailSettings.index', $route->toController(Controller\ShowMailSettingsController::class) ); + + // Send test mail post + $map->post( + '/mail/test', + 'mailTest', + $route->toController(Controller\SendTestMailController::class) + ); }; diff --git a/tests/integration/extenders/MailTest.php b/tests/integration/extenders/MailTest.php index 84bdef6f4e..42e843853e 100644 --- a/tests/integration/extenders/MailTest.php +++ b/tests/integration/extenders/MailTest.php @@ -40,7 +40,7 @@ public function drivers_are_unchanged_by_default() $this->prepDb(); $response = $this->send( - $this->request('GET', '/api/mail-settings', [ + $this->request('GET', '/api/mail/settings', [ 'authenticatedAs' => 1, ]) ); @@ -73,7 +73,7 @@ public function added_driver_appears_in_mail_settings() $this->prepDb(); $response = $this->send( - $this->request('GET', '/api/mail-settings', [ + $this->request('GET', '/api/mail/settings', [ 'authenticatedAs' => 1, ]) ); @@ -97,7 +97,7 @@ public function adding_driver_with_duplicate_name_overrides_fields() $this->prepDb(); $response = $this->send( - $this->request('GET', '/api/mail-settings', [ + $this->request('GET', '/api/mail/settings', [ 'authenticatedAs' => 1, ]) );