donohoe
donohoe

Reputation: 14133

Sending email to subscribers in MailChimp based on Tag

I am using PHP with the MailChimp API. I am not using the MC library, but making calls direct.

I am trying to send a newsletter to a subset of Subscribers in an Audience. This subset is defined by a Tag. I am expecting this group to be in the 1,000s (in the future) so I do not want to get the subscribers email and then send to them. Instead I want to leverage MC's segments, define that, and send it to them.

I define my segment like this:

$segmentData = [
    'name' => $segmentName,
    'options' => [
        'match' => 'any',
        'conditions' => [
            [
                'condition_type' => 'Tags',
                'field' => 'tags',
                'op' => 'contains',
                'value' => [$tagName]
            ],
        ],
    ],
];

But I keep getting an error:

"errors":[{
  "field":"options.conditions.item:0",
  "message":"Data did not match any of the schemas described in anyOf."
}]

I have a test Audience of about 20 subscribers. One of these is tagged "notify_region".

I've tried a few different variations but it doesn't help.

Is this possible, or am I missing something?

This is my test code:

<?php

$apiKey      = 'YOUR_MAILCHIMP_API_KEY';
$listId      = 'YOUR_LIST_ID';
$templateId  = 'YOUR_TEMPLATE_ID';
$tagName     = 'notify_region';
$segmentName = 'Example Segment 1';

function callMailchimp($method, $endpoint, $data = []) {
    global $apiKey;
    $apiUrl = "https://us10.api.mailchimp.com/3.0";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl . $endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_USERPWD, "user:$apiKey");
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json'
        ]);
    }
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}

$segmentData = [
    'name' => $segmentName,
    'options' => [
        'match' => 'any',
        // 'match' => 'all',
        'conditions' => [
            [
                'condition_type' => 'Tags',
                'field' => 'tags',
                'op' => 'contains',
                'value' => [$tagName]
                // 'value' => [ $tagName ],
            ],
        ],
    ],
];

$segmentResponse = callMailchimp('POST', "/lists/$listId/segments", $segmentData);

if (isset($segmentResponse['id'])) {
    $segmentId = $segmentResponse['id'];

    $campaignData = [
        'type' => 'regular',
        'recipients' => [
            'list_id' => $listId,
            'segment_opts' => [
                'saved_segment_id' => $segmentId,
            ],
        ],
        'settings' => [
            'subject_line' => 'Notification for Region',
            'from_name'    => 'Company',
            'from_email'   => '[email protected]',
            'template_id'  => $templateId,
        ],
    ];

    $campaignResponse = callMailchimp('POST', '/campaigns', $campaignData);

    if (isset($campaignResponse['id'])) {
        $campaignId = $campaignResponse['id'];

        // $sendResponse = callMailchimp('POST', "/campaigns/$campaignId/actions/send");
        // if(isset($sendResponse['message']) && $sendResponse['message'] === 'Campaign send started'){
        //     echo "Campaign sent successfully!";
        // } else {
        //     echo "Failed to send campaign: " . json_encode($sendResponse);
        // }

        echo "FIN.";

    } else {
        echo "Failed to create campaign: " . json_encode($campaignResponse);
    }
} else {
    echo "Failed to create segment: " . json_encode($segmentResponse);
}

Upvotes: 1

Views: 19

Answers (0)

Related Questions