This directory contains Python implementations of Mandrill API use cases using the mailchimp_transactional library.
- Python 3.7 or higher
- A Mandrill API key from Mailchimp
- Install the required Python packages:
pip3 install -r requirements.txtOr install packages individually:
pip3 install mailchimp-transactional python-dotenv- Copy the
env.examplefile to.env:
cp env.example .env- Edit the
.envfile and add your credentials:
MANDRILL_API_KEY=your_mandrill_api_key_here
DEFAULT_FROM_EMAIL=sender@yourdomain.com
DEFAULT_FROM_NAME="Your Name"
DEFAULT_TO_EMAIL=recipient@example.com
DEFAULT_TO_NAME="Recipient Name"Note: Always quote values that contain spaces or special characters.
File: email_with_single_recipient.py
Demonstrates how to send a basic email to a single recipient.
python email_with_single_recipient.pyFeatures:
- Basic email sending
- HTML and plain text content
- Custom headers
- Advanced options with tracking and metadata
File: email_with_merge_tags.py
Personalize emails using merge tags for dynamic content.
python email_with_merge_tags.pyFeatures:
- Global merge variables (apply to all recipients)
- Recipient-specific merge variables
- Handlebars template syntax
- Multiple recipient personalization
File: email_with_template.py
Send emails using pre-created Mandrill templates.
python email_with_template.pyFeatures:
- Use stored templates
- Override template defaults
- Dynamic content with merge tags
- mc:edit region replacement
Note: You must first create a template using create_template.py or via the Mandrill UI.
File: email_with_attachments.py
Attach files (PDF, CSV, JSON, etc.) to your emails.
python email_with_attachments.pyFeatures:
- Attach local files (PDF, images, etc.)
- Create dynamic attachments (CSV, JSON, text)
- Base64 encoding handling
- Multiple attachment types
File: create_template.py
Create and manage reusable email templates.
python create_template.pyFeatures:
- Create new templates (if not already available)
- List all templates
- Get template information
- Update and delete templates
- mc:edit regions for dynamic content
File: kitchen_sink_email.py
Comprehensive example demonstrating all Mandrill features.
python kitchen_sink_email.pyFeatures:
- All message options in one example
- Attachments, tracking, metadata
- Multiple recipient types (TO, CC, BCC)
- Scheduled sending
- Advanced headers and custom fields
Each Python script follows this structure:
- Import statements - Required libraries
- Environment configuration - Load API credentials
- Function definitions - Reusable email sending functions
- Main execution block - Run the example
All scripts include error handling for:
- Missing API credentials
- API client errors
- Network issues
- Invalid email addresses
Example error handling pattern:
try:
response = mailchimp.messages.send({'message': message})
# Handle success
except ApiClientError as error:
print(f'Mandrill API Error: {error.text}')A successful API response includes:
[
{
"email": "recipient@example.org",
"status": "sent", # or "queued", "rejected", "invalid"
"_id": "abc123",
"reject_reason": None # or reason if rejected
}
]sent- Message was successfully sentqueued- Message is queued for sendingscheduled- Message is scheduled for future sendingrejected- Message was rejectedinvalid- Invalid recipient email address
To test without sending real emails, you can use Mandrill's test mode or verify your code with print statements before actual sending.
For issues with:
- Mandrill API: Contact Mailchimp support
- Python SDK: Open an issue on the GitHub repository
- These examples: Check the use-cases documentation
See the LICENSE file in the root directory.