custom/plugins/SwagCmsExtensions/src/Form/Action/FormMailSubscriber.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\CmsExtensions\Form\Action;
  8. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldCollection;
  11. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldEntity;
  12. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\Type\Email;
  13. use Swag\CmsExtensions\Form\Event\CustomFormEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class FormMailSubscriber implements EventSubscriberInterface
  16. {
  17.     private SystemConfigService $systemConfigService;
  18.     private AbstractMailService $mailService;
  19.     public function __construct(SystemConfigService $systemConfigServiceAbstractMailService $mailService)
  20.     {
  21.         $this->systemConfigService $systemConfigService;
  22.         $this->mailService $mailService;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             CustomFormEvent::EVENT_NAME => 'sendMail',
  28.         ];
  29.     }
  30.     public function sendMail(CustomFormEvent $event): void
  31.     {
  32.         $receivers $event->getForm()->getReceivers();
  33.         if (empty($receivers)) {
  34.             $receivers[] = $this->systemConfigService->get('core.basicInformation.email'$event->getSalesChannelId());
  35.         }
  36.         $groups $event->getForm()->getGroups();
  37.         $fields $groups === null ? new FormGroupFieldCollection() : $groups->getFields();
  38.         $data = [];
  39.         $mailTemplate $event->getForm()->getMailTemplate();
  40.         if ($mailTemplate) {
  41.             $data $mailTemplate->jsonSerialize();
  42.             $data['subject'] = $mailTemplate->getTranslation('subject');
  43.             $data['senderName'] = $mailTemplate->getTranslation('senderName');
  44.             $data['contentHtml'] = $mailTemplate->getTranslation('contentHtml');
  45.             $data['contentPlain'] = $mailTemplate->getTranslation('contentPlain');
  46.         }
  47.         $data['salesChannelId'] = $event->getSalesChannelId();
  48.         if ($sender $this->getSenderMail($fields$event->getFormData())) {
  49.             $data['replyTo'] = $sender;
  50.         }
  51.         $templateData = [
  52.             'form' => $event->getForm(),
  53.             'fields' => $fields,
  54.             'formData' => $event->getFormData(),
  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.     /**
  65.      * @param array<string, string|null> $formData
  66.      */
  67.     private function getSenderMail(FormGroupFieldCollection $fieldCollection, array $formData): ?string
  68.     {
  69.         $mailField $fieldCollection->filter(static fn (FormGroupFieldEntity $field) => $field->getType() === Email::NAME)->first();
  70.         if ($mailField === null) {
  71.             return null;
  72.         }
  73.         return $formData[$mailField->getTechnicalName()] ?? null;
  74.     }
  75. }