I am trying to subscribe a user to a list using the Mailchimp API and for some reason it keeps submitting the status as "pending" even though I have it set to "subscribed". I am trying to directly subscribe the user without prompting the conformation email.
Code:
$data = [
'email' => '[email protected]',
'status' => 'subscribed',
'firstname' => 'john',
'lastname' => 'doe'
];
syncMailchimp($data);
function syncMailchimp($data) {
$apiKey = ###;
$listId = ###;
$memberId = md5(strtolower($data['email']));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
$json = json_encode([
'email_address' => $data['email'],
'status' => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => [
'FNAME' => $data['firstname'],
'LNAME' => $data['lastname']
]
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpCode;
}
Response:
{
"id": "44848d9005a4ed1d5272c10e46031670",
"email_address": "[email protected]",
"unique_email_id": "37a0bec0fb",
"email_type": "html",
"status": "pending",
"merge_fields": {
"FNAME": "john",
"LNAME": "doe"
},
"stats": {
"avg_open_rate": 0,
"avg_click_rate": 0
},
"ip_signup": "",
"timestamp_signup": "",
"ip_opt": "50.62.176.151",
"timestamp_opt": "2017-01-05T17:20:22+00:00",
"member_rating": 2,
"last_changed": "2017-01-05T18:40:57+00:00",
"language": "",
"vip": false,
"email_client": "",
"location": {
"latitude": 0,
"longitude": 0,
"gmtoff": 0,
"dstoff": 0,
"country_code": "",
"timezone": ""
},
"list_id": "ec3297395e",
"_links": [{
"rel": "self",
"href": "https://us5.api.mailchimp.com/3.0/lists/ec3297395e/members/44848d9005a4ed1d5272c10e46031670",
"method": "GET",
"targetSchema": "https://us5.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json"
}, {
"rel": "parent",
"href": "https://us5.api.mailchimp.com/3.0/lists/ec3297395e/members",
"method": "GET",
"targetSchema": "https://us5.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/CollectionResponse.json",
"schema": "https://us5.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/Members.json"
}, {
"rel": "update",
"href": "https://us5.api.mailchimp.com/3.0/lists/ec3297395e/members/44848d9005a4ed1d5272c10e46031670",
"method": "PATCH",
"targetSchema": "https://us5.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json",
"schema": "https://us5.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/PATCH.json"
}, {
"rel": "upsert",
"href": "https://us5.api.mailchimp.com/3.0/lists/ec3297395e/members/44848d9005a4ed1d5272c10e46031670",
"method": "PUT",
"targetSchema": "https://us5.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json",
"schema": "https://us5.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/PUT.json"
}, {
"rel": "delete",
"href": "https://us5.api.mailchimp.com/3.0/lists/ec3297395e/members/44848d9005a4ed1d5272c10e46031670",
"method": "DELETE"
}, {
"rel": "activity",
"href": "https://us5.api.mailchimp.com/3.0/lists/ec3297395e/members/44848d9005a4ed1d5272c10e46031670/activity",
"method": "GET",
"targetSchema": "https://us5.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Activity/Response.json"
}, {
"rel": "goals",
"href": "https://us5.api.mailchimp.com/3.0/lists/ec3297395e/members/44848d9005a4ed1d5272c10e46031670/goals",
"method": "GET",
"targetSchema": "https://us5.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Goals/Response.json"
}, {
"rel": "notes",
"href": "https://us5.api.mailchimp.com/3.0/lists/ec3297395e/members/44848d9005a4ed1d5272c10e46031670/notes",
"method": "GET",
"targetSchema": "https://us5.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Notes/CollectionResponse.json"
}]
}
There are a number of things that could be going on here, but you might want to make sure that the account your API key belongs to does not have any MailChimp compliance related events preventing you from directly subscribing addresses.