vendor/witalink/starter-bundle/src/EventSubscriber/Subscriber/EmailSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace Witalink\StarterBundle\EventSubscriber\Subscriber;
  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 EmailSubscriber implements EventSubscriberInterface
  8. {
  9.     private $applicationName;
  10.     private $noReplyAddress;
  11.     private $noReplyDisplay;
  12.     public function __construct(string $applicationNamestring $noReplyAddressstring $noReplyDisplay)
  13.     {
  14.         $this->applicationName $applicationName;
  15.         $this->noReplyAddress $noReplyAddress;
  16.         $this->noReplyDisplay $noReplyDisplay;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             MessageEvent::class => 'onMessage',
  22.         ];
  23.     }
  24.     public function onMessage(MessageEvent $event)
  25.     {
  26.         $email $event->getMessage();
  27.         if (!$email instanceof Email) {
  28.             return;
  29.         }
  30.         // Ajout automatique du From
  31.         $email->from(new Address($this->noReplyAddress$this->noReplyDisplay));
  32.         // Ajout de l'application_name si pas dans le titre du mails
  33.         $subject $email->getSubject();
  34.         if (stripos($subject$this->applicationName) === false) {
  35.             $email->subject("[$this->applicationName$subject");
  36.         }
  37.     }
  38. }