src/EventSubscriber/SetFromSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Mailer\Event\MessageEvent;
  5. use Symfony\Component\Mime\Address;
  6. use Symfony\Component\Mime\Email;
  7. class SetFromSubscriber implements EventSubscriberInterface
  8. {
  9.     private array $config;
  10.     public function __construct($config)
  11.     {
  12.         $this->config $config;
  13.     }
  14.     public function onMessage(MessageEvent $event)
  15.     {
  16.         $email $event->getMessage();
  17.         if (!$email instanceof Email) {
  18.             return;
  19.         }
  20.         $email->from(new Address($this->config['from'], $this->config['name']));
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             MessageEvent::class => 'onMessage',
  26.         ];
  27.     }
  28. }