src/Event/Form/SendEmailSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Antony Tkachenko <at@canaryknight.ru>
  4.  */
  5. namespace App\Event\Form;
  6. use App\Form\FormHandledEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Mime\Address;
  11. class SendEmailSubscriber implements EventSubscriberInterface
  12. {
  13.     private $mailer;
  14.     private $recipients = [];
  15.     private $fromEmail;
  16.     public function __construct(
  17.         array $recipients,
  18.         string $fromEmail,
  19.         MailerInterface $mailer
  20.     ) {
  21.         $this->mailer $mailer;
  22.         $this->recipients $recipients;
  23.         $this->fromEmail $fromEmail;
  24.     }
  25.     public function sendEmail(FormHandledEvent $event)
  26.     {
  27.         $data $event->getData();
  28.         $dto $event->getDto();
  29.         $this->recipients = ['kk@getmeback.ru'];
  30.         if( isset( $data['password'] ) ){
  31.             unset( $data['password'] );
  32.         }
  33.         $email = (new TemplatedEmail())
  34.             ->from(new Address($this->fromEmail'GMB Robot'))
  35.             ->to(...$this->recipients)
  36.             ->subject($dto->subject())
  37.             ->htmlTemplate("email/form/tpl.html.twig")
  38.             ->context([
  39.                 'data' => $data,
  40.                 'dto' => $dto,
  41.             ])
  42.         ;
  43.         try {
  44.             $this->mailer->send($email);
  45.         } catch (\Throwable $e) {
  46.         }
  47.     }
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [
  51.             FormHandledEvent::NAME => 'sendEmail'
  52.         ];
  53.     }
  54. }