You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MailSettingsController.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Settings\Controller;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\IConfig;
  33. use OCP\IL10N;
  34. use OCP\IRequest;
  35. use OCP\IURLGenerator;
  36. use OCP\IUserSession;
  37. use OCP\Mail\IMailer;
  38. class MailSettingsController extends Controller {
  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 IURLGenerator */
  48. private $urlGenerator;
  49. /**
  50. * @param string $appName
  51. * @param IRequest $request
  52. * @param IL10N $l10n
  53. * @param IConfig $config
  54. * @param IUserSession $userSession
  55. * @param IURLGenerator $urlGenerator,
  56. * @param IMailer $mailer
  57. */
  58. public function __construct($appName,
  59. IRequest $request,
  60. IL10N $l10n,
  61. IConfig $config,
  62. IUserSession $userSession,
  63. IURLGenerator $urlGenerator,
  64. IMailer $mailer) {
  65. parent::__construct($appName, $request);
  66. $this->l10n = $l10n;
  67. $this->config = $config;
  68. $this->userSession = $userSession;
  69. $this->urlGenerator = $urlGenerator;
  70. $this->mailer = $mailer;
  71. }
  72. /**
  73. * Sets the email settings
  74. *
  75. * @PasswordConfirmationRequired
  76. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Overview)
  77. *
  78. * @param string $mail_domain
  79. * @param string $mail_from_address
  80. * @param string $mail_smtpmode
  81. * @param string $mail_smtpsecure
  82. * @param string $mail_smtphost
  83. * @param int $mail_smtpauth
  84. * @param string $mail_smtpport
  85. * @return DataResponse
  86. */
  87. public function setMailSettings($mail_domain,
  88. $mail_from_address,
  89. $mail_smtpmode,
  90. $mail_smtpsecure,
  91. $mail_smtphost,
  92. $mail_smtpauth,
  93. $mail_smtpport,
  94. $mail_sendmailmode) {
  95. $params = get_defined_vars();
  96. $configs = [];
  97. foreach ($params as $key => $value) {
  98. $configs[$key] = empty($value) ? null : $value;
  99. }
  100. // Delete passwords from config in case no auth is specified
  101. if ($params['mail_smtpauth'] !== 1) {
  102. $configs['mail_smtpname'] = null;
  103. $configs['mail_smtppassword'] = null;
  104. }
  105. $this->config->setSystemValues($configs);
  106. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  107. return new DataResponse();
  108. }
  109. /**
  110. * Store the credentials used for SMTP in the config
  111. *
  112. * @PasswordConfirmationRequired
  113. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Overview)
  114. *
  115. * @param string $mail_smtpname
  116. * @param string $mail_smtppassword
  117. * @return DataResponse
  118. */
  119. public function storeCredentials($mail_smtpname, $mail_smtppassword) {
  120. if ($mail_smtppassword === '********') {
  121. return new DataResponse($this->l10n->t('Invalid SMTP password.'), Http::STATUS_BAD_REQUEST);
  122. }
  123. $this->config->setSystemValues([
  124. 'mail_smtpname' => $mail_smtpname,
  125. 'mail_smtppassword' => $mail_smtppassword,
  126. ]);
  127. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  128. return new DataResponse();
  129. }
  130. /**
  131. * Send a mail to test the settings
  132. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Overview)
  133. * @return DataResponse
  134. */
  135. public function sendTestMail() {
  136. $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', '');
  137. if (!empty($email)) {
  138. try {
  139. $displayName = $this->userSession->getUser()->getDisplayName();
  140. $template = $this->mailer->createEMailTemplate('settings.TestEmail', [
  141. 'displayname' => $displayName,
  142. ]);
  143. $template->setSubject($this->l10n->t('Email setting test'));
  144. $template->addHeader();
  145. $template->addHeading($this->l10n->t('Well done, %s!', [$displayName]));
  146. $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.'));
  147. $template->addFooter();
  148. $message = $this->mailer->createMessage();
  149. $message->setTo([$email => $displayName]);
  150. $message->useTemplate($template);
  151. $errors = $this->mailer->send($message);
  152. if (!empty($errors)) {
  153. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  154. throw new \RuntimeException($this->l10n->t('Email could not be sent. Check your mail server log'));
  155. }
  156. // Store the successful config in the app config
  157. $this->config->setAppValue('core', 'emailTestSuccessful', '1');
  158. return new DataResponse();
  159. } catch (\Exception $e) {
  160. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  161. 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);
  162. }
  163. }
  164. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  165. 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);
  166. }
  167. }