Mailjet REST API Client

1.5.1 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Mailjet client and send a basic email. Ensure your Mailjet API public and private keys are set as environment variables (MJ_APIKEY_PUBLIC, MJ_APIKEY_PRIVATE) or replaced with actual keys. The 'From' email address must be a verified sender in your Mailjet account. The example uses the v3.1 Send API for more robust messaging features.

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()}")

view raw JSON →