Skip to content

Commit

Permalink
Send Test Mail Feature (#2023)
Browse files Browse the repository at this point in the history
- Add UI, backend for sending test emails
- Change mail settings endpoint to /api/mail/settings
  • Loading branch information
askvortsov1 authored May 31, 2020
1 parent 63242ed commit d1750fe
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 11 deletions.
55 changes: 48 additions & 7 deletions js/src/admin/components/MailPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class MailPage extends Page {
super.init();

this.saving = false;
this.sendingTest = false;
this.refresh();
}

Expand All @@ -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'];
Expand Down Expand Up @@ -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>
Expand All @@ -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);
Expand Down
53 changes: 53 additions & 0 deletions src/Api/Controller/SendTestMailController.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
9 changes: 8 additions & 1 deletion src/Api/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
};
6 changes: 3 additions & 3 deletions tests/integration/extenders/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
])
);
Expand Down Expand Up @@ -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,
])
);
Expand All @@ -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,
])
);
Expand Down

0 comments on commit d1750fe

Please sign in to comment.