src/EventSubscriber/ForgotPasswordEventSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. // ...
  4. use CoopTilleuls\ForgotPasswordBundle\Event\CreateTokenEvent;
  5. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Component\Mime\Email;
  10. use Twig\Environment;
  11. use Symfony\Component\Mime\Address;
  12. final class ForgotPasswordEventSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(private readonly MailerInterface $mailer, private readonly Environment $twig, private ParameterBagInterface $params)
  15.     {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             CreateTokenEvent::class => 'onCreateToken',
  21.         ];
  22.     }
  23.     public function onCreateToken(CreateTokenEvent $event): void
  24.     {
  25.         $passwordToken $event->getPasswordToken();
  26.         $user $passwordToken->getUser();
  27.         $email = (new TemplatedEmail())
  28.             ->from(new Address($this->params->get('mailer_from'), $this->params->get('mailer_from_name')))
  29.             ->to(new Address($user->getEmail(), $user->getFirstname()))
  30.             ->bcc(new Address('contact@novalia.online''contact_test'))
  31.             ->subject('Demande de rĂ©initialisation de mot de passe Novallia Compliance')
  32.             ->htmlTemplate('user/reset-password-email.html.twig')
  33.             ->context([
  34.                 'user' => $user,
  35.                 'url' =>  $this->params->get('frontend_url') .'/?#/change-password/' $passwordToken->getToken()
  36.             ]);
  37.         $this->mailer->send($email);
  38.     }
  39. }