src/EventSubscriber/LocalMailSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Boab\CmsBundle\Event\ContactFormSubmitedEvent;
  4. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Mailer\MailerInterface;
  7. use Symfony\Component\Mime\Address;
  8. class LocalMailSubscriber implements EventSubscriberInterface
  9. {
  10.     private $fromEmail;
  11.     private $toEmail;
  12.     private $mailer;
  13.     public function __construct(MailerInterface $mailerstring $fromEmailstring $toEmail)
  14.     {
  15.         $this->mailer $mailer;
  16.         $this->fromEmail $fromEmail;
  17.         $this->toEmail trim($toEmail);
  18.     }
  19.     public function onSendContactMail(ContactFormSubmitedEvent $event)
  20.     {
  21.         $mail $event->getMail();
  22.         $email = (new TemplatedEmail())
  23.             ->from(new Address($this->fromEmail'Contact Form'))
  24.             ->to($this->toEmail)
  25.             ->subject($mail->getSubject())
  26.             ->htmlTemplate('email/contact_message.html.twig')
  27.             ->context([
  28.                 "mail"=>$mail
  29.             ])
  30.         ;
  31.         $this->mailer->send($email);
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             'contact.form_submited' => 'onSendContactMail',
  37.         ];
  38.     }    
  39. }