MailSettingsController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 string $mail_smtpauthtype
  84. * @param int $mail_smtpauth
  85. * @param string $mail_smtpport
  86. * @return DataResponse
  87. */
  88. public function setMailSettings($mail_domain,
  89. $mail_from_address,
  90. $mail_smtpmode,
  91. $mail_smtpsecure,
  92. $mail_smtphost,
  93. $mail_smtpauthtype,
  94. $mail_smtpauth,
  95. $mail_smtpport,
  96. $mail_sendmailmode) {
  97. $params = get_defined_vars();
  98. $configs = [];
  99. foreach ($params as $key => $value) {
  100. $configs[$key] = empty($value) ? null : $value;
  101. }
  102. // Delete passwords from config in case no auth is specified
  103. if ($params['mail_smtpauth'] !== 1) {
  104. $configs['mail_smtpname'] = null;
  105. $configs['mail_smtppassword'] = null;
  106. }
  107. $this->config->setSystemValues($configs);
  108. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  109. return new DataResponse();
  110. }
  111. /**
  112. * Store the credentials used for SMTP in the config
  113. *
  114. * @PasswordConfirmationRequired
  115. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Overview)
  116. *
  117. * @param string $mail_smtpname
  118. * @param string $mail_smtppassword
  119. * @return DataResponse
  120. */
  121. public function storeCredentials($mail_smtpname, $mail_smtppassword) {
  122. if ($mail_smtppassword === '********') {
  123. return new DataResponse($this->l10n->t('Invalid SMTP password.'), Http::STATUS_BAD_REQUEST);
  124. }
  125. $this->config->setSystemValues([
  126. 'mail_smtpname' => $mail_smtpname,
  127. 'mail_smtppassword' => $mail_smtppassword,
  128. ]);
  129. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  130. return new DataResponse();
  131. }
  132. /**
  133. * Send a mail to test the settings
  134. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Overview)
  135. * @return DataResponse
  136. */
  137. public function sendTestMail() {
  138. $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', '');
  139. if (!empty($email)) {
  140. try {
  141. $displayName = $this->userSession->getUser()->getDisplayName();
  142. $template = $this->mailer->createEMailTemplate('settings.TestEmail', [
  143. 'displayname' => $displayName,
  144. ]);
  145. $template->setSubject($this->l10n->t('Email setting test'));
  146. $template->addHeader();
  147. $template->addHeading($this->l10n->t('Well done, %s!', [$displayName]));
  148. $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.'));
  149. $template->addFooter();
  150. $message = $this->mailer->createMessage();
  151. $message->setTo([$email => $displayName]);
  152. $message->useTemplate($template);
  153. $errors = $this->mailer->send($message);
  154. if (!empty($errors)) {
  155. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  156. throw new \RuntimeException($this->l10n->t('Email could not be sent. Check your mail server log'));
  157. }
  158. // Store the successful config in the app config
  159. $this->config->setAppValue('core', 'emailTestSuccessful', '1');
  160. return new DataResponse();
  161. } catch (\Exception $e) {
  162. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  163. 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);
  164. }
  165. }
  166. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  167. 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);
  168. }
  169. }