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