custom/plugins/EsmComputer/src/Storefront/Subscriber/FormMailSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EsmComputer\Storefront\Subscriber;
  3. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldCollection;
  6. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldEntity;
  7. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\Type\Email;
  8. use Swag\CmsExtensions\Form\Event\CustomFormEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use function array_key_exists;
  11. class FormMailSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SystemConfigService
  15.      */
  16.     private $systemConfigService;
  17.     /**
  18.      * @var AbstractMailService
  19.      */
  20.     private $mailService;
  21.     public function __construct(SystemConfigService $systemConfigServiceAbstractMailService $mailService)
  22.     {
  23.         $this->systemConfigService $systemConfigService;
  24.         $this->mailService $mailService;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             CustomFormEvent::EVENT_NAME => 'sendMail',
  30.         ];
  31.     }
  32.     public function sendMail(CustomFormEvent $event): void
  33.     {
  34.         $receivers $event->getForm()->getReceivers();
  35.         if (empty($receivers)) {
  36.             $receivers[] = $this->systemConfigService->get('core.basicInformation.email'$event->getSalesChannelId());
  37.         }
  38.         $groups $event->getForm()->getGroups();
  39.         $fields $groups === null ? new FormGroupFieldCollection() : $groups->getFields();
  40.         $mailTemplate $event->getForm()->getMailTemplate();
  41.         $data $mailTemplate !== null $mailTemplate->jsonSerialize() : [];
  42.         //@TODO this is custom extension to send attachments in swag cms forms
  43.         if(array_key_exists('binAttachments'$event->getFormData())) {
  44.             $data['binAttachments'] = $event->getFormData()['binAttachments'];
  45.         }
  46.         $data['salesChannelId'] = $event->getSalesChannelId();
  47.         if ($sender $this->getSenderMail($fields$event->getFormData())) {
  48.             $data['replyTo'] = $sender;
  49.         }
  50.         $templateData = [
  51.             'form' => $event->getForm(),
  52.             'fields' => $fields,
  53.             'formData' => $event->getFormData(),
  54.             'salesChannel' => $event->getSalesChannelContext()->getSalesChannel(),
  55.         ];
  56.         foreach ($receivers as $mail) {
  57.             if (!\is_string($mail)) {
  58.                 continue;
  59.             }
  60.             $data['recipients'] = [$mail => $mail];
  61.             $this->mailService->send($data$event->getContext(), $templateData);
  62.         }
  63.     }
  64.     private function getSenderMail(FormGroupFieldCollection $fieldCollection, array $formData): ?string
  65.     {
  66.         $mailField $fieldCollection->filter(static function (FormGroupFieldEntity $field) {
  67.             return $field->getType() === Email::NAME;
  68.         })->first();
  69.         if ($mailField === null) {
  70.             return null;
  71.         }
  72.         return $formData[$mailField->getTechnicalName()] ?? null;
  73.     }
  74. }