Send Test Mail Feature (#2023)

- Add UI, backend for sending test emails
- Change mail settings endpoint to /api/mail/settings
This commit is contained in:
Alexander Skvortsov 2020-05-30 22:49:36 -04:00 committed by GitHub
parent 63242edeb3
commit d1750fecc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 112 additions and 11 deletions

View File

@ -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(),
<FieldSet>
{Button.component({
type: 'submit',
className: 'Button Button--primary',
children: app.translator.trans('core.admin.email.submit_button'),
disabled: !this.changed(),
})}
</FieldSet>
{FieldSet.component({
label: app.translator.trans('core.admin.email.send_test_mail_heading'),
className: 'MailPage-MailSettings',
children: [
<div className="helpText">{app.translator.trans('core.admin.email.send_test_mail_text', { email: app.session.user.email() })}</div>,
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(),
}),
],
})}
</form>
</div>
@ -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);

View File

@ -0,0 +1,53 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Controller;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Container\Container;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Message;
use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Translation\TranslatorInterface;
class SendTestMailController implements RequestHandlerInterface
{
use AssertPermissionTrait;
protected $container;
protected $mailer;
protected $translator;
public function __construct(Container $container, Mailer $mailer, TranslatorInterface $translator)
{
$this->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();
}
}

View File

@ -309,8 +309,15 @@ return function (RouteCollection $map, RouteHandlerFactory $route) {
// 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)
);
};

View File

@ -40,7 +40,7 @@ class MailTest extends TestCase
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/mail-settings', [
$this->request('GET', '/api/mail/settings', [
'authenticatedAs' => 1,
])
);
@ -73,7 +73,7 @@ class MailTest extends TestCase
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/mail-settings', [
$this->request('GET', '/api/mail/settings', [
'authenticatedAs' => 1,
])
);
@ -97,7 +97,7 @@ class MailTest extends TestCase
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/mail-settings', [
$this->request('GET', '/api/mail/settings', [
'authenticatedAs' => 1,
])
);