MailSettingsController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Settings\Controller;
  8. use OCP\AppFramework\Controller;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\IConfig;
  12. use OCP\IL10N;
  13. use OCP\IRequest;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserSession;
  16. use OCP\Mail\IMailer;
  17. class MailSettingsController extends Controller {
  18. /** @var IL10N */
  19. private $l10n;
  20. /** @var IConfig */
  21. private $config;
  22. /** @var IUserSession */
  23. private $userSession;
  24. /** @var IMailer */
  25. private $mailer;
  26. /** @var IURLGenerator */
  27. private $urlGenerator;
  28. /**
  29. * @param string $appName
  30. * @param IRequest $request
  31. * @param IL10N $l10n
  32. * @param IConfig $config
  33. * @param IUserSession $userSession
  34. * @param IURLGenerator $urlGenerator,
  35. * @param IMailer $mailer
  36. */
  37. public function __construct($appName,
  38. IRequest $request,
  39. IL10N $l10n,
  40. IConfig $config,
  41. IUserSession $userSession,
  42. IURLGenerator $urlGenerator,
  43. IMailer $mailer) {
  44. parent::__construct($appName, $request);
  45. $this->l10n = $l10n;
  46. $this->config = $config;
  47. $this->userSession = $userSession;
  48. $this->urlGenerator = $urlGenerator;
  49. $this->mailer = $mailer;
  50. }
  51. /**
  52. * Sets the email settings
  53. *
  54. * @PasswordConfirmationRequired
  55. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Overview)
  56. *
  57. * @param string $mail_domain
  58. * @param string $mail_from_address
  59. * @param string $mail_smtpmode
  60. * @param string $mail_smtpsecure
  61. * @param string $mail_smtphost
  62. * @param int $mail_smtpauth
  63. * @param string $mail_smtpport
  64. * @return DataResponse
  65. */
  66. public function setMailSettings($mail_domain,
  67. $mail_from_address,
  68. $mail_smtpmode,
  69. $mail_smtpsecure,
  70. $mail_smtphost,
  71. $mail_smtpauth,
  72. $mail_smtpport,
  73. $mail_sendmailmode) {
  74. $params = get_defined_vars();
  75. $configs = [];
  76. foreach ($params as $key => $value) {
  77. $configs[$key] = empty($value) ? null : $value;
  78. }
  79. // Delete passwords from config in case no auth is specified
  80. if ($params['mail_smtpauth'] !== 1) {
  81. $configs['mail_smtpname'] = null;
  82. $configs['mail_smtppassword'] = null;
  83. }
  84. $this->config->setSystemValues($configs);
  85. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  86. return new DataResponse();
  87. }
  88. /**
  89. * Store the credentials used for SMTP in the config
  90. *
  91. * @PasswordConfirmationRequired
  92. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Overview)
  93. *
  94. * @param string $mail_smtpname
  95. * @param string $mail_smtppassword
  96. * @return DataResponse
  97. */
  98. public function storeCredentials($mail_smtpname, $mail_smtppassword) {
  99. if ($mail_smtppassword === '********') {
  100. return new DataResponse($this->l10n->t('Invalid SMTP password.'), Http::STATUS_BAD_REQUEST);
  101. }
  102. $this->config->setSystemValues([
  103. 'mail_smtpname' => $mail_smtpname,
  104. 'mail_smtppassword' => $mail_smtppassword,
  105. ]);
  106. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  107. return new DataResponse();
  108. }
  109. /**
  110. * Send a mail to test the settings
  111. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Overview)
  112. * @return DataResponse
  113. */
  114. public function sendTestMail() {
  115. $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', '');
  116. if (!empty($email)) {
  117. try {
  118. $displayName = $this->userSession->getUser()->getDisplayName();
  119. $template = $this->mailer->createEMailTemplate('settings.TestEmail', [
  120. 'displayname' => $displayName,
  121. ]);
  122. $template->setSubject($this->l10n->t('Email setting test'));
  123. $template->addHeader();
  124. $template->addHeading($this->l10n->t('Well done, %s!', [$displayName]));
  125. $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.'));
  126. $template->addFooter();
  127. $message = $this->mailer->createMessage();
  128. $message->setTo([$email => $displayName]);
  129. $message->useTemplate($template);
  130. $errors = $this->mailer->send($message);
  131. if (!empty($errors)) {
  132. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  133. throw new \RuntimeException($this->l10n->t('Email could not be sent. Check your mail server log'));
  134. }
  135. // Store the successful config in the app config
  136. $this->config->setAppValue('core', 'emailTestSuccessful', '1');
  137. return new DataResponse();
  138. } catch (\Exception $e) {
  139. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  140. 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);
  141. }
  142. }
  143. $this->config->setAppValue('core', 'emailTestSuccessful', '0');
  144. return new DataResponse($this->l10n->t('You need to set your account email before being able to send test emails. Go to %s for that.', [$this->urlGenerator->linkToRouteAbsolute('settings.PersonalSettings.index')]), Http::STATUS_BAD_REQUEST);
  145. }
  146. }