NewUserMailHelper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /**
  20. * @param Defaults $themingDefaults
  21. * @param IURLGenerator $urlGenerator
  22. * @param IFactory $l10nFactory
  23. * @param IMailer $mailer
  24. * @param ISecureRandom $secureRandom
  25. * @param ITimeFactory $timeFactory
  26. * @param IConfig $config
  27. * @param ICrypto $crypto
  28. * @param string $fromAddress
  29. */
  30. public function __construct(
  31. private Defaults $themingDefaults,
  32. private IURLGenerator $urlGenerator,
  33. private IFactory $l10nFactory,
  34. private IMailer $mailer,
  35. private ISecureRandom $secureRandom,
  36. private ITimeFactory $timeFactory,
  37. private IConfig $config,
  38. private ICrypto $crypto,
  39. private $fromAddress,
  40. ) {
  41. }
  42. /**
  43. * @param IUser $user
  44. * @param bool $generatePasswordResetToken
  45. * @return IEMailTemplate
  46. */
  47. public function generateTemplate(IUser $user, $generatePasswordResetToken = false) {
  48. $userId = $user->getUID();
  49. $lang = $this->l10nFactory->getUserLanguage($user);
  50. $l10n = $this->l10nFactory->get('settings', $lang);
  51. if ($generatePasswordResetToken) {
  52. $token = $this->secureRandom->generate(
  53. 21,
  54. ISecureRandom::CHAR_ALPHANUMERIC
  55. );
  56. $tokenValue = $this->timeFactory->getTime() . ':' . $token;
  57. $mailAddress = ($user->getEMailAddress() !== null) ? $user->getEMailAddress() : '';
  58. $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret'));
  59. $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue);
  60. $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]);
  61. } else {
  62. $link = $this->urlGenerator->getAbsoluteURL('/');
  63. }
  64. $displayName = $user->getDisplayName();
  65. $emailTemplate = $this->mailer->createEMailTemplate('settings.Welcome', [
  66. 'link' => $link,
  67. 'displayname' => $displayName,
  68. 'userid' => $userId,
  69. 'instancename' => $this->themingDefaults->getName(),
  70. 'resetTokenGenerated' => $generatePasswordResetToken,
  71. ]);
  72. $emailTemplate->setSubject($l10n->t('Your %s account was created', [$this->themingDefaults->getName()]));
  73. $emailTemplate->addHeader();
  74. if ($displayName === $userId) {
  75. $emailTemplate->addHeading($l10n->t('Welcome aboard'));
  76. } else {
  77. $emailTemplate->addHeading($l10n->t('Welcome aboard %s', [$displayName]));
  78. }
  79. $emailTemplate->addBodyText($l10n->t('Welcome to your %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()]));
  80. if ($user->getBackendClassName() !== 'LDAP') {
  81. $emailTemplate->addBodyText($l10n->t('Your Login is: %s', [$userId]));
  82. }
  83. if ($generatePasswordResetToken) {
  84. $leftButtonText = $l10n->t('Set your password');
  85. } else {
  86. $leftButtonText = $l10n->t('Go to %s', [$this->themingDefaults->getName()]);
  87. }
  88. $clientDownload = $this->config->getSystemValue('customclient_desktop', 'https://nextcloud.com/install/#install-clients');
  89. if ($clientDownload === '') {
  90. $emailTemplate->addBodyButton(
  91. $leftButtonText,
  92. $link
  93. );
  94. } else {
  95. $emailTemplate->addBodyButtonGroup(
  96. $leftButtonText,
  97. $link,
  98. $l10n->t('Install Client'),
  99. $clientDownload
  100. );
  101. }
  102. $emailTemplate->addFooter('', $lang);
  103. return $emailTemplate;
  104. }
  105. /**
  106. * Sends a welcome mail to $user
  107. *
  108. * @param IUser $user
  109. * @param IEmailTemplate $emailTemplate
  110. * @throws \Exception If mail could not be sent
  111. */
  112. public function sendMail(IUser $user,
  113. IEMailTemplate $emailTemplate): void {
  114. // Be sure to never try to send to an empty e-mail
  115. $email = $user->getEMailAddress();
  116. if ($email === null) {
  117. return;
  118. }
  119. $message = $this->mailer->createMessage();
  120. $message->setTo([$email => $user->getDisplayName()]);
  121. $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]);
  122. $message->useTemplate($emailTemplate);
  123. $message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
  124. $this->mailer->send($message);
  125. }
  126. }