Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams
Created July 16, 2020 15:19
Show Gist options
  • Save mrchrisadams/3e3f4134a5f89892f6668aca325e0cc8 to your computer and use it in GitHub Desktop.
Save mrchrisadams/3e3f4134a5f89892f6668aca325e0cc8 to your computer and use it in GitHub Desktop.
<html>
<body>
<h1>Hello {{var:first_name:""}},</h1>
<p>
Welcome to Mailjet. This is an example of a templating
language message. This message contains variables and
nested loops (for .. endfor)! You could also embed
conditions (if .. else ..).
</p>
<ul>
{% for rock_band in var:rock_bands %}
<li>
Title: {{ rock_band.name }}
<ul>
{% for member in rock_band.members %}
<li>Member name: {{ member }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</body>
</html>
"""
This call sends a message to the given recipient with vars and custom vars.
"""
from mailjet_rest import Client
import os
import datetime
now = datetime.datetime.now()
api_key = os.environ["MJ_APIKEY_PUBLIC"]
api_secret = os.environ["MJ_APIKEY_PRIVATE"]
mailjet = Client(auth=(api_key, api_secret), version="v3.1")
data = {
"Messages": [
{
"From": {"Email": "ian@somewebsite", "Name": "Ian via Mailjet"},
"To": [
{"Email": "chris@productscience.net", "Name": "Chris Mailjet Test"}
],
"TemplateID": 1575107,
"TemplateLanguage": True,
"TemplateErrorReporting": {"Email":"chris@productscience.net"},
"Subject": f"A sample mailjet passport email at {str(now)}",
"Data": {},
"Variables": {
"rock_bands": [
{"name": "Example name", "members": ["Chris", "Joe", "Jane"]}
]
}
}
]
}
result = mailjet.send.create(data=data)
print(result.status_code)
# Drop into ipdb to see if there are clues there.
# import ipdb ; ipdb.set_trace()
print(result.json())
#
print(result.json()['Messages'][0]['To'][0]['MessageHref'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment