MailSettingsController.php 5.1 KB

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