This use case demonstrates how to send emails using pre-created Mandrill templates with the Mailchimp Transactional API.
Templates are reusable email layouts stored in your Mandrill account. They allow you to:
- Maintain consistent branding
- Separate design from code
- Use dynamic content regions
- PHP 7.4+
- Composer
- Mandrill API key
- A template created in Mandrill
<?php
$mailchimp = new \MailchimpTransactional\ApiClient();
$mailchimp->setApiKey($_ENV['MANDRILL_API_KEY']);
$templateData = [
'name' => 'welcome-template',
'from_email' => 'sender@example.com',
'from_name' => 'Company Name',
'subject' => 'Welcome {{fname}}!',
'code' => '
<h1>Hello {{fname}}!</h1>
<div mc:edit="main_content">
<p>Default content here.</p>
</div>
',
'text' => 'Hello {{fname}}!',
'publish' => true
];
$result = $mailchimp->templates->add($templateData);<?php
$mailchimp = new \MailchimpTransactional\ApiClient();
$mailchimp->setApiKey($_ENV['MANDRILL_API_KEY']);
$message = [
'from_email' => 'sender@example.com',
'from_name' => 'Company Name',
'to' => [
[
'email' => 'recipient@example.com',
'name' => 'John Smith',
'type' => 'to'
]
],
'global_merge_vars' => [
['name' => 'fname', 'content' => 'John']
],
'merge_language' => 'handlebars'
];
// Content for mc:edit regions
$templateContent = [
[
'name' => 'main_content',
'content' => '<p>Welcome to our platform!</p>'
]
];
$result = $mailchimp->messages->sendTemplate([
'template_name' => 'welcome-template',
'template_content' => $templateContent,
'message' => $message
]);Use mc:edit attributes to define editable regions:
<div mc:edit="header">
Default header content
</div>
<div mc:edit="body">
Default body content
</div>
<div mc:edit="footer">
Default footer content
</div>Then override when sending:
$templateContent = [
['name' => 'header', 'content' => '<h1>Custom Header</h1>'],
['name' => 'body', 'content' => '<p>Custom body content</p>'],
['name' => 'footer', 'content' => '<p>© 2024 Company</p>']
];// List all templates
$templates = $mailchimp->templates->list();
// Get template info
$info = $mailchimp->templates->info(['name' => 'welcome-template']);
// Delete template
$mailchimp->templates->delete(['name' => 'old-template']);- Create templates via API or Mandrill dashboard
- Use
mc:editfor dynamic content regions - Combine with merge tags for personalization
- Templates can have default values that get overridden