@@ -39,136 +39,136 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class MailSettingsController extends Controller { |
| 41 | 41 | |
| 42 | - /** @var IL10N */ |
|
| 43 | - private $l10n; |
|
| 44 | - /** @var IConfig */ |
|
| 45 | - private $config; |
|
| 46 | - /** @var IUserSession */ |
|
| 47 | - private $userSession; |
|
| 48 | - /** @var IMailer */ |
|
| 49 | - private $mailer; |
|
| 50 | - /** @var string */ |
|
| 51 | - private $defaultMailAddress; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param string $appName |
|
| 55 | - * @param IRequest $request |
|
| 56 | - * @param IL10N $l10n |
|
| 57 | - * @param IConfig $config |
|
| 58 | - * @param IUserSession $userSession |
|
| 59 | - * @param IMailer $mailer |
|
| 60 | - * @param string $fromMailAddress |
|
| 61 | - */ |
|
| 62 | - public function __construct($appName, |
|
| 63 | - IRequest $request, |
|
| 64 | - IL10N $l10n, |
|
| 65 | - IConfig $config, |
|
| 66 | - IUserSession $userSession, |
|
| 67 | - IMailer $mailer, |
|
| 68 | - $fromMailAddress) { |
|
| 69 | - parent::__construct($appName, $request); |
|
| 70 | - $this->l10n = $l10n; |
|
| 71 | - $this->config = $config; |
|
| 72 | - $this->userSession = $userSession; |
|
| 73 | - $this->mailer = $mailer; |
|
| 74 | - $this->defaultMailAddress = $fromMailAddress; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Sets the email settings |
|
| 79 | - * |
|
| 80 | - * @PasswordConfirmationRequired |
|
| 81 | - * |
|
| 82 | - * @param string $mail_domain |
|
| 83 | - * @param string $mail_from_address |
|
| 84 | - * @param string $mail_smtpmode |
|
| 85 | - * @param string $mail_smtpsecure |
|
| 86 | - * @param string $mail_smtphost |
|
| 87 | - * @param string $mail_smtpauthtype |
|
| 88 | - * @param int $mail_smtpauth |
|
| 89 | - * @param string $mail_smtpport |
|
| 90 | - * @return DataResponse |
|
| 91 | - */ |
|
| 92 | - public function setMailSettings($mail_domain, |
|
| 93 | - $mail_from_address, |
|
| 94 | - $mail_smtpmode, |
|
| 95 | - $mail_smtpsecure, |
|
| 96 | - $mail_smtphost, |
|
| 97 | - $mail_smtpauthtype, |
|
| 98 | - $mail_smtpauth, |
|
| 99 | - $mail_smtpport) { |
|
| 100 | - |
|
| 101 | - $params = get_defined_vars(); |
|
| 102 | - $configs = []; |
|
| 103 | - foreach($params as $key => $value) { |
|
| 104 | - $configs[$key] = (empty($value)) ? null : $value; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // Delete passwords from config in case no auth is specified |
|
| 108 | - if ($params['mail_smtpauth'] !== 1) { |
|
| 109 | - $configs['mail_smtpname'] = null; |
|
| 110 | - $configs['mail_smtppassword'] = null; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->config->setSystemValues($configs); |
|
| 114 | - |
|
| 115 | - return new DataResponse(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Store the credentials used for SMTP in the config |
|
| 120 | - * |
|
| 121 | - * @PasswordConfirmationRequired |
|
| 122 | - * |
|
| 123 | - * @param string $mail_smtpname |
|
| 124 | - * @param string $mail_smtppassword |
|
| 125 | - * @return DataResponse |
|
| 126 | - */ |
|
| 127 | - public function storeCredentials($mail_smtpname, $mail_smtppassword) { |
|
| 128 | - if ($mail_smtppassword === '********') { |
|
| 129 | - return new DataResponse($this->l10n->t('Invalid SMTP password.'), Http::STATUS_BAD_REQUEST); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - $this->config->setSystemValues([ |
|
| 133 | - 'mail_smtpname' => $mail_smtpname, |
|
| 134 | - 'mail_smtppassword' => $mail_smtppassword, |
|
| 135 | - ]); |
|
| 136 | - |
|
| 137 | - return new DataResponse(); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Send a mail to test the settings |
|
| 142 | - * @return DataResponse |
|
| 143 | - */ |
|
| 144 | - public function sendTestMail() { |
|
| 145 | - $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', ''); |
|
| 146 | - if (!empty($email)) { |
|
| 147 | - try { |
|
| 148 | - $displayName = $this->userSession->getUser()->getDisplayName(); |
|
| 149 | - |
|
| 150 | - $template = $this->mailer->createEMailTemplate(); |
|
| 151 | - $template->addHeader(); |
|
| 152 | - $template->addHeading($this->l10n->t('Well done, %s!', [$displayName])); |
|
| 153 | - $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.')); |
|
| 154 | - $template->addFooter(); |
|
| 155 | - |
|
| 156 | - $message = $this->mailer->createMessage(); |
|
| 157 | - $message->setTo([$email => $displayName]); |
|
| 158 | - $message->setSubject($this->l10n->t('Email setting test')); |
|
| 159 | - $message->setHtmlBody($template->renderHTML()); |
|
| 160 | - $message->setPlainBody($template->renderText()); |
|
| 161 | - $errors = $this->mailer->send($message); |
|
| 162 | - if (!empty($errors)) { |
|
| 163 | - throw new \RuntimeException($this->l10n->t('Mail could not be sent. Check your mail server log')); |
|
| 164 | - } |
|
| 165 | - return new DataResponse(); |
|
| 166 | - } catch (\Exception $e) { |
|
| 167 | - 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); |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return new DataResponse($this->l10n->t('You need to set your user email before being able to send test emails.'), Http::STATUS_BAD_REQUEST); |
|
| 172 | - } |
|
| 42 | + /** @var IL10N */ |
|
| 43 | + private $l10n; |
|
| 44 | + /** @var IConfig */ |
|
| 45 | + private $config; |
|
| 46 | + /** @var IUserSession */ |
|
| 47 | + private $userSession; |
|
| 48 | + /** @var IMailer */ |
|
| 49 | + private $mailer; |
|
| 50 | + /** @var string */ |
|
| 51 | + private $defaultMailAddress; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param string $appName |
|
| 55 | + * @param IRequest $request |
|
| 56 | + * @param IL10N $l10n |
|
| 57 | + * @param IConfig $config |
|
| 58 | + * @param IUserSession $userSession |
|
| 59 | + * @param IMailer $mailer |
|
| 60 | + * @param string $fromMailAddress |
|
| 61 | + */ |
|
| 62 | + public function __construct($appName, |
|
| 63 | + IRequest $request, |
|
| 64 | + IL10N $l10n, |
|
| 65 | + IConfig $config, |
|
| 66 | + IUserSession $userSession, |
|
| 67 | + IMailer $mailer, |
|
| 68 | + $fromMailAddress) { |
|
| 69 | + parent::__construct($appName, $request); |
|
| 70 | + $this->l10n = $l10n; |
|
| 71 | + $this->config = $config; |
|
| 72 | + $this->userSession = $userSession; |
|
| 73 | + $this->mailer = $mailer; |
|
| 74 | + $this->defaultMailAddress = $fromMailAddress; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Sets the email settings |
|
| 79 | + * |
|
| 80 | + * @PasswordConfirmationRequired |
|
| 81 | + * |
|
| 82 | + * @param string $mail_domain |
|
| 83 | + * @param string $mail_from_address |
|
| 84 | + * @param string $mail_smtpmode |
|
| 85 | + * @param string $mail_smtpsecure |
|
| 86 | + * @param string $mail_smtphost |
|
| 87 | + * @param string $mail_smtpauthtype |
|
| 88 | + * @param int $mail_smtpauth |
|
| 89 | + * @param string $mail_smtpport |
|
| 90 | + * @return DataResponse |
|
| 91 | + */ |
|
| 92 | + public function setMailSettings($mail_domain, |
|
| 93 | + $mail_from_address, |
|
| 94 | + $mail_smtpmode, |
|
| 95 | + $mail_smtpsecure, |
|
| 96 | + $mail_smtphost, |
|
| 97 | + $mail_smtpauthtype, |
|
| 98 | + $mail_smtpauth, |
|
| 99 | + $mail_smtpport) { |
|
| 100 | + |
|
| 101 | + $params = get_defined_vars(); |
|
| 102 | + $configs = []; |
|
| 103 | + foreach($params as $key => $value) { |
|
| 104 | + $configs[$key] = (empty($value)) ? null : $value; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // Delete passwords from config in case no auth is specified |
|
| 108 | + if ($params['mail_smtpauth'] !== 1) { |
|
| 109 | + $configs['mail_smtpname'] = null; |
|
| 110 | + $configs['mail_smtppassword'] = null; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->config->setSystemValues($configs); |
|
| 114 | + |
|
| 115 | + return new DataResponse(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Store the credentials used for SMTP in the config |
|
| 120 | + * |
|
| 121 | + * @PasswordConfirmationRequired |
|
| 122 | + * |
|
| 123 | + * @param string $mail_smtpname |
|
| 124 | + * @param string $mail_smtppassword |
|
| 125 | + * @return DataResponse |
|
| 126 | + */ |
|
| 127 | + public function storeCredentials($mail_smtpname, $mail_smtppassword) { |
|
| 128 | + if ($mail_smtppassword === '********') { |
|
| 129 | + return new DataResponse($this->l10n->t('Invalid SMTP password.'), Http::STATUS_BAD_REQUEST); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $this->config->setSystemValues([ |
|
| 133 | + 'mail_smtpname' => $mail_smtpname, |
|
| 134 | + 'mail_smtppassword' => $mail_smtppassword, |
|
| 135 | + ]); |
|
| 136 | + |
|
| 137 | + return new DataResponse(); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Send a mail to test the settings |
|
| 142 | + * @return DataResponse |
|
| 143 | + */ |
|
| 144 | + public function sendTestMail() { |
|
| 145 | + $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', ''); |
|
| 146 | + if (!empty($email)) { |
|
| 147 | + try { |
|
| 148 | + $displayName = $this->userSession->getUser()->getDisplayName(); |
|
| 149 | + |
|
| 150 | + $template = $this->mailer->createEMailTemplate(); |
|
| 151 | + $template->addHeader(); |
|
| 152 | + $template->addHeading($this->l10n->t('Well done, %s!', [$displayName])); |
|
| 153 | + $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.')); |
|
| 154 | + $template->addFooter(); |
|
| 155 | + |
|
| 156 | + $message = $this->mailer->createMessage(); |
|
| 157 | + $message->setTo([$email => $displayName]); |
|
| 158 | + $message->setSubject($this->l10n->t('Email setting test')); |
|
| 159 | + $message->setHtmlBody($template->renderHTML()); |
|
| 160 | + $message->setPlainBody($template->renderText()); |
|
| 161 | + $errors = $this->mailer->send($message); |
|
| 162 | + if (!empty($errors)) { |
|
| 163 | + throw new \RuntimeException($this->l10n->t('Mail could not be sent. Check your mail server log')); |
|
| 164 | + } |
|
| 165 | + return new DataResponse(); |
|
| 166 | + } catch (\Exception $e) { |
|
| 167 | + 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); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return new DataResponse($this->l10n->t('You need to set your user email before being able to send test emails.'), Http::STATUS_BAD_REQUEST); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | 174 | } |