Looking to give MailChimp dynamic content?

19,729

Solution 1

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

Solution 2

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.

Share:
19,729
Kane Mitchell
Author by

Kane Mitchell

Updated on July 14, 2022

Comments

  • Kane Mitchell
    Kane Mitchell almost 2 years

    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.

  • iwaduarte
    iwaduarte over 5 years
    How would this update to the v3.0 API ? Is there any support for node js ? Did not find anything related on the documentation yet.
  • Valentine Shi
    Valentine Shi over 5 years
    @iwaduarte, I just added the the answer on how to add the dynamic content down below. As for NodeJS you can see a bunch of MailChimp wrappers if you do not like to use HTTP Node's API itself. Or just google
  • Kane Mitchell
    Kane Mitchell over 5 years
    Thanks for the answer, I've not needed to do this for a while but it'll be good to know when I do need to.
  • Urvah Shabbir
    Urvah Shabbir over 5 years
    Thank you so much for your post. There is very little info available regarding dynamic content using API. One question: I have successfully implemented all of the above. One step further I am trying to use mc:edit within a mc:repeatable block. So what I am trying to do is:- repeat a block (a picture and a button) and then update picture link and button link. Is it possible because I am failing to do it.
  • Valentine Shi
    Valentine Shi over 5 years
    @urwaCFC, my pleasure :) I did some research and put my view upon your isue in the edit of my answer above.
  • Urvah Shabbir
    Urvah Shabbir over 5 years
    Thank you for your response. I have also created a separate question for this here:stackoverflow.com/questions/53692549/… I believe I was not able to communicate my issue correctly. I am trying to have just one repeatable block. So the first question is: can be repeat a block (the one with mc:repeatable ofcourse) through the API? I have not come across any documentation for that.
  • Urvah Shabbir
    Urvah Shabbir over 5 years
    As for mc:variant my understanding is that it is only used for formatting (one column, two column, left aligned, right aligned) that kind of thing. But ofcourse I could be wrong.
  • Urvah Shabbir
    Urvah Shabbir over 5 years
    The only solution that I can think right now is to have custom template saved as a variable in java script. And depending on number of blocks needed (image + button), add the whole ` <tbody class="mcnImageBlockOuter">... </tbody <tbody class="mcnButtonBlockOuter">...</tbody> ` as many times as required.
  • Valentine Shi
    Valentine Shi over 5 years
    @urwaCFC yes, I belive this is the way to go. See my edit update and the answer in your other SO questioni.
  • Urvah Shabbir
    Urvah Shabbir over 5 years
    Thanks for your help.
  • Taytus
    Taytus over 5 years
    This is great for when you need to update a specific section. But how would you create a campaign where each email to each member of a list will have different information? Is that even possible? I know I can dynamically create templates, but that would be a nightmare to track right?
  • Valentine Shi
    Valentine Shi over 5 years
    @Taytus, hey there :) By different information do you mean the template with fixed data fields dynamicallt filled with the member-specific information? Or you mean completelly different template for each member?
  • Valentine Shi
    Valentine Shi over 5 years
    @Taytus, If you have the particular use case to clarify you can open the new question on SO, post the link here and I will answer if I have a solution for it.
  • Taytus
    Taytus over 5 years
    I meant the template with fixed data fields dynamically filled with the member-specific information. I figured it out :) Thank you @bob-12345
  • Valentine Shi
    Valentine Shi over 5 years
    @Taytus, then look up my answer above, this is all about adding the dynamic filelds content via MC API to the pre-configured template.
  • Serge D
    Serge D about 5 years
    @bob-12345 is there any way to contact you to get some personal help on this topic. Paid. I know we don't really do that here but I'm running out of ideas :) [email protected]
  • ThreeCheeseHigh
    ThreeCheeseHigh almost 4 years
    @ValentineShi Is there a way to bypass the MailChimp Standard Plan, using the Essentials Plan with a "normal" Template and a custom block in it? Could I use the template vars in the custom block and fill it via API?
  • Valentine Shi
    Valentine Shi almost 4 years
    @ThreeCheeseHigh as I may infer from your description, basically you need a unique content letter for each recepient. If you anyways using API so you are well qualified to work with APIs and templates on your server side. Then a much simpler solution would work instead of trying to make MailChimp things it does poorly. That is SendGrid transaction emails API. They have an appropriate free tier pricing plan. The API have its quircks but it is 100% does what you need and worked reliable for me for past 3 years under PHP or Node.