vendor/shopware/core/Content/Flow/Dispatching/Action/SendMailAction.php line 123

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching\Action;
  3. use Doctrine\DBAL\Connection;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Checkout\Document\DocumentService;
  6. use Shopware\Core\Content\ContactForm\Event\ContactFormEvent;
  7. use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
  8. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  9. use Shopware\Core\Content\MailTemplate\Exception\MailEventConfigurationException;
  10. use Shopware\Core\Content\MailTemplate\Exception\SalesChannelNotFoundException;
  11. use Shopware\Core\Content\MailTemplate\MailTemplateActions;
  12. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  13. use Shopware\Core\Content\MailTemplate\Subscriber\MailSendSubscriberConfig;
  14. use Shopware\Core\Content\Media\MediaService;
  15. use Shopware\Core\Framework\Adapter\Translation\Translator;
  16. use Shopware\Core\Framework\Context;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\FetchModeHelper;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  19. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  22. use Shopware\Core\Framework\Event\DelayAware;
  23. use Shopware\Core\Framework\Event\FlowEvent;
  24. use Shopware\Core\Framework\Event\MailAware;
  25. use Shopware\Core\Framework\Event\OrderAware;
  26. use Shopware\Core\Framework\Uuid\Uuid;
  27. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  28. use Shopware\Core\System\Locale\LanguageLocaleCodeProvider;
  29. use Symfony\Contracts\EventDispatcher\Event;
  30. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  31. class SendMailAction extends FlowAction
  32. {
  33.     public const ACTION_NAME = MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION;
  34.     public const MAIL_CONFIG_EXTENSION = 'mail-attachments';
  35.     private const RECIPIENT_CONFIG_ADMIN = 'admin';
  36.     private const RECIPIENT_CONFIG_CUSTOM = 'custom';
  37.     private const RECIPIENT_CONFIG_CONTACT_FORM_MAIL = 'contactFormMail';
  38.     private EntityRepositoryInterface $mailTemplateRepository;
  39.     private MediaService $mediaService;
  40.     private EntityRepositoryInterface $mediaRepository;
  41.     private DocumentService $documentService;
  42.     private EntityRepositoryInterface $documentRepository;
  43.     private LoggerInterface $logger;
  44.     private AbstractMailService $emailService;
  45.     private EventDispatcherInterface $eventDispatcher;
  46.     private EntityRepositoryInterface $mailTemplateTypeRepository;
  47.     private Translator $translator;
  48.     private Connection $connection;
  49.     private LanguageLocaleCodeProvider $languageLocaleProvider;
  50.     private bool $updateMailTemplate;
  51.     /**
  52.      * @internal
  53.      */
  54.     public function __construct(
  55.         AbstractMailService $emailService,
  56.         EntityRepositoryInterface $mailTemplateRepository,
  57.         MediaService $mediaService,
  58.         EntityRepositoryInterface $mediaRepository,
  59.         EntityRepositoryInterface $documentRepository,
  60.         DocumentService $documentService,
  61.         LoggerInterface $logger,
  62.         EventDispatcherInterface $eventDispatcher,
  63.         EntityRepositoryInterface $mailTemplateTypeRepository,
  64.         Translator $translator,
  65.         Connection $connection,
  66.         LanguageLocaleCodeProvider $languageLocaleProvider,
  67.         bool $updateMailTemplate
  68.     ) {
  69.         $this->mailTemplateRepository = $mailTemplateRepository;
  70.         $this->mediaService = $mediaService;
  71.         $this->mediaRepository = $mediaRepository;
  72.         $this->documentRepository = $documentRepository;
  73.         $this->documentService = $documentService;
  74.         $this->logger = $logger;
  75.         $this->emailService = $emailService;
  76.         $this->eventDispatcher = $eventDispatcher;
  77.         $this->mailTemplateTypeRepository = $mailTemplateTypeRepository;
  78.         $this->translator = $translator;
  79.         $this->connection = $connection;
  80.         $this->languageLocaleProvider = $languageLocaleProvider;
  81.         $this->updateMailTemplate = $updateMailTemplate;
  82.     }
  83.     public static function getName(): string
  84.     {
  85.         return 'action.mail.send';
  86.     }
  87.     public static function getSubscribedEvents(): array
  88.     {
  89.         return [
  90.             self::getName() => 'handle',
  91.         ];
  92.     }
  93.     public function requirements(): array
  94.     {
  95.         return [MailAware::class, DelayAware::class];
  96.     }
  97.     /**
  98.      * @throws MailEventConfigurationException
  99.      * @throws SalesChannelNotFoundException
  100.      * @throws InconsistentCriteriaIdsException
  101.      */
  102.     public function handle(Event $event): void
  103.     {
  104.         if (!$event instanceof FlowEvent) {
  105.             return;
  106.         }
  107.         $mailEvent = $event->getEvent();
  108.         $extension = $event->getContext()->getExtension(self::MAIL_CONFIG_EXTENSION);
  109.         if (!$extension instanceof MailSendSubscriberConfig) {
  110.             $extension = new MailSendSubscriberConfig(false, [], []);
  111.         }
  112.         if ($extension->skip()) {
  113.             return;
  114.         }
  115.         if (!$mailEvent instanceof MailAware) {
  116.             throw new MailEventConfigurationException('Not an instance of MailAware', \get_class($mailEvent));
  117.         }
  118.         $eventConfig = $event->getConfig();
  119.         if (empty($eventConfig['recipient'])) {
  120.             throw new MailEventConfigurationException('The recipient value in the flow action configuration is missing.', \get_class($mailEvent));
  121.         }
  122.         if (!isset($eventConfig['mailTemplateId'])) {
  123.             return;
  124.         }
  125.         $mailTemplate = $this->getMailTemplate($eventConfig['mailTemplateId'], $event->getContext());
  126.         if ($mailTemplate === null) {
  127.             return;
  128.         }
  129.         $injectedTranslator = $this->injectTranslator($mailEvent);
  130.         $data = new DataBag();
  131.         $recipients = $this->getRecipients($eventConfig['recipient'], $mailEvent);
  132.         if (empty($recipients)) {
  133.             return;
  134.         }
  135.         $data->set('recipients', $recipients);
  136.         $data->set('senderName', $mailTemplate->getTranslation('senderName'));
  137.         $data->set('salesChannelId', $mailEvent->getSalesChannelId());
  138.         $data->set('templateId', $mailTemplate->getId());
  139.         $data->set('customFields', $mailTemplate->getCustomFields());
  140.         $data->set('contentHtml', $mailTemplate->getTranslation('contentHtml'));
  141.         $data->set('contentPlain', $mailTemplate->getTranslation('contentPlain'));
  142.         $data->set('subject', $mailTemplate->getTranslation('subject'));
  143.         $data->set('mediaIds', []);
  144.         $attachments = array_unique($this->buildAttachments($mailEvent, $mailTemplate, $extension, $eventConfig), \SORT_REGULAR);
  145.         if (!empty($attachments)) {
  146.             $data->set('binAttachments', $attachments);
  147.         }
  148.         $this->eventDispatcher->dispatch(new FlowSendMailActionEvent($data, $mailTemplate, $event));
  149.         if ($data->has('templateId')) {
  150.             $this->updateMailTemplateType($event, $mailEvent, $mailTemplate);
  151.         }
  152.         try {
  153.             $this->emailService->send(
  154.                 $data->all(),
  155.                 $event->getContext(),
  156.                 $this->getTemplateData($mailEvent)
  157.             );
  158.             $writes = array_map(static function ($id) {
  159.                 return ['id' => $id, 'sent' => true];
  160.             }, array_column($attachments, 'id'));
  161.             if (!empty($writes)) {
  162.                 $this->documentRepository->update($writes, $event->getContext());
  163.             }
  164.         } catch (\Exception $e) {
  165.             $this->logger->error(
  166.                 "Could not send mail:\n"
  167.                 . $e->getMessage() . "\n"
  168.                 . 'Error Code:' . $e->getCode() . "\n"
  169.                 . "Template data: \n"
  170.                 . json_encode($data->all()) . "\n"
  171.             );
  172.         }
  173.         if ($injectedTranslator) {
  174.             $this->translator->resetInjection();
  175.         }
  176.     }
  177.     private function updateMailTemplateType(FlowEvent $event, MailAware $mailAware, MailTemplateEntity $mailTemplate): void
  178.     {
  179.         if (!$mailTemplate->getMailTemplateTypeId()) {
  180.             return;
  181.         }
  182.         if (!$this->updateMailTemplate) {
  183.             return;
  184.         }
  185.         $mailTemplateTypeTranslation = $this->connection->fetchOne(
  186.             'SELECT 1 FROM mail_template_type_translation WHERE language_id = :languageId AND mail_template_type_id =:mailTemplateTypeId',
  187.             [
  188.                 'languageId' => Uuid::fromHexToBytes($event->getContext()->getLanguageId()),
  189.                 'mailTemplateTypeId' => Uuid::fromHexToBytes($mailTemplate->getMailTemplateTypeId()),
  190.             ]
  191.         );
  192.         if (!$mailTemplateTypeTranslation) {
  193.             // Don't throw errors if this fails // Fix with NEXT-15475
  194.             $this->logger->error(
  195.                 "Could not update mail template type, because translation for this language does not exits:\n"
  196.                 . 'Flow id: ' . $event->getFlowState()->flowId . "\n"
  197.                 . 'Sequence id: ' . $event->getFlowState()->getSequenceId()
  198.             );
  199.             return;
  200.         }
  201.         $this->mailTemplateTypeRepository->update([[
  202.             'id' => $mailTemplate->getMailTemplateTypeId(),
  203.             'templateData' => $this->getTemplateData($mailAware),
  204.         ]], $mailAware->getContext());
  205.     }
  206.     private function getMailTemplate(string $id, Context $context): ?MailTemplateEntity
  207.     {
  208.         $criteria = new Criteria([$id]);
  209.         $criteria->setTitle('send-mail::load-mail-template');
  210.         $criteria->addAssociation('media.media');
  211.         $criteria->setLimit(1);
  212.         return $this->mailTemplateRepository
  213.             ->search($criteria, $context)
  214.             ->first();
  215.     }
  216.     /**
  217.      * @throws MailEventConfigurationException
  218.      */
  219.     private function getTemplateData(MailAware $event): array
  220.     {
  221.         $data = [];
  222.         foreach (array_keys($event::getAvailableData()->toArray()) as $key) {
  223.             $getter = 'get' . ucfirst($key);
  224.             if (!method_exists($event, $getter)) {
  225.                 throw new MailEventConfigurationException('Data for ' . $key . ' not available.', \get_class($event));
  226.             }
  227.             $data[$key] = $event->$getter();
  228.         }
  229.         return $data;
  230.     }
  231.     private function buildAttachments(MailAware $mailEvent, MailTemplateEntity $mailTemplate, MailSendSubscriberConfig $extensions, array $eventConfig): array
  232.     {
  233.         $attachments = [];
  234.         if ($mailTemplate->getMedia() !== null) {
  235.             foreach ($mailTemplate->getMedia() as $mailTemplateMedia) {
  236.                 if ($mailTemplateMedia->getMedia() === null) {
  237.                     continue;
  238.                 }
  239.                 if ($mailTemplateMedia->getLanguageId() !== null && $mailTemplateMedia->getLanguageId() !== $mailEvent->getContext()->getLanguageId()) {
  240.                     continue;
  241.                 }
  242.                 $attachments[] = $this->mediaService->getAttachment(
  243.                     $mailTemplateMedia->getMedia(),
  244.                     $mailEvent->getContext()
  245.                 );
  246.             }
  247.         }
  248.         if (!empty($extensions->getMediaIds())) {
  249.             $criteria = new Criteria($extensions->getMediaIds());
  250.             $criteria->setTitle('send-mail::load-media');
  251.             $entities = $this->mediaRepository->search($criteria, $mailEvent->getContext());
  252.             foreach ($entities as $media) {
  253.                 $attachments[] = $this->mediaService->getAttachment($media, $mailEvent->getContext());
  254.             }
  255.         }
  256.         if (!empty($extensions->getDocumentIds())) {
  257.             $attachments = $this->buildOrderAttachments($extensions->getDocumentIds(), $attachments, $mailEvent->getContext());
  258.         }
  259.         if (empty($eventConfig['documentTypeIds']) || !\is_array($eventConfig['documentTypeIds']) || !$mailEvent instanceof OrderAware) {
  260.             return $attachments;
  261.         }
  262.         return $this->buildFlowSettingAttachments($mailEvent->getOrderId(), $eventConfig['documentTypeIds'], $attachments, $mailEvent->getContext());
  263.     }
  264.     private function injectTranslator(MailAware $event): bool
  265.     {
  266.         if ($event->getSalesChannelId() === null) {
  267.             return false;
  268.         }
  269.         if ($this->translator->getSnippetSetId() !== null) {
  270.             return false;
  271.         }
  272.         $this->translator->injectSettings(
  273.             $event->getSalesChannelId(),
  274.             $event->getContext()->getLanguageId(),
  275.             $this->languageLocaleProvider->getLocaleForLanguageId($event->getContext()->getLanguageId()),
  276.             $event->getContext()
  277.         );
  278.         return true;
  279.     }
  280.     private function getRecipients(array $recipients, MailAware $mailEvent): array
  281.     {
  282.         switch ($recipients['type']) {
  283.             case self::RECIPIENT_CONFIG_CUSTOM:
  284.                 return $recipients['data'];
  285.             case self::RECIPIENT_CONFIG_ADMIN:
  286.                 $admins = $this->connection->fetchAllAssociative(
  287.                     'SELECT first_name, last_name, email FROM user WHERE admin = true'
  288.                 );
  289.                 $emails = [];
  290.                 foreach ($admins as $admin) {
  291.                     $emails[$admin['email']] = $admin['first_name'] . ' ' . $admin['last_name'];
  292.                 }
  293.                 return $emails;
  294.             case self::RECIPIENT_CONFIG_CONTACT_FORM_MAIL:
  295.                 if (!$mailEvent instanceof ContactFormEvent) {
  296.                     return [];
  297.                 }
  298.                 $data = $mailEvent->getContactFormData();
  299.                 if (!\array_key_exists('email', $data)) {
  300.                     return [];
  301.                 }
  302.                 return [$data['email'] => ($data['firstName'] ?? '') . ' ' . ($data['lastName'] ?? '')];
  303.             default:
  304.                 return $mailEvent->getMailStruct()->getRecipients();
  305.         }
  306.     }
  307.     private function buildOrderAttachments(array $documentIds, array $attachments, Context $context): array
  308.     {
  309.         $criteria = new Criteria($documentIds);
  310.         $criteria->setTitle('send-mail::load-attachments');
  311.         $criteria->addAssociation('documentMediaFile');
  312.         $criteria->addAssociation('documentType');
  313.         $entities = $this->documentRepository->search($criteria, $context);
  314.         return $this->mappingAttachmentsInfo($entities, $attachments, $context);
  315.     }
  316.     private function buildFlowSettingAttachments(string $orderId, array $documentTypeIds, array $attachments, Context $context): array
  317.     {
  318.         $documents = $this->connection->fetchAllAssociative(
  319.             'SELECT
  320.                 LOWER(hex(`document`.`document_type_id`)) as doc_type,
  321.                 LOWER(hex(`document`.`id`)) as doc_id,
  322.                 `document`.`created_at` as newest_date
  323.             FROM
  324.                 `document`
  325.             WHERE
  326.                 HEX(`document`.`order_id`) = :orderId
  327.                 AND HEX(`document`.`document_type_id`) IN (:documentTypeIds)
  328.             ORDER BY `document`.`created_at` DESC',
  329.             [
  330.                 'orderId' => $orderId,
  331.                 'documentTypeIds' => $documentTypeIds,
  332.             ],
  333.             [
  334.                 'documentTypeIds' => Connection::PARAM_STR_ARRAY,
  335.             ]
  336.         );
  337.         $documentsGroupByType = FetchModeHelper::group($documents);
  338.         foreach ($documentsGroupByType as $document) {
  339.             $documentIds[] = array_shift($document)['doc_id'];
  340.         }
  341.         if (empty($documentIds)) {
  342.             return $attachments;
  343.         }
  344.         $criteria = new Criteria($documentIds);
  345.         $criteria->setTitle('send-mail::load-flow-documents');
  346.         $criteria->addAssociations(['documentMediaFile', 'documentType']);
  347.         $entities = $this->documentRepository->search($criteria, $context);
  348.         return $this->mappingAttachmentsInfo($entities, $attachments, $context);
  349.     }
  350.     private function mappingAttachmentsInfo(EntityCollection $entities, array $attachments, Context $context): array
  351.     {
  352.         foreach ($entities as $document) {
  353.             $documentId = $document->getId();
  354.             $document = $this->documentService->getDocument($document, $context);
  355.             $attachments[] = [
  356.                 'id' => $documentId,
  357.                 'content' => $document->getFileBlob(),
  358.                 'fileName' => $document->getFilename(),
  359.                 'mimeType' => $document->getContentType(),
  360.             ];
  361.         }
  362.         return $attachments;
  363.     }
  364. }