<?php declare(strict_types=1);
namespace App\EventListener;
use App\Event\ContactEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContactSubscriber implements EventSubscriberInterface
{
private $mailer;
private $logger;
public function __construct(\Swift_Mailer $mailer, LoggerInterface $logger)
{
$this->mailer = $mailer;
$this->logger = $logger;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
ContactEvent::CREATE_CONTACT => ['createContact'],
];
}
public function createContact(ContactEvent $contactEvent)
{
$message = (new \Swift_Message())
->setSubject('Nouveau message de contact Plateforme Evasion Ishere')
->setTo([$_ENV['MAILER_ADMIN']])
->setBcc(['[email protected]'])
->setFrom(['[email protected]'=> 'Admin Plateforme Evasion Ishere'])
->setBody(
$contactEvent->getContact()->mailFormat(),
'text/html'
);
try {
$this->mailer->send($message);
} catch (\Exception $e) {
$this->logger->error('Error sending email', [ 'message' => $e->getMessage(), 'code' => $e->getCode() ]);
}
}
}