custom/plugins/TTechEmailReplyTo/src/Subscriber/ContactFormMailBeforeSentEventSubscriber.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TTechEmailReplyTo\Subscriber;
  3. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
  4. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  5. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Mime\Address;
  13. class ContactFormMailBeforeSentEventSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var SystemConfigService
  17.      */
  18.     private $systemConfigService;
  19.     /**
  20.      * @var EntityRepositoryInterface
  21.      */
  22.     private $mailTemplateRepository;
  23.     private ?string $customerEmail null;
  24.     private ?string $customerName null;
  25.     public function __construct(
  26.         SystemConfigService $systemConfigService,
  27.         EntityRepositoryInterface $mailTemplateRepository
  28.     )
  29.     {
  30.         $this->systemConfigService $systemConfigService;
  31.         $this->mailTemplateRepository $mailTemplateRepository;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             MailBeforeValidateEvent::class => 'onBeforeMailValidateEvent',
  37.             MailBeforeSentEvent::class => 'onMailBeforeSentEvent',
  38.         ];
  39.     }
  40.     public function onBeforeMailValidateEvent(MailBeforeValidateEvent $event) {
  41.         if(!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
  42.             return;
  43.         }
  44.         /** @var SalesChannelApiSource $source */
  45.         $source $event->getContext()->getSource();
  46.         if($this->systemConfigService->get('TTechEmailReplyTo.config.setReplyToForContactForm'$source->getSalesChannelId()) !== true) {
  47.             return;
  48.         }
  49.         if(
  50.             !array_key_exists('templateId'$event->getData())
  51.             || !$this->isCorrectMailTemplate($event->getData()['templateId'], $event->getContext())
  52.         ) {
  53.             $this->customerEmail null;
  54.             $this->customerName null;
  55.             return;
  56.         }
  57.         $this->customerEmail $event->getTemplateData()['contactFormData']['email'];
  58.         $this->customerName $event->getTemplateData()['contactFormData']['firstName'] . ' ' $event->getTemplateData()['contactFormData']['lastName'];
  59.     }
  60.     public function onMailBeforeSentEvent(MailBeforeSentEvent $event) {
  61.         if(!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
  62.             return;
  63.         }
  64.         /** @var SalesChannelApiSource $source */
  65.         $source $event->getContext()->getSource();
  66.         if($this->systemConfigService->get('TTechEmailReplyTo.config.setReplyToForContactForm'$source->getSalesChannelId()) !== true) {
  67.             return;
  68.         }
  69.         if(
  70.             !array_key_exists('templateId'$event->getData())
  71.             || !$this->isCorrectMailTemplate($event->getData()['templateId'], $event->getContext())
  72.         ) {
  73.             return;
  74.         }
  75.         if($this->customerEmail === null) {
  76.             return;
  77.         }
  78.         $event->getMessage()->replyTo(new Address($this->customerEmail$this->customerName));
  79.     }
  80.     private function isCorrectMailTemplate(string $templateIdContext $context): bool
  81.     {
  82.         $criteria = new Criteria();
  83.         $criteria->addAssociation('mailTemplateType');
  84.         $criteria->addFilter(
  85.             new EqualsFilter('id'$templateId),
  86.             new EqualsFilter('mailTemplateType.technicalName''contact_form')
  87.         );
  88.         $result $this->mailTemplateRepository->search($criteria$context);
  89.         if($result->getTotal() < 1) {
  90.             return false;
  91.         }
  92.         return true;
  93.     }
  94. }