src/EventSubscriber/SendEmailEventSubscriber.php line 46

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\SendEmailEvent;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Psr\Log\LoggerAwareTrait;
  6. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Mime\Address;
  11. class SendEmailEventSubscriber implements EventSubscriberInterface
  12. {
  13.     use LoggerAwareTrait;
  14.     protected MailerInterface $mailer;
  15.     protected EntityManagerInterface $entityManager;
  16.     protected string $bccEmail;
  17.     /**
  18.      * SendEmailEventSubscriber constructor.
  19.      * @param MailerInterface $mailer
  20.      * @param EntityManagerInterface $entityManager
  21.      * @param string $bccEmail
  22.      */
  23.     public function __construct(MailerInterface $mailerEntityManagerInterface $entityManagerstring $bccEmail)
  24.     {
  25.         $this->mailer $mailer;
  26.         $this->entityManager $entityManager;
  27.         $this->bccEmail $bccEmail;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             SendEmailEvent::NAME => [
  33.                 ['sendEmail'10]
  34.             ]
  35.         ];
  36.     }
  37.     public function sendEmail(SendEmailEvent $event): void
  38.     {
  39. //        $this->logger->info('mail started');
  40.         $emailParameters $event->getEmailParameters();
  41.         $email = (new TemplatedEmail())
  42.             ->from(new Address($emailParameters['from']['email'], $emailParameters['from']['name']));
  43. //        $email = (new TemplatedEmail())
  44. //            ->from(new Address('giftcards@mooments.com', $emailParameters['from']['name']));
  45.         if(is_array($emailParameters['to']['email'])) {
  46.             foreach($emailParameters['to']['email'] as $emailId)
  47.             {
  48.                 $email->addTo($emailId);
  49.             }
  50.         }
  51.         else {
  52.             $email->to(new Address($emailParameters['to']['email']));
  53.         }
  54.         if(isset($emailParameters['attachment']))
  55.         {
  56.             $email->attachFromPath($emailParameters['attachment']);
  57.         }
  58.         $email->subject($emailParameters['subject'])
  59.             ->htmlTemplate($emailParameters['template'])
  60.             ->context($emailParameters['parameters'])
  61.         ;
  62.         $email->addBcc(new Address($this->bccEmail));
  63.         try {
  64. //            $this->logger->info('mail submitted');
  65.             $this->mailer->send($email);
  66.         } catch (TransportExceptionInterface $e) {
  67.             $this->logger->critical($e->getMessage());
  68.         }
  69.     }
  70. }