Mailchimp API not replacing mc:edit content sections (using ruby library)

℡╲_俬逩灬. 提交于 2019-12-18 05:50:46

问题


I'm having a problem replacing mc:edit content areas in Mailchimp with the content that I provide.

The email is sent out to the subscribers, but none of the content provided is added to the email. Can anyone see where I might be going wrong?

This is the script that I am using:

campaign = mailchimp.campaigns.create(
    "regular",
    {
        "list_id" => list_id,
        "subject" => "Email Test",
        "from_email" => "edward@somewhere.com",
        "from_name" => "Edward",
        "to_name" => "The to name",
        "template_id" => 35089
    },
    {
        "sections" =>
        {
            "commit_stuff" => "Modified project to use XYZ ruby gem. #ABC-123",
            "content" => "This is the content",
            "more-content" => "This is more content"
        }
    }
)
result = mailchimp.campaigns.send(campaign["id"])

And this is the section inside the email that I am trying to modify:

<div mc:edit="commit_stuff" class="mcnTextContent">Use your own custom HTML</div>

<div mc:edit="content"></div>

<div mc:edit="more-content"></div>

Relevant docs:

  • API https://apidocs.mailchimp.com/api/2.0/campaigns/create.php
  • Library http://www.rubydoc.info/gems/mailchimp-api/2.0.4/Mailchimp/Campaigns#create-instance_method

回答1:


I struggled with this for a few days, using the template manager in MailChimp. The only way I got it to work was exporting my existing template, adding the mc:edit tag to the code and then uploading it as a custom template.

Exporting template from MailChimp

  • Go to 'Templates'
  • Click on the 'Edit' drop down arrow, next to the template you want to use with the API
  • Select 'Export HTML'

Uploading your template to MailChimp

  • Go to 'Templates'
  • Click the 'Create Template ' button in the top right
  • Click the 'Code Your Own'
  • Then select 'Import html'

Example of my template code:

<div mc:edit="eventmessage">
Custom Event Message (replaced by API Call)
<br></div>

As a check, I was now able to see the section now appear when using /templates/info API call

Once I confirmed that Mailchimp saw the template section I used /campaigns/create call, as mentioned above but skipping over the html definition.

Updated campaign/create (content/sections):

    },
"content": {
    "sections": {
        "eventmessage": "Event Message Content"
    },

},



回答2:


As per @kateray comment above, after an hour of tries I managed to insert my custom HTML from my back-end as the MailChimp campaign content via its API 3.0. For such a simple use case it is rather annoying not to have a ready-made solution on their docs. Surely MailChimp API lacks a cookbook.

So from the beginning:

  1. a) create the mailing list with API or with MailChimp web interface - create list and b) add the recepients to it - add members.

  2. Create the new campaign via API create campaign or their website. Do not assign any template to it.

  3. Assign the mailing list to the camplaign assign mailing list.

  4. Now set the campaign content with this API endpoint. Assign the following value to the JSON body of the request you send to the endpoint:

{
    "html": "<p>This is your custom HTML assigned to your campaign as content.</p>"
}

and send the request.

  1. In the response to this request you get the HTML you set and its plain text version.

  2. Go in the MailChimp web interface and ensure the campaign has all the check marks green.

  3. Send out the campaign with this API request.

NB:

  • double check you use the correct HTTP verbs for each API request: GET, PUT, PATCH, POST as shown in the API reference
  • do not forget to authenticate your API calls, here is the quick intro with clean examples made in Postman
  • double check you pass the correct list ID, campaign ID with your API requests.



回答3:


There's an endpoint to set the content along with the sections available at:

https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content/#edit-put_campaigns_campaign_id_content




回答4:


Should that be inside a "content" block? In the API example I see this:

    },
    "content": {
        "html": "example html",
        "sections": {
            "...": "..."
        },
        "text": "example text",
        "url": "example url",
        "archive": "example archive",
        "archive_type": "example archive_type"
    },



回答5:


Following PHP code worked for me

$api = new MCAPI($apikey);

$type = 'regular';

$opts['list_id'] = 'id';
$opts['subject'] = 'The subject';
/*<div mc:edit="std_content00">*/
$content = array('html_std_content00'=> $template);

$retval = $api->campaignCreate($type, $opts, $content);


来源:https://stackoverflow.com/questions/29366766/mailchimp-api-not-replacing-mcedit-content-sections-using-ruby-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!