This directory contains PHP implementations of Mandrill API use cases using the mailchimp/transactional library.
- PHP 7.4 or higher (8.0+ recommended)
- Composer
- A Mandrill API key from Mailchimp
-
Install Composer if you haven't already:
- Visit getcomposer.org
-
Install the required packages:
cd scripts
composer installInstall the required packages individually:
composer require mailchimp/transactional vlucas/phpdotenv- 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"Important: Always quote values that contain spaces or special characters. The PHP dotenv library strictly enforces this.
File: email_with_single_recipient.php
Demonstrates how to send a basic email to a single recipient.
php email_with_single_recipient.phpFeatures:
- Basic email sending
- HTML and plain text content
- Custom headers
- Advanced options with tracking and metadata
File: email_with_merge_tags.php
Personalize emails using merge tags for dynamic content.
php email_with_merge_tags.phpFeatures:
- Global merge variables (apply to all recipients)
- Recipient-specific merge variables
- Handlebars template syntax
- Multiple recipient personalization
File: email_with_template.php
Send emails using pre-created Mandrill templates.
php email_with_template.phpFeatures:
- 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.php or via the Mandrill UI.
File: email_with_attachments.php
Attach files (PDF, CSV, JSON, etc.) to your emails.
php email_with_attachments.phpFeatures:
- Attach local files (PDF, images, etc.)
- Create dynamic attachments (CSV, JSON, text)
- Base64 encoding handling
- Multiple attachment types
File: create_template.php
Create and manage reusable email templates.
php create_template.phpFeatures:
- Create new templates
- List all templates
- Get template information
- Delete templates
- mc:edit regions for dynamic content
File: kitchen_sink_email.php
Comprehensive example demonstrating all Mandrill features.
php kitchen_sink_email.phpFeatures:
- All message options in one example
- Attachments, tracking, metadata
- Multiple recipient types (TO, CC, BCC)
- Advanced headers and custom fields
All scripts include error handling for:
- Missing API credentials
- API client errors (
MailchimpTransactional\ApiException) - Network issues
- Invalid email addresses
Example error handling pattern:
try {
$result = $mailchimp->messages->send(['message' => $message]);
// Handle success
} catch (\MailchimpTransactional\ApiException $e) {
echo "Mandrill API Error: " . $e->getMessage();
}A successful API response is an array of results:
[
[
"email" => "recipient@example.org",
"status" => "sent", // or "queued", "rejected", "invalid"
"_id" => "abc123",
"reject_reason" => null // 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
Fatal error: Class 'MailchimpTransactional\ApiClient' not found
Solution:
composer install
# or
composer dump-autoloadPermission denied: .env
Solution: Check file permissions:
chmod 644 .envYour PHP version does not satisfy requirements
Solution: Check your PHP version and upgrade if needed:
php -vSee the LICENSE file in the root directory.