<?php
/**
* @author Antony Tkachenko <at@canaryknight.ru>
*/
namespace App\Event\Form;
use App\Form\FormHandledEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
class SendEmailSubscriber implements EventSubscriberInterface
{
private $mailer;
private $recipients = [];
private $fromEmail;
public function __construct(
array $recipients,
string $fromEmail,
MailerInterface $mailer
) {
$this->mailer = $mailer;
$this->recipients = $recipients;
$this->fromEmail = $fromEmail;
}
public function sendEmail(FormHandledEvent $event)
{
$data = $event->getData();
$dto = $event->getDto();
$this->recipients = ['kk@getmeback.ru'];
if( isset( $data['password'] ) ){
unset( $data['password'] );
}
$email = (new TemplatedEmail())
->from(new Address($this->fromEmail, 'GMB Robot'))
->to(...$this->recipients)
->subject($dto->subject())
->htmlTemplate("email/form/tpl.html.twig")
->context([
'data' => $data,
'dto' => $dto,
])
;
try {
$this->mailer->send($email);
} catch (\Throwable $e) {
}
}
public static function getSubscribedEvents()
{
return [
FormHandledEvent::NAME => 'sendEmail'
];
}
}