MailSettingsController.php 4.7 KB

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