src/EventSubscriber/SendEmailEventSubscriber.php line 46
<?php
namespace App\EventSubscriber;
use App\Event\SendEmailEvent;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
class SendEmailEventSubscriber implements EventSubscriberInterface
{
use LoggerAwareTrait;
protected MailerInterface $mailer;
protected EntityManagerInterface $entityManager;
protected string $bccEmail;
/**
* SendEmailEventSubscriber constructor.
* @param MailerInterface $mailer
* @param EntityManagerInterface $entityManager
* @param string $bccEmail
*/
public function __construct(MailerInterface $mailer, EntityManagerInterface $entityManager, string $bccEmail)
{
$this->mailer = $mailer;
$this->entityManager = $entityManager;
$this->bccEmail = $bccEmail;
}
public static function getSubscribedEvents(): array
{
return [
SendEmailEvent::NAME => [
['sendEmail', 10]
]
];
}
public function sendEmail(SendEmailEvent $event): void
{
// $this->logger->info('mail started');
$emailParameters = $event->getEmailParameters();
$email = (new TemplatedEmail())
->from(new Address($emailParameters['from']['email'], $emailParameters['from']['name']));
// $email = (new TemplatedEmail())
// ->from(new Address('giftcards@mooments.com', $emailParameters['from']['name']));
if(is_array($emailParameters['to']['email'])) {
foreach($emailParameters['to']['email'] as $emailId)
{
$email->addTo($emailId);
}
}
else {
$email->to(new Address($emailParameters['to']['email']));
}
if(isset($emailParameters['attachment']))
{
$email->attachFromPath($emailParameters['attachment']);
}
$email->subject($emailParameters['subject'])
->htmlTemplate($emailParameters['template'])
->context($emailParameters['parameters'])
;
$email->addBcc(new Address($this->bccEmail));
try {
// $this->logger->info('mail submitted');
$this->mailer->send($email);
} catch (TransportExceptionInterface $e) {
$this->logger->critical($e->getMessage());
}
}
}