1
0

NewUserMailHelper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Mailer;
  7. use OCP\AppFramework\Utility\ITimeFactory;
  8. use OCP\Defaults;
  9. use OCP\IConfig;
  10. use OCP\IURLGenerator;
  11. use OCP\IUser;
  12. use OCP\L10N\IFactory;
  13. use OCP\Mail\Headers\AutoSubmitted;
  14. use OCP\Mail\IEMailTemplate;
  15. use OCP\Mail\IMailer;
  16. use OCP\Security\ICrypto;
  17. use OCP\Security\ISecureRandom;
  18. class NewUserMailHelper {
  19. /** @var Defaults */
  20. private $themingDefaults;
  21. /** @var IURLGenerator */
  22. private $urlGenerator;
  23. /** @var IFactory */
  24. private $l10nFactory;
  25. /** @var IMailer */
  26. private $mailer;
  27. /** @var ISecureRandom */
  28. private $secureRandom;
  29. /** @var ITimeFactory */
  30. private $timeFactory;
  31. /** @var IConfig */
  32. private $config;
  33. /** @var ICrypto */
  34. private $crypto;
  35. /** @var string */
  36. private $fromAddress;
  37. /**
  38. * @param Defaults $themingDefaults
  39. * @param IURLGenerator $urlGenerator
  40. * @param IFactory $l10nFactory
  41. * @param IMailer $mailer
  42. * @param ISecureRandom $secureRandom
  43. * @param ITimeFactory $timeFactory
  44. * @param IConfig $config
  45. * @param ICrypto $crypto
  46. * @param string $fromAddress
  47. */
  48. public function __construct(Defaults $themingDefaults,
  49. IURLGenerator $urlGenerator,
  50. IFactory $l10nFactory,
  51. IMailer $mailer,
  52. ISecureRandom $secureRandom,
  53. ITimeFactory $timeFactory,
  54. IConfig $config,
  55. ICrypto $crypto,
  56. $fromAddress) {
  57. $this->themingDefaults = $themingDefaults;
  58. $this->urlGenerator = $urlGenerator;
  59. $this->l10nFactory = $l10nFactory;
  60. $this->mailer = $mailer;
  61. $this->secureRandom = $secureRandom;
  62. $this->timeFactory = $timeFactory;
  63. $this->config = $config;
  64. $this->crypto = $crypto;
  65. $this->fromAddress = $fromAddress;
  66. }
  67. /**
  68. * @param IUser $user
  69. * @param bool $generatePasswordResetToken
  70. * @return IEMailTemplate
  71. */
  72. public function generateTemplate(IUser $user, $generatePasswordResetToken = false) {
  73. $userId = $user->getUID();
  74. $lang = $this->l10nFactory->getUserLanguage($user);
  75. $l10n = $this->l10nFactory->get('settings', $lang);
  76. if ($generatePasswordResetToken) {
  77. $token = $this->secureRandom->generate(
  78. 21,
  79. ISecureRandom::CHAR_ALPHANUMERIC
  80. );
  81. $tokenValue = $this->timeFactory->getTime() . ':' . $token;
  82. $mailAddress = ($user->getEMailAddress() !== null) ? $user->getEMailAddress() : '';
  83. $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret'));
  84. $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue);
  85. $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]);
  86. } else {
  87. $link = $this->urlGenerator->getAbsoluteURL('/');
  88. }
  89. $displayName = $user->getDisplayName();
  90. $emailTemplate = $this->mailer->createEMailTemplate('settings.Welcome', [
  91. 'link' => $link,
  92. 'displayname' => $displayName,
  93. 'userid' => $userId,
  94. 'instancename' => $this->themingDefaults->getName(),
  95. 'resetTokenGenerated' => $generatePasswordResetToken,
  96. ]);
  97. $emailTemplate->setSubject($l10n->t('Your %s account was created', [$this->themingDefaults->getName()]));
  98. $emailTemplate->addHeader();
  99. if ($displayName === $userId) {
  100. $emailTemplate->addHeading($l10n->t('Welcome aboard'));
  101. } else {
  102. $emailTemplate->addHeading($l10n->t('Welcome aboard %s', [$displayName]));
  103. }
  104. $emailTemplate->addBodyText($l10n->t('Welcome to your %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()]));
  105. if ($user->getBackendClassName() !== 'LDAP') {
  106. $emailTemplate->addBodyText($l10n->t('Your Login is: %s', [$userId]));
  107. }
  108. if ($generatePasswordResetToken) {
  109. $leftButtonText = $l10n->t('Set your password');
  110. } else {
  111. $leftButtonText = $l10n->t('Go to %s', [$this->themingDefaults->getName()]);
  112. }
  113. $clientDownload = $this->config->getSystemValue('customclient_desktop', 'https://nextcloud.com/install/#install-clients');
  114. if ($clientDownload === '') {
  115. $emailTemplate->addBodyButton(
  116. $leftButtonText,
  117. $link
  118. );
  119. } else {
  120. $emailTemplate->addBodyButtonGroup(
  121. $leftButtonText,
  122. $link,
  123. $l10n->t('Install Client'),
  124. $clientDownload
  125. );
  126. }
  127. $emailTemplate->addFooter('', $lang);
  128. return $emailTemplate;
  129. }
  130. /**
  131. * Sends a welcome mail to $user
  132. *
  133. * @param IUser $user
  134. * @param IEmailTemplate $emailTemplate
  135. * @throws \Exception If mail could not be sent
  136. */
  137. public function sendMail(IUser $user,
  138. IEMailTemplate $emailTemplate): void {
  139. // Be sure to never try to send to an empty e-mail
  140. $email = $user->getEMailAddress();
  141. if ($email === null) {
  142. return;
  143. }
  144. $message = $this->mailer->createMessage();
  145. $message->setTo([$email => $user->getDisplayName()]);
  146. $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]);
  147. $message->useTemplate($emailTemplate);
  148. $message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
  149. $this->mailer->send($message);
  150. }
  151. }