From c49a55eb08e92438cec0ad0649f5cfea9436d764 Mon Sep 17 00:00:00 2001 From: szaimen Date: Thu, 3 Feb 2022 11:43:17 +0100 Subject: [PATCH] show if the mail server settings are not set or verified Signed-off-by: szaimen --- .../lib/Controller/CheckSetupController.php | 15 +++++++++++++++ .../lib/Controller/MailSettingsController.php | 17 ++++++++++++++++- .../settings/admin/additional-mail.php | 2 +- core/js/setupchecks.js | 8 ++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/apps/settings/lib/Controller/CheckSetupController.php b/apps/settings/lib/Controller/CheckSetupController.php index 3c7d5a5c0ab..11900fad45b 100644 --- a/apps/settings/lib/Controller/CheckSetupController.php +++ b/apps/settings/lib/Controller/CheckSetupController.php @@ -572,6 +572,20 @@ Raw output return \OC_Helper::isReadOnlyConfigEnabled(); } + protected function wasEmailTestSuccessful(): bool { + // Handle the case that the configuration was set before the check was introduced or it was only set via command line and not from the UI + if ($this->config->getAppValue('core', 'emailTestSuccessful', '') === '' && $this->config->getSystemValue('mail_domain', '') === '') { + return false; + } + + // The mail test was unsuccessful or the config was changed using the UI without verifying with a testmail, hence return false + if ($this->config->getAppValue('core', 'emailTestSuccessful', '') === '0') { + return false; + } + + return true; + } + protected function hasValidTransactionIsolationLevel(): bool { try { if ($this->db->getDatabasePlatform() instanceof SqlitePlatform) { @@ -822,6 +836,7 @@ Raw output 'isGetenvServerWorking' => !empty(getenv('PATH')), 'isReadOnlyConfig' => $this->isReadOnlyConfig(), 'hasValidTransactionIsolationLevel' => $this->hasValidTransactionIsolationLevel(), + 'wasEmailTestSuccessful' => $this->wasEmailTestSuccessful(), 'hasFileinfoInstalled' => $this->hasFileinfoInstalled(), 'hasWorkingFileLocking' => $this->hasWorkingFileLocking(), 'suggestedOverwriteCliURL' => $this->getSuggestedOverwriteCliURL(), diff --git a/apps/settings/lib/Controller/MailSettingsController.php b/apps/settings/lib/Controller/MailSettingsController.php index 04143cc7fa7..22c0622a072 100644 --- a/apps/settings/lib/Controller/MailSettingsController.php +++ b/apps/settings/lib/Controller/MailSettingsController.php @@ -33,6 +33,7 @@ use OCP\AppFramework\Http\DataResponse; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; +use OCP\IURLGenerator; use OCP\IUserSession; use OCP\Mail\IMailer; @@ -46,6 +47,8 @@ class MailSettingsController extends Controller { private $userSession; /** @var IMailer */ private $mailer; + /** @var IURLGenerator */ + private $urlGenerator; /** * @param string $appName @@ -53,6 +56,7 @@ class MailSettingsController extends Controller { * @param IL10N $l10n * @param IConfig $config * @param IUserSession $userSession + * @param IURLGenerator $urlGenerator, * @param IMailer $mailer */ public function __construct($appName, @@ -60,11 +64,13 @@ class MailSettingsController extends Controller { IL10N $l10n, IConfig $config, IUserSession $userSession, + IURLGenerator $urlGenerator, IMailer $mailer) { parent::__construct($appName, $request); $this->l10n = $l10n; $this->config = $config; $this->userSession = $userSession; + $this->urlGenerator = $urlGenerator; $this->mailer = $mailer; } @@ -107,6 +113,8 @@ class MailSettingsController extends Controller { $this->config->setSystemValues($configs); + $this->config->setAppValue('core', 'emailTestSuccessful', '0'); + return new DataResponse(); } @@ -130,6 +138,8 @@ class MailSettingsController extends Controller { 'mail_smtppassword' => $mail_smtppassword, ]); + $this->config->setAppValue('core', 'emailTestSuccessful', '0'); + return new DataResponse(); } @@ -159,14 +169,19 @@ class MailSettingsController extends Controller { $message->useTemplate($template); $errors = $this->mailer->send($message); if (!empty($errors)) { + $this->config->setAppValue('core', 'emailTestSuccessful', '0'); throw new \RuntimeException($this->l10n->t('Email could not be sent. Check your mail server log')); } + // Store the successful config in the app config + $this->config->setAppValue('core', 'emailTestSuccessful', '1'); return new DataResponse(); } catch (\Exception $e) { + $this->config->setAppValue('core', 'emailTestSuccessful', '0'); return new DataResponse($this->l10n->t('A problem occurred while sending the email. Please revise your settings. (Error: %s)', [$e->getMessage()]), Http::STATUS_BAD_REQUEST); } } - return new DataResponse($this->l10n->t('You need to set your user email before being able to send test emails.'), Http::STATUS_BAD_REQUEST); + $this->config->setAppValue('core', 'emailTestSuccessful', '0'); + return new DataResponse($this->l10n->t('You need to set your user email before being able to send test emails. Go to %s for that.', [$this->urlGenerator->linkToRouteAbsolute('settings.PersonalSettings.index')]), Http::STATUS_BAD_REQUEST); } } diff --git a/apps/settings/templates/settings/admin/additional-mail.php b/apps/settings/templates/settings/admin/additional-mail.php index 82cd9e09b13..a7e8382de18 100644 --- a/apps/settings/templates/settings/admin/additional-mail.php +++ b/apps/settings/templates/settings/admin/additional-mail.php @@ -158,7 +158,7 @@ $mail_sendmailmode = [
- t('Test email settings')); ?> + t('Test and verify email settings')); ?> diff --git a/core/js/setupchecks.js b/core/js/setupchecks.js index 8e6f17f07ed..5976f3b701c 100644 --- a/core/js/setupchecks.js +++ b/core/js/setupchecks.js @@ -195,6 +195,14 @@ type: OC.SetupChecks.MESSAGE_TYPE_INFO }); } + if (!data.wasEmailTestSuccessful) { + messages.push({ + msg: t('core', 'You have not set or verified your email server configuration, yet. Please head over to the {mailSettingsStart} Basic settings {mailSettingsEnd} in order to set them. Afterwards, use the "Send email" button below the form to verify your settings.',) + .replace('{mailSettingsStart} ', '') + .replace(' {mailSettingsEnd}', ''), + type: OC.SetupChecks.MESSAGE_TYPE_INFO + }); + } if (!data.hasValidTransactionIsolationLevel) { messages.push({ msg: t('core', 'Your database does not run with "READ COMMITTED" transaction isolation level. This can cause problems when multiple actions are executed in parallel.'), -- 2.39.5