. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\FlashMessages; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Services\EmailService; use Fisharebest\Webtrees\Site; use Fisharebest\Webtrees\SiteUser; use Fisharebest\Webtrees\Validator; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function e; use function redirect; use function route; /** * Edit the email preferences. */ class EmailPreferencesAction implements RequestHandlerInterface { private EmailService $email_service; /** * AdminSiteController constructor. * * @param EmailService $email_service */ public function __construct(EmailService $email_service) { $this->email_service = $email_service; } /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $user = Validator::attributes($request)->user(); $params = (array) $request->getParsedBody(); Site::setPreference('SMTP_ACTIVE', $params['SMTP_ACTIVE']); Site::setPreference('SMTP_DISP_NAME', $params['SMTP_DISP_NAME']); Site::setPreference('SMTP_FROM_NAME', $params['SMTP_FROM_NAME']); Site::setPreference('SMTP_HOST', $params['SMTP_HOST']); Site::setPreference('SMTP_PORT', $params['SMTP_PORT']); Site::setPreference('SMTP_AUTH', $params['SMTP_AUTH']); Site::setPreference('SMTP_AUTH_USER', $params['SMTP_AUTH_USER']); Site::setPreference('SMTP_SSL', $params['SMTP_SSL']); Site::setPreference('SMTP_HELO', $params['SMTP_HELO']); Site::setPreference('DKIM_DOMAIN', $params['DKIM_DOMAIN']); Site::setPreference('DKIM_SELECTOR', $params['DKIM_SELECTOR']); Site::setPreference('DKIM_KEY', $params['DKIM_KEY']); if ($params['SMTP_AUTH_PASS'] !== '') { Site::setPreference('SMTP_AUTH_PASS', $params['SMTP_AUTH_PASS']); } FlashMessages::addMessage(I18N::translate('The website preferences have been updated.'), 'success'); $test = $params['test'] ?? ''; if ($test === 'on') { $success = $this->email_service->send(new SiteUser(), $user, $user, 'test', 'test', 'test'); if ($success) { FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($user->email())), 'success'); } else { FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger'); } return redirect(route(EmailPreferencesPage::class)); } return redirect(route(ControlPanel::class)); } }