<?php
namespace Boldr\Cms\CmsBundle\EventSubscriber;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Boldr\Cms\CmsBundle\MailRenderer;
class MailSubscriber implements EventSubscriberInterface
{
private MailRenderer $mailRenderer;
public function __construct(MailRenderer $mailRenderer)
{
$this->mailRenderer = $mailRenderer;
}
public static function getSubscribedEvents()
{
return [
MessageEvent::class => [['onMessage']]
];
}
public function onMessage(MessageEvent $event): void
{
$message = $event->getMessage();
if (method_exists($message, 'getHtmlBody') && method_exists($message, 'html') && method_exists($message, 'getSubject'))
{
$message->html($this->mailRenderer->renderMail($message->getHtmlBody(), $message->getSubject()));
}
}
}