postoffice-1.0.x-dev/extensions/postoffice_twig/src/EventSubscriber/TwigFromSubscriber.php
extensions/postoffice_twig/src/EventSubscriber/TwigFromSubscriber.php
<?php
namespace Drupal\postoffice_twig\EventSubscriber;
use Drupal\postoffice\Email\TemplateAttachmentsInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Email;
/**
* Sets the from name and/or address defined by a twig filter.
*
* Runs with priority -50 (after Symfony MessageListener which is responsible
* for body rendering).
*/
class TwigFromSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
$events = [];
$events[MessageEvent::class] = ['onMessage', -50];
return $events;
}
/**
* Populates the subject defined postoffice_subject twig filter.
*/
public function onMessage(MessageEvent $event): void {
$message = $event->getMessage();
if ($message instanceof Email && $message instanceof TemplateAttachmentsInterface) {
$settings = $message->getTemplateAttachments()->getSettings();
if (isset($settings['postofficeFromAddress'])) {
$message->from($settings['postofficeFromAddress']);
}
}
}
}
