This comprehensive example demonstrates all major features of the Mailchimp Transactional (Mandrill) API in a single email.
<?php
require_once __DIR__ . '/vendor/autoload.php';
$mailchimp = new \MailchimpTransactional\ApiClient();
$mailchimp->setApiKey($_ENV['MANDRILL_API_KEY']);
// Prepare attachments
$attachments = [];
// PDF attachment
if (file_exists('sample.pdf')) {
$attachments[] = [
'type' => 'application/pdf',
'name' => 'sample.pdf',
'content' => base64_encode(file_get_contents('sample.pdf'))
];
}
// Dynamic text file
$textContent = "Kitchen Sink Demo\nGenerated: " . date('c');
$attachments[] = [
'type' => 'text/plain',
'name' => 'readme.txt',
'content' => base64_encode($textContent)
];
// Embedded images
$images = [];
if (file_exists('logo.png')) {
$images[] = [
'type' => 'image/png',
'name' => 'logo',
'content' => base64_encode(file_get_contents('logo.png'))
];
}
// Build the message
$message = [
// Content
'html' => '
<h1>Hello {{fname}}!</h1>
<p>This demonstrates all API features.</p>
<p>Company: {{company_name}}</p>
<p>Account: {{account_id}}</p>
<p><a href="https://example.com">Click here</a> for tracking demo.</p>
<img src="cid:logo" alt="Logo">
',
'text' => 'Hello {{fname}}! This demonstrates all API features.',
'subject' => 'Kitchen Sink Demo - {{fname}}',
// Sender
'from_email' => 'sender@example.com',
'from_name' => 'Demo Sender',
// Recipients
'to' => [
[
'email' => 'recipient@example.com',
'name' => 'John Smith',
'type' => 'to'
]
],
// Custom headers
'headers' => [
'Reply-To' => 'reply@example.com',
'X-Custom-Header' => 'Kitchen-Sink-Demo',
'X-Campaign-ID' => 'demo-2024'
],
// Merge variables
'global_merge_vars' => [
['name' => 'company_name', 'content' => 'Acme Corp']
],
'merge_vars' => [
[
'rcpt' => 'recipient@example.com',
'vars' => [
['name' => 'fname', 'content' => 'John'],
['name' => 'account_id', 'content' => 'ACC-12345']
]
]
],
'merge_language' => 'handlebars',
// Attachments and images
'attachments' => $attachments,
'images' => $images,
// Tracking
'track_opens' => true,
'track_clicks' => true,
// Processing options
'auto_text' => true,
'auto_html' => false,
'inline_css' => true,
// Organization
'tags' => ['demo', 'kitchen-sink', 'all-features'],
'metadata' => [
'campaign' => 'kitchen-sink',
'version' => '1.0',
'environment' => 'development'
],
// Delivery options
'important' => true,
'view_content_link' => true,
'preserve_recipients' => false,
'async' => false
];
try {
$result = $mailchimp->messages->send(['message' => $message]);
print_r($result);
} catch (\MailchimpTransactional\ApiException $e) {
echo "Error: " . $e->getMessage();
}
<img src="cid:logo" alt="Logo">
'images' => [
[
'type' => 'image/png',
'name' => 'logo', // Referenced as cid:logo
'content' => base64_encode($imageContent)
]
]