Automatically execute body renderer when posting an email in Chill

+ adaptation of services which already uses body renderer
This commit is contained in:
2024-10-10 14:06:00 +02:00
parent 3d53e7da65
commit 1313b6f138
3 changed files with 15 additions and 6 deletions

View File

@@ -12,9 +12,11 @@ declare(strict_types=1);
namespace Chill\MainBundle\Service\Mailer;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\BodyRendererInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\RawMessage;
@@ -22,7 +24,7 @@ class ChillMailer implements MailerInterface
{
private string $prefix = '[Chill] ';
public function __construct(private readonly MailerInterface $initial, private readonly LoggerInterface $chillLogger) {}
public function __construct(private readonly MailerInterface $initial, private readonly LoggerInterface $chillLogger, private readonly BodyRendererInterface $bodyRenderer) {}
public function send(RawMessage $message, ?Envelope $envelope = null): void
{
@@ -30,6 +32,10 @@ class ChillMailer implements MailerInterface
$message->subject($this->prefix.$message->getSubject());
}
if ($message instanceof TemplatedEmail) {
$this->bodyRenderer->render($message);
}
$this->chillLogger->info('chill email sent', [
'to' => array_map(static fn (Address $address) => $address->getAddress(), $message->getTo()),
'subject' => $message->getSubject(),