MailSettingsController.php 5.0 KB

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