Mailjet REST API Client
mailjet-rest is the official Python wrapper for the Mailjet Email API (v3 and v3.1). It simplifies sending transactional and marketing emails, managing contacts, and retrieving statistics. The library is currently at version 1.5.1 and receives regular updates and maintenance.
Warnings
- breaking Support for Python 3.9 was dropped in mailjet-rest v1.5.0. Ensure your environment uses Python 3.10 or newer for full compatibility and continued updates.
- gotcha Prior to v1.5.0, users experienced `csvimport error 'List index (0) out of bounds'` when attempting CSV imports. This was fixed in v1.5.0.
- gotcha Incorrect Mailjet API keys (public and private) will lead to `401 Unauthorized` errors. Always double-check your credentials and ensure they are active.
- gotcha Mailjet API PUT requests behave like PATCH requests, meaning they only update the specified properties and do not overwrite the entire resource. Other existing properties will remain unchanged.
- gotcha Mailjet's API has rate limits (e.g., typically 30 calls/second for some endpoints). Exceeding these limits will result in `429 Too Many Requests` errors.
- gotcha The `Recipients` property in the email payload sends a separate, individualized message to each recipient without revealing other recipients. In contrast, `To`, `Cc`, and `Bcc` will show other recipients as expected in a standard email client. Mixing `Recipients` with `To`, `Cc`, or `Bcc` is not allowed.
Install
-
pip install mailjet-rest
Imports
- Client
from mailjet_rest import Client
Quickstart
import os
from mailjet_rest import Client
# Mailjet API keys can be found at https://app.mailjet.com/account/api_keys
# It's highly recommended to store these in environment variables.
api_key = os.environ.get('MJ_APIKEY_PUBLIC', 'your_public_api_key_here')
api_secret = os.environ.get('MJ_APIKEY_PRIVATE', 'your_private_api_secret_here')
# Initialize the Mailjet client, specifying API version v3.1 for the Send API.
mailjet = Client(auth=(api_key, api_secret), version='v3.1')
# Prepare the email data
data = {
'Messages': [
{
"From": {
"Email": "pilot@mailjet.com", # Must be a verified sender in your Mailjet account
"Name": "Mailjet Pilot"
},
"To": [
{
"Email": "passenger@mailjet.com",
"Name": "Passenger 1"
}
],
"Subject": "Your email flight plan!",
"TextPart": "Dear passenger, welcome to Mailjet! May the delivery force be with you!",
"HTMLPart": "<h3>Dear passenger, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"
}
]
}
# Send the email
result = mailjet.send.create(data=data)
# Print the response status and content
print(f"Status Code: {result.status_code}")
print(f"Response: {result.json()}")
# Check for common API errors
if result.status_code >= 400:
print(f"Error details: {result.json()}")