‪TYPO3CMS  ‪main
RecoveryService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
21 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
22 use Symfony\Component\Mime\Address;
32 
37 {
38  protected array ‪$settings;
39 
40  public function ‪__construct(
41  protected readonly ‪MailerInterface $mailer,
42  protected EventDispatcherInterface $eventDispatcher,
43  ‪ConfigurationManagerInterface $configurationManager,
44  protected ‪RecoveryConfiguration $recoveryConfiguration,
45  protected ‪UriBuilder $uriBuilder
46  ) {
48  }
49 
56  public function ‪sendRecoveryEmail(‪RequestInterface $request, array $userData, string $hash): void
57  {
58  $receiver = new Address($userData['email'], $this->‪getReceiverName($userData));
59  $email = $this->‪prepareMail($request, $receiver, $hash, $userData);
60 
61  $event = new ‪SendRecoveryEmailEvent($email, $userData);
62  $this->eventDispatcher->dispatch($event);
63  $this->mailer->send($event->getEmail());
64  }
65 
69  protected function ‪getReceiverName(array $userInformation): string
70  {
71  $displayName = trim(
72  sprintf(
73  '%s%s%s',
74  $userInformation['first_name'],
75  $userInformation['middle_name'] ? " {$userInformation['middle_name']}" : '',
76  $userInformation['last_name'] ? " {$userInformation['last_name']}" : ''
77  )
78  );
79 
80  return $displayName ? $displayName . ' (' . $userInformation['username'] . ')' : $userInformation['username'];
81  }
82 
86  protected function ‪prepareMail(‪RequestInterface $request, Address $receiver, string $hash, array $userData): ‪FluidEmail
87  {
88  ‪$url = $this->uriBuilder
89  ->reset()
90  ->setRequest($request)
91  ->setCreateAbsoluteUri(true)
92  ->uriFor(
93  'showChangePassword',
94  ['hash' => $hash],
95  'PasswordRecovery',
96  'felogin',
97  'Login'
98  );
99 
100  $variables = [
101  'receiverName' => $receiver->getName(),
102  'userData' => $userData,
103  'url' => ‪$url,
104  'validUntil' => date($this->settings['dateFormat'], $this->recoveryConfiguration->getLifeTimeTimestamp()),
105  ];
106 
107  $mailTemplatePaths = $this->recoveryConfiguration->getMailTemplatePaths();
108 
109  $mail = GeneralUtility::makeInstance(FluidEmail::class, $mailTemplatePaths);
110  $mail->setRequest($request);
111  $mail->subject($this->‪getEmailSubject())
112  ->from($this->recoveryConfiguration->getSender())
113  ->to($receiver)
114  ->assignMultiple($variables)
115  ->setTemplate($this->recoveryConfiguration->getMailTemplateName());
116 
117  $replyTo = $this->recoveryConfiguration->getReplyTo();
118  if ($replyTo) {
119  $mail->addReplyTo($replyTo);
120  }
121 
122  return $mail;
123  }
124 
125  protected function ‪getEmailSubject(): string
126  {
127  return ‪LocalizationUtility::translate('password_recovery_mail_header', 'felogin');
128  }
129 }
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:35
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\prepareMail
‪prepareMail(RequestInterface $request, Address $receiver, string $hash, array $userData)
Definition: RecoveryService.php:86
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Mail\MailerInterface
Definition: MailerInterface.php:28
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\getReceiverName
‪getReceiverName(array $userInformation)
Definition: RecoveryService.php:69
‪TYPO3\CMS\FrontendLogin\Service
Definition: RecoveryService.php:18
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService
Definition: RecoveryService.php:37
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\sendRecoveryEmail
‪sendRecoveryEmail(RequestInterface $request, array $userData, string $hash)
Definition: RecoveryService.php:56
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$settings
‪array $settings
Definition: RecoveryService.php:38
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\__construct
‪__construct(protected readonly MailerInterface $mailer, protected EventDispatcherInterface $eventDispatcher, ConfigurationManagerInterface $configurationManager, protected RecoveryConfiguration $recoveryConfiguration, protected UriBuilder $uriBuilder)
Definition: RecoveryService.php:40
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, Locale|string $languageKey=null)
Definition: LocalizationUtility.php:47
‪TYPO3\CMS\FrontendLogin\Configuration\RecoveryConfiguration
Definition: RecoveryConfiguration.php:33
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\getConfiguration
‪array getConfiguration(string $configurationType, ?string $extensionName=null, ?string $pluginName=null)
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_SETTINGS
‪const CONFIGURATION_TYPE_SETTINGS
Definition: ConfigurationManagerInterface.php:30
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\getEmailSubject
‪getEmailSubject()
Definition: RecoveryService.php:125
‪TYPO3\CMS\FrontendLogin\Event\SendRecoveryEmailEvent
Definition: SendRecoveryEmailEvent.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52