Mailchimp php curl sending email to subscriber

196 Views Asked by At

I am trying to send email to all subscribers in mailchimp using PHP's cURL but I am keep having an error saying The requested resource could not be found. My cURL code for other features like Adding member as a "Subscriber" is working fine with same point $url having version 3.0. But sending email with similar $url version 3.0 seems to be not working for me. Below is my full code what I am trying so far.

<?php 
echo sendEmailToSubscriber();
function sendEmailToSubscriber(){
    $apiKey = "MY-API-KEY";
    $campaignId = "MY-COMPAIGN-ID";
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
    $url = 'https://'. $dataCenter . '.api.mailchimp.com/3.0/campaigns/' . $campaignId .'/actions/send';
    $jsonEmail = '{"test_emails":["the mail you want to send thing sat"],"send_type":"html"}';
    $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, 'POST');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonEmail);            
    $result = curl_exec($ch);
    print_r($result);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $httpCode;
}

As you can see .. when I echo the url I am getting the correct url.

URL - https://us11.api.mailchimp.com/3.0/campaigns/MY-CAMPAIGN-ID/actions/send

But when I echo or print the response.. I am keep getting below errors in response.

Status code - 404

Response - {"type":"https://mailchimp.com/developer/marketing/docs/errors/","title":"Resource Not Found","status":404,"detail":"The requested resource could not be found.","instance":"101f48b7-0d59-6812-5a35-29ccc67032b2"}

Can someone guide me what I am doing wrong here ? I want to send email to all my subscribers.

0

There are 0 best solutions below