Search code examples
shopware6shopware6-apishopware6-app

How to use a mail template ID when sending an email with admin api in shopware 6?


When sending an email via Shopware 6 admin api (https://shopware.stoplight.io/docs/admin-api/b3A6MTI2MjUzOTg-send-a-mail) i would like to pass only the mail template ID, and not the other information from the template (contentHtml, contentPlain, subject).

Do you know if this is possible ?


Solution

  • This endpoint was not implemented to fetch an existing mail_template entity by an id. You'll have to provide the mails content yourself.

    Your best bet would be to send a request to the corresponding endpoint for mail templates, e.g. GET /api/mail-template/086f8adc94f14a618e3729d933befb8d, and retrieve the values for subject, contentHtml and contentPlain from the response. These will still be un-rendered Twig at that point. So there should be no problem in passing them with your POST /api/_action/mail-template/send request. Just remember that you have to provide the data for the variables used in the templates. It looks like this isn't documented, but you need to provide the data in the request body with the key mailTemplateData.

    So as an example see this abbreviated request body:

    {
      "contentPlain": "Hello {{ user.firstName }}!", // retrieved by `/api/mail-template/:id`
      "mailTemplateData": {
        "user": {
          "firstName": "Pete"
        }
      }
    }