Looking to give MailChimp dynamic content?

Kane Mitchell picture Kane Mitchell · Oct 27, 2014 · Viewed 15.7k times · Source

Ok so I'm looking to send out a weekly scheduled email with MailChimp. The email is to contain the newest 20 of the stock list (car stocklist of garage) to their subscribers.

I can't seem to get this to work with an RSS feed as imagined so i wondered is there any other way to get some formatted HTML (in a PHP file) into the body of MailChimp template on a weekly basis?

Many thanks.

Answer

Valentine Shi picture Valentine Shi · Nov 1, 2018

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.