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