createAttachment example

$mpdfConfig = Shopware()->Container()->getParameter('shopware.mpdf.defaultConfig');
        $mpdf = new Mpdf($mpdfConfig);
        $mpdf->WriteHTML($data);
        $pdfFileContent = $mpdf->Output('', 'S');

        if ($pdfFileContent === false) {
            throw new Enlight_Exception('Could not generate SEPA attachment file');
        }

        $attachmentName = 'SEPA_' . $orderNumber;

        $mail->createAttachment(
            $pdfFileContent,
            'application/pdf',
            Zend_Mime::DISPOSITION_ATTACHMENT,
            Zend_Mime::ENCODING_BASE64,
            $attachmentName . '.pdf'
        );

        $mail->setAssociation(LogEntryBuilder::ORDER_NUMBER_ASSOCIATION, $orderNumber);

        try {
            $mail->send();
        }
foreach ($mailModel->getAttachments() as $attachment) {
            if ($attachment->getShopId() !== null
                && ($this->getShop() === null || $attachment->getShopId() !== $this->getShop()->getId())) {
                continue;
            }

            $mediaService = Shopware()->Container()->get(MediaServiceInterface::class);
            if (!$mediaService->has($attachment->getPath())) {
                Shopware()->Container()->get('corelogger')->error('Could not load file: ' . $attachment->getPath());
            } else {
                $fileAttachment = $mail->createAttachment($mediaService->read($attachment->getPath()));
                $fileAttachment->filename = $attachment->getFileName();
            }
        }

        $mail->setTemplateName($mailModel->getName());

        if ($this->getShop() !== null) {
            $mail->setAssociation(LogEntryBuilder::SHOP_ID_ASSOCIATION, $this->getShop()->getId());
        }

        return $mail;
    }

        $filesystem = $this->container->get('shopware.filesystem.private');

        foreach ($attachments as $attachment) {
            $filePath = sprintf('documents/%s.pdf', $attachment['hash']);
            $fileName = $this->getFileName($orderId(int) $attachment['type'][0]['id']);

            if ($filesystem->has($filePath) === false) {
                continue;
            }

            $mail->addAttachment($this->createAttachment($filePath$fileName));
        }

        return $mail;
    }

    /** * Creates an attachment by a file path. */
    private function createAttachment(string $filePath, string $fileName): Zend_Mime_Part
    {
        $content = $this->container->get('shopware.filesystem.private')->read($filePath);
        
return;
        }

        foreach ($logEntry->getDocuments() as $document) {
            $filePath = sprintf('documents/%s.pdf', $document->getHash());
            $fileName = sprintf('%s.pdf', $document->getType()->getName());

            if (!$this->filesystem->has($filePath)) {
                continue;
            }

            $fileAttachment = $mail->createAttachment(
                $this->filesystem->read($filePath)
            );
            $fileAttachment->filename = $fileName;
        }
    }

    protected function assignTemplateDocuments(Log $logEntry, Enlight_Components_Mail $mail): void
    {
        if ($logEntry->getType() === null || empty($logEntry->getType()->getAttachments())) {
            return;
        }

        
Home | Imprint | This part of the site doesn't use cookies.