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

Viewed 51
1 Answers

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"
    }
  }
}
Related