Skip to content

Commit

Permalink
Set send test mail controller to use saved settings instead of testin…
Browse files Browse the repository at this point in the history
…g new ones
  • Loading branch information
askvortsov1 committed Mar 6, 2020
1 parent 96f4d63 commit c662438
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 118 deletions.
21 changes: 12 additions & 9 deletions js/src/admin/components/MailPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ export default class MailPage extends Page {
]
})}

{FieldSet.component({
children: [
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',
Expand All @@ -127,19 +138,11 @@ export default class MailPage extends Page {
type: 'button',
className: 'Button Button--primary',
children: app.translator.trans('core.admin.email.send_test_mail_button'),
disabled: this.sendingTest(),
disabled: this.sendingTest() || this.changed(),
onclick: () => this.sendTestEmail()
})
]
})}


{Button.component({
type: 'submit',
className: 'Button Button--primary',
children: app.translator.trans('core.admin.email.submit_button'),
disabled: !this.changed()
})}
</form>
</div>
</div>
Expand Down
74 changes: 13 additions & 61 deletions src/Api/Controller/SendTestMailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@

namespace Flarum\Api\Controller;

use Flarum\Settings\TemporarySettingsRepository;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Container\Container;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\JsonResponse;
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 Swift_Mailer;
use Symfony\Component\Translation\TranslatorInterface;

class SendTestMailController implements RequestHandlerInterface
Expand All @@ -27,11 +25,14 @@ class SendTestMailController implements RequestHandlerInterface

protected $container;

protected $mailer;

protected $translator;

public function __construct(Container $container, TranslatorInterface $translator)
public function __construct(Container $container, TranslatorInterface $translator, Mailer $mailer)
{
$this->container = $container;
$this->mailer = $mailer;
$this->translator = $translator;
}

Expand All @@ -40,62 +41,13 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$actor = $request->getAttribute('actor');
$this->assertAdmin($actor);

$settings = $request->getParsedBody();

$requiredSettings = ['mail_driver', 'mail_from'];

foreach ($requiredSettings as $setting) {
if (! array_key_exists($setting, $settings)) {
return $this->response([$this->translator->trans('core.email.send_test.missing_setting', ['setting' => $setting])], 400);
}
}

$drivers = $this->container->make('mail.supported_drivers');
$driverKey = Arr::get($settings, 'mail_driver');

if (empty($drivers[$driverKey])) {
return $this->response([$this->translator->trans('core.email.send_test.unsupported_driver', ['driver' => $driverKey])], 400);
}

$driver = $this->container->make($drivers[$driverKey]);

$settingsRepository = new TemporarySettingsRepository();

foreach ($driver->availableSettings() as $setting => $default) {
if (array_key_exists($setting, $settings)) {
$settingsRepository->set($setting, Arr::get($settings, $setting));
} else {
return $this->response([$this->translator->trans('core.email.send_test.missing_setting', ['setting' => $setting])], 400);
}
}

$validator = $this->container->make(Factory::class);
$body = $this->translator->trans('core.email.send_test.body', ['{username}' => $actor->username]);

$errors = $driver->validate($settingsRepository, $validator);
$this->mailer->raw($body, function (Message $message) use ($actor) {
$message->to($actor->email);
$message->subject($this->translator->trans('core.email.send_test.subject'));
});

if (empty($errors)) {
return $this->response($errors, 400);
}

$mailer = new Swift_Mailer($driver->buildTransport($settingsRepository));

$message = $mailer->createMessage();

$message->setSubject($this->translator->trans('core.email.send_test.subject'));
$message->setSender(Arr::get($settings, 'mail_from'));
$message->setFrom(Arr::get($settings, 'mail_from'));
$message->setTo($actor->email);
$message->setBody($this->translator->trans('core.email.send_test.body', ['{username}' => $actor->username]));

$mailer->send($message);

return $this->response([]);
}

private function response($message, $status = 200)
{
return new JsonResponse([
'message' => $message
], $status);
return new EmptyResponse();
}
}
48 changes: 0 additions & 48 deletions src/Settings/TemporarySettingsRepository.php

This file was deleted.

0 comments on commit c662438

Please sign in to comment.