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:
$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);
}