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.
pip install mailjet-restVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Python environment to 3.10 or a later supported version.
Upgrade to mailjet-rest v1.5.0 or later to resolve CSV import issues.
Verify your API keys on your Mailjet account dashboard and ensure they are correctly configured in your application.
Be aware that PUT operations are partial updates. Only include the fields you intend to modify in your payload.
Implement retry logic with exponential backoff and consider using batch sending for high volumes of emails to stay within rate limits.
Choose either `Recipients` for individual, hidden messages or `To`/`Cc`/`Bcc` for group visibility, but do not combine them in a single send request.
Verify your API Key and Secret Key in your Mailjet account's API Key Management section, ensure they are active and have the required permissions, then correctly initialize the `mailjet-rest` client with these keys. ```python from mailjet_rest import Client api_key = 'YOUR_MAILJET_API_KEY' # Replace with your actual Public API Key api_secret = 'YOUR_MAILJET_API_SECRET' # Replace with your actual Secret API Key mailjet = Client(auth=(api_key, api_secret), version='v3.1') # Use v3.1 for sending emails ```
Log into your Mailjet account, navigate to the Sender Emails section, verify the email address you intend to use as the sender, and ensure it's included correctly in your API request payload as a verified sender.
```python
# Assuming 'mailjet' client is already initialized
message_data = {
'Messages': [
{
'From': {
'Email': "your_verified_email@example.com", # Must be a verified sender in your Mailjet account
'Name': "Your Name"
},
'To': [
{
'Email': "recipient@example.com",
'Name': "Recipient Name"
}
],
'Subject': "My first Mailjet Email!",
'TextPart': "Greetings from Mailjet!",
'HTMLPart': "<h3>Dear passenger, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"
}
]
}
result = mailjet.send.create(data=message_data)
```Install the `requests` library using pip. If `mailjet-rest` was installed without its dependencies for some reason, reinstall `mailjet-rest` to ensure all its requirements are met. ```bash pip install requests # Or, to ensure mailjet-rest and its dependencies are properly installed/updated pip install --upgrade mailjet-rest ```
Carefully review the official Mailjet API documentation for the specific endpoint you are targeting (e.g., Send API v3.1, Contact management) and ensure your Python dictionary exactly matches the expected JSON structure and parameter requirements, including all mandatory fields and correct data types.
```python
# Corrected example for sending an email (ensure all required fields are present and correct)
# Assuming 'mailjet' client is already initialized
message_data = {
'Messages': [
{
'From': {
'Email': "your_verified_email@example.com",
'Name': "Your Name"
},
'To': [
{
'Email': "recipient@example.com",
'Name': "Recipient Name"
}
],
'Subject': "Your Email Subject", # Required field for email sending
'TextPart': "Your email text content.", # Either TextPart or HTMLPart is required
# 'HTMLPart': "<h3>Your <b>HTML</b> content.</h3>"
}
]
}
result = mailjet.send.create(data=message_data)
```