custom/plugins/MadFrontendContactForm/src/Subscriber/MailSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadFrontendContactForm\Subscriber;
  4. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
  5. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  6. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  15. use MadFrontendContactForm\Core\Event\SendFrontendContactFormToCustomerEvent;
  16. use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
  17. class MailSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var EntityRepositoryInterface
  21.      */
  22.     private $mailTemplateRepository;
  23.     private SystemConfigService $systemConfigService;
  24.     private EventDispatcherInterface $eventDispatcher;
  25.     public function __construct(
  26.         EntityRepositoryInterface $mailTemplateRepository,
  27.         SystemConfigService $systemConfigService,
  28.         EventDispatcherInterface $eventDispatcher
  29.     )
  30.     {
  31.         $this->mailTemplateRepository $mailTemplateRepository;
  32.         $this->systemConfigService $systemConfigService;
  33.         $this->eventDispatcher $eventDispatcher;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             MailBeforeValidateEvent::class => 'onBeforeMailValidateEvent',
  39.             MailBeforeSentEvent::class => 'onMailBeforeSentEvent'
  40.         ];
  41.     }
  42.     public function onBeforeMailValidateEvent(MailBeforeValidateEvent $event) {
  43.         if(!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
  44.             return;
  45.         }
  46.         if(
  47.             !array_key_exists('templateId'$event->getData())
  48.             || !$this->checkTamplate($event->getData()['templateId'], $event->getContext())
  49.         ) {
  50.             $this->customerEmail null;
  51.             return;
  52.         }
  53.         /* Change sender mail adress */
  54.         if($this->systemConfigService->get('MadFrontendContactForm.config.contactFormSender')){
  55.             /* ['email' => 'firstName lastName'] */
  56.             $recipients = array($this->systemConfigService->get('MadFrontendContactForm.config.contactFormSender') => $this->systemConfigService->get('MadFrontendContactForm.config.contactFormSender'));
  57.             $data $event->getData();
  58.             $data['recipients'] = $recipients;
  59.             $event->setData($data);
  60.         }
  61.         /* EOF: Change sender mail adress */
  62.         /* Change mail sender address */
  63.         if($this->systemConfigService->get('MadFrontendContactForm.config.contactFormMailSender')){
  64.             $senders = array($this->systemConfigService->get('MadFrontendContactForm.config.contactFormMailSender') => $this->systemConfigService->get('MadFrontendContactForm.config.contactFormMailSender'));
  65.             foreach ($senders as $sender){
  66.                 $event->addData('senderEmail'$sender);
  67.                 break;
  68.             }
  69.             $receivers = [
  70.                 $event->getTemplateData()['contactFormData']['email'] => $event->getTemplateData()['contactFormData']['email'],
  71.             ];
  72.             $dataCustomerMail = new DataBag();
  73.             $dataCustomerMail->set('salutationId'$event->getTemplateData()['contactFormData']['salutationId']);
  74.             $dataCustomerMail->set('firstName'$event->getTemplateData()['contactFormData']['firstName']);
  75.             $dataCustomerMail->set('lastName'$event->getTemplateData()['contactFormData']['lastName']);
  76.             $dataCustomerMail->set('email'$event->getTemplateData()['contactFormData']['email']);
  77.             $dataCustomerMail->set('phone'$event->getTemplateData()['contactFormData']['phone']);
  78.             $dataCustomerMail->set('subject'$event->getTemplateData()['contactFormData']['subject']);
  79.             $dataCustomerMail->set('comment'$event->getTemplateData()['contactFormData']['comment']);
  80.             $dataCustomerMail->set('shopware_surname_confirm'$event->getTemplateData()['contactFormData']['shopware_surname_confirm']);
  81.             $dataCustomerMail->set('privacy'$event->getTemplateData()['contactFormData']['privacy']);
  82.             $dataCustomerMail->set('navigationId'$event->getTemplateData()['contactFormData']['navigationId']);
  83.             $dataCustomerMail->set('slotId'$event->getTemplateData()['contactFormData']['slotId']);
  84.             $eventMailCustomer = new SendFrontendContactFormToCustomerEvent(
  85.                 $event->getContext(),
  86.                 $event->getContext()->getSource()->getSalesChannelId(),
  87.                 new MailRecipientStruct($receivers),
  88.                 $dataCustomerMail
  89.             );
  90.             $this->eventDispatcher->dispatch($eventMailCustomer);
  91.         }
  92.         /* EOF: Change mail sender address */
  93.         $this->customerEmail $event->getTemplateData()['contactFormData']['email'];
  94.     }
  95.     public function onMailBeforeSentEvent(MailBeforeSentEvent $event)
  96.     {
  97.         if(!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
  98.             return;
  99.         }
  100.         if(
  101.             !array_key_exists('templateId'$event->getData())
  102.             || !$this->checkTamplate($event->getData()['templateId'], $event->getContext())
  103.             || $this->customerEmail === null
  104.         ) {
  105.             return;
  106.         }
  107.         $event->getMessage()->addReplyTo($this->customerEmail);
  108.     }
  109.     private function checkTamplate(string $templateIdContext $context): bool
  110.     {
  111.         $criteria = new Criteria();
  112.         $criteria->addAssociation('mailTemplateType');
  113.         $criteria->addFilter(
  114.             new EqualsFilter('id'$templateId),
  115.             new EqualsFilter('mailTemplateType.technicalName''contact_form')
  116.         );
  117.         $result $this->mailTemplateRepository->search($criteria$context);
  118.         if($result->getTotal() < 1) {
  119.             return false;
  120.         }
  121.         return true;
  122.     }
  123. }