custom/plugins/MoorlFormBuilder/src/Subscriber/MailSendSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFormBuilder\Subscriber;
  3. use MoorlFormBuilder\MoorlFormBuilder;
  4. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  5. use Shopware\Core\Content\MailTemplate\Exception\MailEventConfigurationException;
  6. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  7. use Shopware\Core\Content\Media\MediaService;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Symfony\Contracts\EventDispatcher\Event;
  13. use Shopware\Core\Framework\Event\EventData\EventDataType;
  14. use Shopware\Core\Framework\Event\MailAware;
  15. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class MailSendSubscriber implements EventSubscriberInterface
  18. {
  19.     public const ACTION_NAME MoorlFormBuilder::MAIL_TEMPLATE_MAIL_SEND_ACTION;
  20.     public const MAIL_TEMPLATE_TYPE 'moorl_form_builder_cms';
  21.     private AbstractMailService $mailService;
  22.     private EntityRepositoryInterface $mailTemplateRepository;
  23.     private MediaService $mediaService;
  24.     public function __construct(
  25.         AbstractMailService $mailService,
  26.         EntityRepositoryInterface $mailTemplateRepository,
  27.         MediaService $mediaService
  28.     )
  29.     {
  30.         $this->mailService $mailService;
  31.         $this->mailTemplateRepository $mailTemplateRepository;
  32.         $this->mediaService $mediaService;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             self::ACTION_NAME => 'sendMail',
  38.         ];
  39.     }
  40.     /**
  41.      * @throws MailEventConfigurationException
  42.      */
  43.     public function sendMail(/*MailAware*/ $event): void
  44.     {
  45.         $form $event->getForm();
  46.         if (!$form) {
  47.             return;
  48.         }
  49.         $mailTemplate $this->getMailTemplate($form->getMailTemplateId(), $event->getContext());
  50.         if (!$mailTemplate) {
  51.             return;
  52.         }
  53.         $data = new DataBag();
  54.         $data->set('recipients'$event->getMailStruct()->getRecipients());
  55.         if ($form->getReplyTo()) {
  56.             $data->set('replyTo'$form->getReplyTo());
  57.         }
  58.         $data->set('senderName'$mailTemplate->getTranslation('senderName'));
  59.         $data->set('salesChannelId'$event->getSalesChannelId());
  60.         $data->set('templateId'$mailTemplate->getId());
  61.         $data->set('customFields'$mailTemplate->getCustomFields());
  62.         $data->set('contentHtml'$mailTemplate->getTranslation('contentHtml'));
  63.         $data->set('contentPlain'$mailTemplate->getTranslation('contentPlain'));
  64.         $data->set('subject'$mailTemplate->getTranslation('subject'));
  65.         $data->set('mediaIds', []);
  66.         $attachments = [];
  67.         if ($event->getMedias()) {
  68.             foreach ($event->getMedias() as $mailEventMedia) {
  69.                 if (!$mailEventMedia) {
  70.                     continue;
  71.                 }
  72.                 $fileName mb_substr($mailEventMedia->getFileName(), 33);
  73.                 $updatedMedia = clone $mailEventMedia;
  74.                 $updatedMedia->setFileName($fileName);
  75.                 $attachments[] = $this->mediaService->getAttachment(
  76.                     $updatedMedia,
  77.                     $event->getContext()
  78.                 );
  79.             }
  80.         } else {
  81.             $attachments array_merge($attachments$form->getBinAttachments());
  82.         }
  83.         if ($mailTemplate->getMedia()) {
  84.             foreach ($mailTemplate->getMedia() as $mailTemplateMedia) {
  85.                 if (!$mailTemplateMedia->getMedia()) {
  86.                     continue;
  87.                 }
  88.                 if ($mailTemplateMedia->getLanguageId()  && $mailTemplateMedia->getLanguageId() !== $event->getContext()->getLanguageId()) {
  89.                     continue;
  90.                 }
  91.                 $attachments[] = $this->mediaService->getAttachment(
  92.                     $mailTemplateMedia->getMedia(),
  93.                     $event->getContext()
  94.                 );
  95.             }
  96.         }
  97.         if (!empty($attachments)) {
  98.             $data->set('binAttachments'$attachments);
  99.         }
  100.         $this->mailService->send(
  101.             $data->all(),
  102.             $event->getContext(),
  103.             $this->getTemplateData($event)
  104.         );
  105.     }
  106.     private function getMailTemplate(?string $mailTemplateIdContext $context): MailTemplateEntity
  107.     {
  108.         $mailTemplate null;
  109.         if ($mailTemplateId) {
  110.             $criteria = new Criteria([$mailTemplateId]);
  111.             $criteria->addAssociation('media.media');
  112.             $criteria->setLimit(1);
  113.             $mailTemplate $this->mailTemplateRepository->search($criteria$context)->first();
  114.         }
  115.         if (!$mailTemplate) {
  116.             $criteria = new Criteria();
  117.             $criteria->addAssociation('media.media');
  118.             $criteria->setLimit(1);
  119.             $criteria->addFilter(new EqualsFilter('mailTemplateType.technicalName'self::MAIL_TEMPLATE_TYPE));
  120.             $mailTemplate $this->mailTemplateRepository->search($criteria$context)->first();
  121.         }
  122.         return $mailTemplate;
  123.     }
  124.     /**
  125.      * @throws MailEventConfigurationException
  126.      */
  127.     private function getTemplateData(/*MailAware*/ $event): array
  128.     {
  129.         $data = [];
  130.         /* @var EventDataType $item */
  131.         foreach (array_keys($event::getAvailableData()->toArray()) as $key) {
  132.             $getter 'get' ucfirst($key);
  133.             if (method_exists($event$getter)) {
  134.                 $data[$key] = $event->$getter();
  135.             } else {
  136.                 throw new MailEventConfigurationException('Data for ' $key ' not available.'get_class($event));
  137.             }
  138.         }
  139.         return $data;
  140.     }
  141. }