mirror of
https://github.com/flarum/framework.git
synced 2024-11-23 13:30:47 +08:00
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:
parent
63242edeb3
commit
d1750fecc0
|
@ -11,6 +11,7 @@ export default class MailPage extends Page {
|
||||||
super.init();
|
super.init();
|
||||||
|
|
||||||
this.saving = false;
|
this.saving = false;
|
||||||
|
this.sendingTest = false;
|
||||||
this.refresh();
|
this.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +29,7 @@ export default class MailPage extends Page {
|
||||||
app
|
app
|
||||||
.request({
|
.request({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: app.forum.attribute('apiUrl') + '/mail-settings',
|
url: app.forum.attribute('apiUrl') + '/mail/settings',
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
this.driverFields = response['data']['attributes']['fields'];
|
this.driverFields = response['data']['attributes']['fields'];
|
||||||
|
@ -121,11 +122,27 @@ export default class MailPage extends Page {
|
||||||
],
|
],
|
||||||
})}
|
})}
|
||||||
|
|
||||||
{Button.component({
|
<FieldSet>
|
||||||
type: 'submit',
|
{Button.component({
|
||||||
className: 'Button Button--primary',
|
type: 'submit',
|
||||||
children: app.translator.trans('core.admin.email.submit_button'),
|
className: 'Button Button--primary',
|
||||||
disabled: !this.changed(),
|
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>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
@ -149,10 +166,34 @@ export default class MailPage extends Page {
|
||||||
return this.fields.some((key) => this.values[key]() !== app.data.settings[key]);
|
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) {
|
onsubmit(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
if (this.saving) return;
|
if (this.saving || this.sendingTest) return;
|
||||||
|
|
||||||
this.saving = true;
|
this.saving = true;
|
||||||
app.alerts.dismiss(this.successAlert);
|
app.alerts.dismiss(this.successAlert);
|
||||||
|
|
53
src/Api/Controller/SendTestMailController.php
Normal file
53
src/Api/Controller/SendTestMailController.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -309,8 +309,15 @@ return function (RouteCollection $map, RouteHandlerFactory $route) {
|
||||||
|
|
||||||
// List available mail drivers, available fields and validation status
|
// List available mail drivers, available fields and validation status
|
||||||
$map->get(
|
$map->get(
|
||||||
'/mail-settings',
|
'/mail/settings',
|
||||||
'mailSettings.index',
|
'mailSettings.index',
|
||||||
$route->toController(Controller\ShowMailSettingsController::class)
|
$route->toController(Controller\ShowMailSettingsController::class)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Send test mail post
|
||||||
|
$map->post(
|
||||||
|
'/mail/test',
|
||||||
|
'mailTest',
|
||||||
|
$route->toController(Controller\SendTestMailController::class)
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -40,7 +40,7 @@ class MailTest extends TestCase
|
||||||
$this->prepDb();
|
$this->prepDb();
|
||||||
|
|
||||||
$response = $this->send(
|
$response = $this->send(
|
||||||
$this->request('GET', '/api/mail-settings', [
|
$this->request('GET', '/api/mail/settings', [
|
||||||
'authenticatedAs' => 1,
|
'authenticatedAs' => 1,
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
@ -73,7 +73,7 @@ class MailTest extends TestCase
|
||||||
$this->prepDb();
|
$this->prepDb();
|
||||||
|
|
||||||
$response = $this->send(
|
$response = $this->send(
|
||||||
$this->request('GET', '/api/mail-settings', [
|
$this->request('GET', '/api/mail/settings', [
|
||||||
'authenticatedAs' => 1,
|
'authenticatedAs' => 1,
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
@ -97,7 +97,7 @@ class MailTest extends TestCase
|
||||||
$this->prepDb();
|
$this->prepDb();
|
||||||
|
|
||||||
$response = $this->send(
|
$response = $this->send(
|
||||||
$this->request('GET', '/api/mail-settings', [
|
$this->request('GET', '/api/mail/settings', [
|
||||||
'authenticatedAs' => 1,
|
'authenticatedAs' => 1,
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user