1
0

MailSettingsController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  47. * @param string $appName
  48. * @param IRequest $request
  49. * @param IL10N $l10n
  50. * @param IConfig $config
  51. * @param IUserSession $userSession
  52. * @param IMailer $mailer
  53. */
  54. public function __construct($appName,
  55. IRequest $request,
  56. IL10N $l10n,
  57. IConfig $config,
  58. IUserSession $userSession,
  59. IMailer $mailer) {
  60. parent::__construct($appName, $request);
  61. $this->l10n = $l10n;
  62. $this->config = $config;
  63. $this->userSession = $userSession;
  64. $this->mailer = $mailer;
  65. }
  66. /**
  67. * Sets the email settings
  68. *
  69. * @PasswordConfirmationRequired
  70. *
  71. * @param string $mail_domain
  72. * @param string $mail_from_address
  73. * @param string $mail_smtpmode
  74. * @param string $mail_smtpsecure
  75. * @param string $mail_smtphost
  76. * @param string $mail_smtpauthtype
  77. * @param int $mail_smtpauth
  78. * @param string $mail_smtpport
  79. * @return DataResponse
  80. */
  81. public function setMailSettings($mail_domain,
  82. $mail_from_address,
  83. $mail_smtpmode,
  84. $mail_smtpsecure,
  85. $mail_smtphost,
  86. $mail_smtpauthtype,
  87. $mail_smtpauth,
  88. $mail_smtpport) {
  89. $params = get_defined_vars();
  90. $configs = [];
  91. foreach($params as $key => $value) {
  92. $configs[$key] = empty($value) ? null : $value;
  93. }
  94. // Delete passwords from config in case no auth is specified
  95. if ($params['mail_smtpauth'] !== 1) {
  96. $configs['mail_smtpname'] = null;
  97. $configs['mail_smtppassword'] = null;
  98. }
  99. $this->config->setSystemValues($configs);
  100. return new DataResponse();
  101. }
  102. /**
  103. * Store the credentials used for SMTP in the config
  104. *
  105. * @PasswordConfirmationRequired
  106. *
  107. * @param string $mail_smtpname
  108. * @param string $mail_smtppassword
  109. * @return DataResponse
  110. */
  111. public function storeCredentials($mail_smtpname, $mail_smtppassword) {
  112. if ($mail_smtppassword === '********') {
  113. return new DataResponse($this->l10n->t('Invalid SMTP password.'), Http::STATUS_BAD_REQUEST);
  114. }
  115. $this->config->setSystemValues([
  116. 'mail_smtpname' => $mail_smtpname,
  117. 'mail_smtppassword' => $mail_smtppassword,
  118. ]);
  119. return new DataResponse();
  120. }
  121. /**
  122. * Send a mail to test the settings
  123. * @return DataResponse
  124. */
  125. public function sendTestMail() {
  126. $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', '');
  127. if (!empty($email)) {
  128. try {
  129. $displayName = $this->userSession->getUser()->getDisplayName();
  130. $template = $this->mailer->createEMailTemplate('settings.TestEmail', [
  131. 'displayname' => $displayName,
  132. ]);
  133. $template->setSubject($this->l10n->t('Email setting test'));
  134. $template->addHeader();
  135. $template->addHeading($this->l10n->t('Well done, %s!', [$displayName]));
  136. $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.'));
  137. $template->addFooter();
  138. $message = $this->mailer->createMessage();
  139. $message->setTo([$email => $displayName]);
  140. $message->useTemplate($template);
  141. $errors = $this->mailer->send($message);
  142. if (!empty($errors)) {
  143. throw new \RuntimeException($this->l10n->t('Email could not be sent. Check your mail server log'));
  144. }
  145. return new DataResponse();
  146. } catch (\Exception $e) {
  147. 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);
  148. }
  149. }
  150. return new DataResponse($this->l10n->t('You need to set your user email before being able to send test emails.'), Http::STATUS_BAD_REQUEST);
  151. }
  152. }