Looking to give MailChimp dynamic content?

Here is how @Miles M. 's answer translates into MailChimp API 3.0 (language-agnostic, links to Postman's and PHP examples are at the bottom side notes).

  1. Prepare all the MailChimp things according to my explanations here except step 4.

    That explanation is for the use case when you want MailChimp to send the completelly flexible content, providing the email's entire markup by yourself via API, not using the MailChimp's template (neither its pre-coded one nor the one custom-coded by you).

    The step 4 will be replaced by the following instructions. This describes the use case when you want to populate the specific part(s) of your own custom template with the dynamic data supplied from the API side before sending out the campaign this template is assigned to.

    So, let's get down to it.

  2. Create the MailChimp empty custom HTML template and add the following HTML there (simplified down to the bones)

    <div mc:edit="mytext">Mytext should come here from the API call</div>

    Now:

    • Save, exit and open again to see that the MailChimp template validator wrapped your markup in the generally must have HTML tags.
    • See this Mailchimp guide to understand why mc:edit="mytext" attribute should be added to a HTML tag and how to add your own mc:something attributes.
  3. Dynamically set the content of the above template's <div> marked with mc:edit="mytext" attribute by sending the API request (assume here using language-agnostic tool like Postman to make the requests and see the responses)

    • Make a request to the campaign update endpoint with an URL like this https://<dc>.api.mailchimp.com/3.0/campaigns/<your_campaign_id>/content and the JSON request body like this:

      {
          "template": {
              "id":29345,
              "sections": {
                  "mytext": "<p>This is my text set via the the API request</p>"
              }
          }
      }
      
    • You see, there you have to replace id with the template ID you created in step 2 (get templates list with this API request, find the one you need in the response and look up the ID or look up it in the MailChimp web interface, when hover over the template name in templates list, the browser's bottom line will show you the id at the end of the URL)

    • Then send the request. In the response you will see the campaign email in its HTML form (as well as in plain text form) with your <div> supplied with inner HTML from the content of your "mytext" JSON key, namely <div><p>This is my text set via the the API request</p></div>

    • Surely you can replace the content of "mytext" key with your dynamic markup.

    • Consequently you can add another HTML container tag with another attribute e.g. mc:edit="myotherdynamicdata" into the template, then add JSON "myotherdynamicdata"key in the request body, fill its content with some other dynamic HTML and send the request again. Then look at the repsonse body to see your dynamic info is set there.

  4. Now you have to send out the campaign. Look at the explanations linked in the item 1 above starting item 6. As you send out the campaign your subscribers see the dynamic portions embedded via editable content areas' content set dynamically via the API.

As a side notes on other use cases:

  • To send out the new posts from your blog you do not need the API. MailChimp does this automatically, see this guide, you just have to provide it with the link to RSS feed from your blog. It will check for new posts, and send out the campaign template.

  • For WoprPress users willing to send out newsletter with custom posts, while constructing the MailChimp automation task as per the item above, supply MailChimp with RSS link to your custom post type RSS feed that is provided by WordPress by default e.g. http://www.mywordpresssite.com/feed/?post_type=my_custom_post_type

  • The examples on how to make MailChimp API requests via Postman, authorization example and via PHP, adding the content via editable areas.

EDIT after @urwaCFC question in the comments below: how to use mc:edit within a mc:repeatable block.

In the experiment I could not make the template with mc:edit tags nested within the mc:repeatable and mc:variant blocks (using the MailChimp example markup (see Repeating Content Area section) linked here to be updated via the MailChimp update template API call.


If looking to inject custom content into a template at the time of sending, I would recommend having a look into creating a custom template that uses our template language.

If you've created a custom template within MailChimp using our template language to specify editable content areas: http://templates.mailchimp.com/getting-started/template-language/, then you would be able to update those content areas via the API.

To do this, you'll want to make either a campaigns/create call: https://apidocs.mailchimp.com/api/2.0/campaigns/create.php or a campaigns/update call: https://apidocs.mailchimp.com/api/2.0/campaigns/update.php and specify the section and content that you'd like to change as part of the 'content' parameter. the content 'sections' will correspond to the mc:edit tags that were added to the custom template.

You also have the ability to customize your content, like adding a first name to a greeting in the body of your content for instance, even further with the use of merge tags. I highly recommend checking those out as well and consider using them in your content if you need that level of customization: Getting Started with Merge Tags: http://kb.mailchimp.com/merge-tags/using/getting-started-with-merge-tags