Mailchimp Transactional Email API Examples

Transactional emails are automated, one-to-one messages triggered by user actions or behaviors. Examples include welcome emails, password reset requests, order confirmations, order status updates, and shipping notifications. These emails are essential for maintaining customer communication and providing timely updates related to their interactions with a service or product. To comply with CAN-SPAM laws and best practices, transactional emails must:

  • Not contain promotional content, focusing solely on the transaction-related information.
  • Adhere to privacy laws such as GDPR, ensuring customer data is handled securely.

Mailchimp’s Transactional Email Features

Mailchimp offers a robust transactional email platform with various features to enhance user engagement and streamline communications. Key offerings include:

  1. Fast Delivery: Ensures timely delivery of critical emails, reducing the chance of them landing in spam folders.
  2. Personalization and Segmentation: This feature allows sending personalized messages based on user behavior and preferences, enhancing the relevance of each email.
  3. Scalability and Reliability: Backed by over 20 years of email experience, Mailchimp provides a dependable platform capable of handling large volumes of emails.
  4. Comprehensive Analytics: Offers detailed insights into email performance, enabling businesses to optimize their strategies.
  5. Integration with Mailchimp’s Platform: It seamlessly connects with other Mailchimp ESP services, such as marketing emails and automation tools, for a unified approach to customer communication.

Mailchimp’s transactional email services are available under various pricing plans, catering to different business needs and scales:

  • Free: Suitable for new users with basic needs, limited to 500 contacts.
  • Essentials: A low-cost paid tier that includes A/B testing and basic automation.
  • Standard: A medium-cost paid tier offering enhanced automation and segmentation.
  • Premium: A robust, full-featured tier provides comprehensive features for large teams.

Mailchimp’s transactional email solutions offer businesses a powerful tool to communicate with customers effectively. By leveraging these services, companies can ensure that crucial information reaches their audience promptly and securely.

Mailchimp Transactional Email API: Overview and Key Calls

Mailchimp’s Transactional Email API, formerly known as Mandrill, allows developers to send and manage transactional emails from their applications. The API is designed for flexibility, enabling personalized, targeted emails based on user actions. Here, we’ll delve into the core aspects of the API and highlight critical calls using PHP as the example language.

The Mailchimp Transactional Email API is RESTful and uses JSON for requests and responses. It provides various endpoints for managing emails, templates, and reporting. To use the API, you must generate an API key from your Mailchimp account, which will authenticate your requests.

Critical API Calls

  • Sending Emails: One of the most common uses of the API is sending transactional emails. This can be done using the /messages/send endpoint. Here’s an example of how to send an email using PHP:
<?php
$apiKey = 'your_api_key';
$message = array(
    'html' => '<p>Your email content here</p>',
    'text' => 'Your email content here',
    'subject' => 'Your subject here',
    'from_email' => '[email protected]',
    'to' => array(
        array('email' => '[email protected]')
    )
);

$ch = curl_init('https://mandrillapp.com/api/1.0/messages/send.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('key' => $apiKey, 'message' => $message)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

echo $result;
?>
  • Sending A Templated Email: Send a transactional email using a pre-defined template in Mailchimp via the Transactional Email API. We’ll assume that you’ve already created a template in Mailchimp’s platform, and now you want to use it to send an email to a customer.
    1. Define Your PHP Function to Send an Email: First, let’s create a PHP function that sends an email using the Mailchimp Transactional Email API. This function will use a template by its name and fill in the dynamic content (like customer name, order details, etc.) through merge fields.
<?php
function sendTransactionalEmail($recipientEmail, $recipientName, $templateName, $mergeVars) {
    $apiKey = 'your_mailchimp_api_key_here';

    $message = array(
        'from_email' => '[email protected]',
        'to' => array(array('email' => $recipientEmail, 'name' => $recipientName)),
        'merge_language' => 'mailchimp',  // Use 'mailchimp' to enable Mailchimp-style template syntax
        'global_merge_vars' => $mergeVars,
    );

    $postData = array(
        'key' => $apiKey,
        'template_name' => $templateName,
        'template_content' => array(), // Can be empty when not using editable regions
        'message' => $message,
    );

    $ch = curl_init('https://mandrillapp.com/api/1.0/messages/send-template.json');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}
?>
  1. Prepare Your Data and Call the Function: Now, prepare the data you want to send. This includes the recipient’s details, the template name, and any merge fields that your template expects. Merge fields are used to insert dynamic content into your email template.
<?php
// Customer and email details
$customerEmail = '[email protected]';
$customerName = 'Jane Doe';
$templateName = 'your_template_name_here'; // The name of your Mailchimp template

// Merge variables for personalization
$mergeVars = array(
    array('name' => 'FNAME', 'content' => 'Jane'), // First name
    array('name' => 'ORDER_NUM', 'content' => '123456'), // Example of an order number
    array('name' => 'ORDER_TOTAL', 'content' => '$29.99'), // Example of an order total
);

// Send the email
$result = sendTransactionalEmail($customerEmail, $customerName, $templateName, $mergeVars);
echo $result;
?>

In this example, $mergeVars contains the data that will replace the corresponding placeholders in your email template. For instance, if your template contains the tag |FNAME|, it will be replaced by ‘Jane’ when the email is sent. Some notes:

  • Ensure that your Mailchimp transactional template is set up with the correct merge tags that match those in your $mergeVars array.
  • Replace 'your_mailchimp_api_key_here', '[email protected]', and 'your_template_name_here' with your actual Mailchimp API key, sending email address, and template name, respectively.
  • The function sendTransactionalEmail() sends a request to the Mailchimp API to send an email based on the specified template and data.

This example demonstrates how to send a personalized, template-based email to a single customer. You can adapt this approach to send different types of transactional emails, such as order confirmations, shipping notifications, or password resets.

  • Managing Templates: If you use templates for your emails, you can manage them through the API. The endpoints /templates/add, /templates/info, and /templates/update are particularly useful. Here’s how you can retrieve information about a template:
<?php
$apiKey = 'your_api_key';
$templateName = 'your_template_name';

$ch = curl_init('https://mandrillapp.com/api/1.0/templates/info.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('key' => $apiKey, 'name' => $templateName)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

echo $result;
?>
  • Viewing Reports and Analytics: Mailchimp provides detailed reports on your emails. Accessing this data can help you refine your email strategy. Use the /reports/search endpoint to fetch email performance data:
<?php
$apiKey = 'your_api_key';

$ch = curl_init('https://mandrillapp.com/api/1.0/reports/search.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('key' => $apiKey)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

echo $result;
?>

Mailchimp’s Transactional Email API is a powerful tool for automating and managing email communications. By leveraging the key API calls outlined above, you can integrate sophisticated email functionalities into your applications, enhancing your customer communication strategy.

Mailchimp Transactional Email Information

©2024 DK New Media, LLC, All rights reserved.

Originally Published on Martech Zone: Mailchimp: Streamlining Customer Communication with Transactional Emails (Formerly Mandrill)