'Use X-Mailgun-Variables with html template field in Mailgun API
I'm using the Mailgun API to send an email. I'm providing my own HTML as the template in the request as opposed to using the UI or API to send the template.
I'd like to inject my variables in the email but the variables are not being passed on. Do I have to upload the template to Mailgun first? As opposed to sending on the same POST request.
All the examples I see are using "template" with the variables but none with "html".
THIS IS THE TEMPLATE {{my_var}}
Python request
requests.post("https://api.mailgun.net/v3/XXX.mailgun.org/messages",
auth=("api", "XXX"),
data={"from": "Auto Send <[email protected]>",
"to": ["[email protected]"],
"html": "<html>THIS IS THE TEMPLATE {{my_var}}</html>",
"subject": "XXX",
"text": "hello this is text",
"h:X-Mailgun-Variables": json.dumps({"my_var": "SOME NAME"})})
I've read:
Solution 1:[1]
Here's a solution provided from the support at Mailgun:
return requests.post(
f"https://api.mailgun.net/v3/{MY_DOMAIN}/messages",
auth=("api", API_KEY),
data={"from": f"Excited User <admin@{MY_DOMAIN}>",
"to": f"{RECIPIENT}",
"subject": "Mailgun Test Message",
"text": "Testing some Mailgun awesomness!",
"html": f"{file_content}",
"recipient-variables": ('"[email protected]": {"first":"Bob", "id":1}, '
'"[email protected]": {"first":"Alice", "id": 2}}')})
This is a great option but I'll be going with uploading the HTML directly to Mailgun and referencing the template with "template" field.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | engineer-x |
