vendor/excelwebzone/symfony-admin-bundle/src/EventSubscriber/EmailSubscriber.php line 63

Open in your IDE?
  1. <?php
  2. namespace EWZ\SymfonyAdminBundle\EventSubscriber;
  3. use EWZ\SymfonyAdminBundle\Event\UserEvent;
  4. use EWZ\SymfonyAdminBundle\Events;
  5. use EWZ\SymfonyAdminBundle\Model\User;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Mime\Address;
  9. use Symfony\Component\Mime\Email;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Twig\Environment as TwigEnvironment;
  12. /**
  13.  * Emails events.
  14.  */
  15. class EmailSubscriber implements EventSubscriberInterface
  16. {
  17.     /** @var TwigEnvironment */
  18.     protected $twig;
  19.     /** @var MailerInterface */
  20.     protected $mailer;
  21.     /** @var UrlGeneratorInterface */
  22.     protected $urlGenerator;
  23.     /** @var string */
  24.     protected $sender;
  25.     /**
  26.      * @param TwigEnvironment       $twig
  27.      * @param MailerInterface       $mailer
  28.      * @param UrlGeneratorInterface $urlGenerator
  29.      * @param string                $sender
  30.      */
  31.     public function __construct(
  32.         TwigEnvironment $twig,
  33.         MailerInterface $mailer,
  34.         UrlGeneratorInterface $urlGenerator,
  35.         string $sender
  36.     ) {
  37.         $this->twig $twig;
  38.         $this->mailer $mailer;
  39.         $this->urlGenerator $urlGenerator;
  40.         $this->sender $sender;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             Events::RESETTING_PASSWORD_SENT => 'onResettingPasswordSent',
  49.         ];
  50.     }
  51.     /**
  52.      * @param UserEvent $event
  53.      */
  54.     public function onResettingPasswordSent(UserEvent $event): void
  55.     {
  56.         /** @var User $user */
  57.         $user $event->getUser();
  58.         $url $this->urlGenerator->generate('resetting_reset', [
  59.             'token' => $user->getConfirmationToken(),
  60.         ], UrlGeneratorInterface::ABSOLUTE_URL);
  61.         $context = [
  62.             'user' => $user,
  63.             'confirmationUrl' => $url,
  64.         ];
  65.         $this->sendMessage('@SymfonyAdmin/resetting/email.txt.twig'$context$user->getEmail());
  66.     }
  67.     /**
  68.      * @param string              $templateName
  69.      * @param array               $context
  70.      * @param string|array        $toEmail
  71.      * @param string|Address|null $sender
  72.      * @param array               $attachments
  73.      */
  74.     protected function sendMessage(string $templateName, array $context$toEmail$sender null, array $attachments = []): void
  75.     {
  76.         if (empty($toEmail)) {
  77.             return;
  78.         } elseif (!\is_array($toEmail)) {
  79.             $toEmail = [$toEmail];
  80.         }
  81.         $template $this->twig->load($templateName);
  82.         $subject $template->renderBlock('subject'$context);
  83.         $textBody $template->renderBlock('body_text'$context);
  84.         $htmlBody null;
  85.         if ($template->hasBlock('body_html'$context)) {
  86.             $htmlBody $this->parseHtmlBody(
  87.                 $template->renderBlock('body_html'$context)
  88.             );
  89.         }
  90.         $email = (new Email())
  91.             ->from($sender ?: $this->sender)
  92.             ->to(...array_filter($toEmail))
  93.             ->subject($subject)
  94.             ->text($textBody);
  95.         if (!empty($htmlBody)) {
  96.             $email->html($htmlBody);
  97.         }
  98.         foreach ($attachments as $attachment) {
  99.             $email->attachFromPath($attachment);
  100.         }
  101.         $this->mailer->send($email);
  102.     }
  103.     /**
  104.      * @param string $htmlBody
  105.      *
  106.      * @return string
  107.      */
  108.     protected function parseHtmlBody(string $htmlBody): string
  109.     {
  110.         return $htmlBody;
  111.     }
  112. }