<?php
namespace App\EventSubscriber;
use Boab\CmsBundle\Event\ContactFormSubmitedEvent;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
class LocalMailSubscriber implements EventSubscriberInterface
{
private $fromEmail;
private $toEmail;
private $mailer;
public function __construct(MailerInterface $mailer, string $fromEmail, string $toEmail)
{
$this->mailer = $mailer;
$this->fromEmail = $fromEmail;
$this->toEmail = trim($toEmail);
}
public function onSendContactMail(ContactFormSubmitedEvent $event)
{
$mail = $event->getMail();
$email = (new TemplatedEmail())
->from(new Address($this->fromEmail, 'Contact Form'))
->to($this->toEmail)
->subject($mail->getSubject())
->htmlTemplate('email/contact_message.html.twig')
->context([
"mail"=>$mail
])
;
$this->mailer->send($email);
}
public static function getSubscribedEvents()
{
return [
'contact.form_submited' => 'onSendContactMail',
];
}
}