Shopware 6

Shopware 6 API: Automating Document Dispatch and 'Sent' Status Without Custom Plugins

Screenshot of Shopware 6 admin showing an order document with a 'Sent' status, confirming successful dispatch.
Screenshot of Shopware 6 admin showing an order document with a 'Sent' status, confirming successful dispatch.

Streamlining Your Shopware 6 Operations: Automated Document Dispatch via API

In the fast-paced world of e-commerce, efficiency is paramount. For businesses leveraging Shopware 6, particularly those integrating external systems like ERPs or CRMs via API, the automated handling of order documents—invoices, delivery notes, credit memos—is not just a convenience, but a necessity. Manually sending documents or verifying their 'sent' status can quickly become a bottleneck, leading to delays, errors, and increased operational costs. This is where intelligent automation steps in.

At Migrate My Store, we frequently encounter scenarios where merchants and developers seek seamless solutions for document management post-migration or during ongoing operations. A common challenge, recently highlighted in the Shopware community forum, revolves around ensuring documents created or uploaded via the Shopware API are automatically dispatched to customers and correctly marked as 'sent' within the Shopware administration.

The Challenge: API-Generated Documents Lacking Automated Dispatch and 'Sent' Status

Imagine a scenario where your external system creates an invoice for a Shopware order via the API. While the document is successfully generated and stored in Shopware, it remains in a 'not sent' state. This means customers don't receive it automatically, and your team has to manually intervene to send each document and update its status. This was precisely the issue raised by a Shopware user, c.rosenberger, who was working with Shopware version 6.7.13.1 and noted that the V2 DocumentGenerator was not yet fully applicable for their needs.

The core problem was clear: after API creation or upload, documents were neither automatically emailed to the customer nor marked with the crucial 'sent' status in the Shopware backend. The natural question that arose was whether a custom plugin or a DocumentGenerator Decorator was the only viable, albeit complex, solution.

The Elegant Solution: Leveraging Shopware's Native Mail API

Fortunately, as expert b.david clarified in the forum, there's no need for complex custom plugins or decorators for this specific task. Shopware provides a robust, built-in mechanism for this. The key insight is that Shopware inherently marks documents as 'sent' once they are successfully dispatched as attachments through its internal mail sending mechanism. The trick is to utilize the very same API endpoint that the Shopware administration uses for sending documents.

This means that after your document has been created or uploaded via the API, a subsequent, simple API call can handle the dispatch and status update. This approach ensures consistency, reduces development overhead, and maintains the integrity of your Shopware system.

Illustration of an API request flow with documents and email icons
Figure 1: Visualizing the API flow for automated document creation, attachment, and email dispatch in Shopware 6.

Step-by-Step: Automating Document Dispatch with the Shopware Mail API

The solution involves making a POST request to the /api/_action/mail-template/send endpoint. This powerful endpoint is designed to send emails based on Shopware mail templates, and crucially, it allows you to attach existing documents.

The API Endpoint and Payload:

{
  "mailTemplateId": "",
  "recipients": { "[email protected]": "John Doe" },
  "salesChannelId": "",
  "subject": "Your Order Documents from [Your Shop Name]",
  "senderName": "[Your Shop Name]",
  "contentHtml": "

Dear John Doe,

Please find your order documents attached.

", "contentPlain": "Dear John Doe,\ \ Please find your order documents attached.", "mailTemplateData": { "order": { "id": "" } }, "documentIds": [""] }

Breaking Down the Payload Parameters:

  • mailTemplateId: This is the UUID of the email template you wish to use (e.g., 'Order confirmation', 'Invoice email'). You can retrieve this by searching the /api/search/mail-template endpoint. Using an existing template ensures consistent branding and messaging.
  • recipients: An object containing the customer's email address as the key and their name as the value. This should typically be pulled directly from the order data.
  • salesChannelId: The UUID of the sales channel through which the order was placed. Essential for correct context and template localization.
  • subject, senderName, contentHtml, contentPlain: While you can hardcode these, it's best practice to pre-fetch them from the chosen mailTemplateId via /api/search/mail-template. This ensures your email content is dynamic and consistent with your Shopware settings.
  • mailTemplateData: This is crucial for dynamic content within your email template. For order-related documents, you must pass an object containing the order with its id. This allows the mail template to access order details (like order number, customer name, items) for placeholders.
  • documentIds: An array of UUIDs for the documents you wish to attach. These IDs are returned when you create or upload documents via the Shopware API (e.g., POST /api/document). This is the parameter that tells Shopware which documents to send and subsequently mark as 'sent'.

Best Practices and Important Considerations

To ensure a robust and reliable automated document dispatch system, consider the following:

  • Pre-fetching Mail Template Data: Always retrieve the subject, sender name, and content (HTML and plain) from an existing Shopware mail template. This ensures your emails are consistent with your brand and leverage Shopware's built-in templating capabilities.
  • Error Logging: Keep a close eye on your Shopware logs for entries related to mail.sent.error. Shopware will log template errors or other mail sending issues here without necessarily returning an API error. This is your first line of defense for debugging.
  • Idempotency and Duplicate Sending: Before attempting to send a document, you might want to check its 'sent' status in Shopware. While the API call itself is designed to mark it as sent upon success, preventing redundant calls can be beneficial.
  • API Authentication: Ensure your API calls are properly authenticated using a Shopware API key with the necessary permissions to send emails and access documents.
  • Scalability: For high-volume stores, consider implementing a queueing system for your document dispatch. Instead of making direct API calls for every document, add them to a queue that processes them asynchronously, preventing potential bottlenecks.
  • Shopware Version Compatibility: While this solution was discussed in the context of Shopware 6.7.13.1, the underlying Mail API endpoint has been a stable and fundamental part of Shopware 6 for a considerable time, making this approach reliable across many versions.
Screenshot of Shopware 6 admin showing a document marked as 'sent'
Figure 2: A Shopware 6 administration screenshot highlighting a document successfully marked with the 'sent' status after API dispatch.

Why This Matters for Your E-commerce Migration

For businesses undergoing a migration to Shopware 6, understanding and implementing such automation is critical. A seamless migration isn't just about moving data; it's about establishing efficient, automated workflows that minimize manual intervention post-launch. By integrating automated document dispatch from day one, you ensure a smoother transition, reduce the learning curve for your team, and provide a consistent customer experience.

At Migrate My Store, we specialize in ensuring your Shopware migration is not just successful, but also optimized for modern e-commerce operations. This includes setting up robust API integrations and automation workflows that save you time and money.

Conclusion

Automating the dispatch of API-generated documents in Shopware 6 is a straightforward process that doesn't require complex custom development. By leveraging Shopware's native POST /api/_action/mail-template/send endpoint, you can ensure your invoices, delivery notes, and other critical documents are automatically sent to customers and correctly marked as 'sent' in the backend. This not only streamlines your operations but also enhances customer satisfaction and frees up your team to focus on growth.

Ready to optimize your Shopware store or planning a migration? Visit migrate-my-store.com to learn how our expertise can help you achieve seamless, automated e-commerce success.

Share:

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools