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

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