1
0

MailSettingsController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. $mail_sendmailmode) {
  90. $params = get_defined_vars();
  91. $configs = [];
  92. foreach($params as $key => $value) {
  93. $configs[$key] = empty($value) ? null : $value;
  94. }
  95. // Delete passwords from config in case no auth is specified
  96. if ($params['mail_smtpauth'] !== 1) {
  97. $configs['mail_smtpname'] = null;
  98. $configs['mail_smtppassword'] = null;
  99. }
  100. $this->config->setSystemValues($configs);
  101. return new DataResponse();
  102. }
  103. /**
  104. * Store the credentials used for SMTP in the config
  105. *
  106. * @PasswordConfirmationRequired
  107. *
  108. * @param string $mail_smtpname
  109. * @param string $mail_smtppassword
  110. * @return DataResponse
  111. */
  112. public function storeCredentials($mail_smtpname, $mail_smtppassword) {
  113. if ($mail_smtppassword === '********') {
  114. return new DataResponse($this->l10n->t('Invalid SMTP password.'), Http::STATUS_BAD_REQUEST);
  115. }
  116. $this->config->setSystemValues([
  117. 'mail_smtpname' => $mail_smtpname,
  118. 'mail_smtppassword' => $mail_smtppassword,
  119. ]);
  120. return new DataResponse();
  121. }
  122. /**
  123. * Send a mail to test the settings
  124. * @return DataResponse
  125. */
  126. public function sendTestMail() {
  127. $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', '');
  128. if (!empty($email)) {
  129. try {
  130. $displayName = $this->userSession->getUser()->getDisplayName();
  131. $template = $this->mailer->createEMailTemplate('settings.TestEmail', [
  132. 'displayname' => $displayName,
  133. ]);
  134. $template->setSubject($this->l10n->t('Email setting test'));
  135. $template->addHeader();
  136. $template->addHeading($this->l10n->t('Well done, %s!', [$displayName]));
  137. $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.'));
  138. $template->addFooter();
  139. $message = $this->mailer->createMessage();
  140. $message->setTo([$email => $displayName]);
  141. $message->useTemplate($template);
  142. $errors = $this->mailer->send($message);
  143. if (!empty($errors)) {
  144. throw new \RuntimeException($this->l10n->t('Email could not be sent. Check your mail server log'));
  145. }
  146. return new DataResponse();
  147. } catch (\Exception $e) {
  148. 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);
  149. }
  150. }
  151. return new DataResponse($this->l10n->t('You need to set your user email before being able to send test emails.'), Http::STATUS_BAD_REQUEST);
  152. }
  153. }