<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
class SetFromSubscriber implements EventSubscriberInterface
{
private array $config;
public function __construct($config)
{
$this->config = $config;
}
public function onMessage(MessageEvent $event)
{
$email = $event->getMessage();
if (!$email instanceof Email) {
return;
}
$email->from(new Address($this->config['from'], $this->config['name']));
}
public static function getSubscribedEvents(): array
{
return [
MessageEvent::class => 'onMessage',
];
}
}