Mailchimp Error: Bad Request - Your Campaign is not ready to send
Asked Answered
H

1

6

I am using following tutorial to create campaign and send email in MailChimp using Php.

https://isabelcastillo.com/create-send-mailchimp-campaign-api-3

My Code piece are

    require_once('../wp-load.php');

    function isa_mailchimp_api_request( $endpoint, $type = 'POST', $body = '' ) 
    { 
    // Configure -------------------------------------- 
    $api_key = 'API KEY HERE'; // Changed API Key here 
    // STOP Configuring ------------------------------- 
    $core_api_endpoint = 'https://<dc>.api.mailchimp.com/3.0/';
    list(, $datacenter) = explode( '-', $api_key );
    $core_api_endpoint = str_replace( '<dc>', $datacenter, $core_api_endpoint );

    $url = $core_api_endpoint . $endpoint;  
    //print_r($url );

    $request_args = array(
        'method'      => $type,
        'timeout'     => 20,
        'headers'     => array(
            'Content-Type' => 'application/json',
            'Authorization' => 'apikey ' . $api_key
        )
    );

    if ( $body ) {
        $request_args['body'] = json_encode( $body );
    }

    $request = wp_remote_post( $url, $request_args );
    $response = is_wp_error( $request ) ? false : json_decode( wp_remote_retrieve_body( $request ) );


    echo '<pre>';
    print_r($response); 

    return $response;   
    }



    function isa_create_mailchimp_campaign( $list_id, $subject ) {    
    $reply_to   = '[email protected]';
    $from_name  = 'NewsLume';
    $subject= 'Another new test message 14 17'; 
    $campaign_id = ''; 
    $body = array(
        'recipients'    => array('list_id' => $list_id),
        'type'          => 'regular',
        'settings'      => array('subject_line' => $subject,
                                'title' => 'a  test title NewsLUme',
                                'reply_to'      => $reply_to,
                                'from_name'     => $from_name,
                                'use_conversation'=> false,
                                'to_name'=> 'sajid',

                                'auto_footer'=> false,
                                'inline_css'=> false,
                                'auto_tweet'=> false,
                                'drag_and_drop'=> false

                                )
    );

    $create_campaign = isa_mailchimp_api_request( 'campaigns', 'POST', $body ); 

    if ( $create_campaign ) {
        if ( ! empty( $create_campaign->id ) && isset( $create_campaign->status ) && 'save' == $create_campaign->status ) {
            // The campaign id: 
            $campaign_id = $create_campaign->id;
        }
    }

    return $campaign_id ? $campaign_id : false;

}    

function isa_set_mail_campaign_content( $campaign_id, $template_content  ) {
    $set_content = '';
    $set_campaign_content = isa_mailchimp_api_request( "campaigns/$campaign_id/content", 'PUT', $template_content ); 

    if ( $set_campaign_content ) {
        if ( ! empty( $set_campaign_content->html ) ) {
            $set_content = true;
        }
    }               
    return $set_content ? true : false;
}


$list_id='my_list_id_here'; // LIST HERE

$campaign_id = isa_create_mailchimp_campaign( $list_id, $subject );

if ( $campaign_id ) { 
    // Set the content for this campaign 
   $template_content = array( 
        'template' => array( 
                // The id of the template to use. 
                'id' => 47615, // INTEGER   
                'sections'  => array(                     
                    'tst_content' => 'THIS IS THE CONTENT BODY OF MY EMAIL MESSAGE.' 
            )

        )
    );
    $set_campaign_content = isa_set_mail_campaign_content( $campaign_id, $template_content );


    if ( $set_campaign_content ) {

        $send_campaign = isa_mailchimp_api_request( "campaigns/$campaign_id/actions/send", 'POST' ); 
        if ( empty( $send_campaign ) ) { 
            // Campaign was sent! 
        } elseif( isset( $send_campaign->detail ) ) { 
            $error_detail = $send_campaign->detail;

        }

    }

}

I have updated all values, including API KEY, List ID, template ID etc. but still i am getting errors

Here is Error object

stdClass Object ( [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ [title] => Bad Request [status] => 400 [detail] => Your Campaign is not ready to send. [instance] => 89dc8734-2611-4f3b-a4f7-d18bd181bded )

I checked in Mail Chimp, campaigns are created there but they are saved as Draft.

Here are my API Logs

API Logs can be seen by clicking link below https://drive.google.com/file/d/0BwIWuJmCDI1vNHgtVm9TQm1FMVU/view?usp=drivesdk

I am able to create campaign, set a template to campaign but i cannot send emails. My Domain is also verified and authenticated with Mailchimp using guidelines. Please check and suggest a solution

Holohedral answered 15/9, 2017 at 11:13 Comment(1)
I was also facing the same issue. My issue was, the sum of all subscriber in all lists were exceeding the free subscriber limit given by mailchimp (which is 2000).Mccants
G
4

While the "Your Campaign is not ready to send" message isn't very helpful, you can check for a more detailed message in MailChimp itself. Edit the draft that the API created, and navigate to the final Confirm step. You'll see a checklist where most of the items passed, but there will also be an item that explains why the campaign failed.

When I attempted to replicate the issue, the campaign failed to send because there was some default placeholder text left unchanged in the template. Since the code you posted only sets the content for one block, this is probably the same issue you're having.

Hope this helps!

Gracegraceful answered 16/9, 2017 at 4:12 Comment(3)
Hello Joel. Thank you for suggestions. I contacted Mail chimp and they told me same thing and issue was with email templates i was using. I deleted all content from email and then added dummy text to check and it went fine. Thank you for your help.Holohedral
@OptimumCreative happy to help!Gracegraceful
I was also facing the same issue. My issue was, the sum of all subscriber in all lists were exceeding the free subscriber limit given by mailchimp (which is 2000).Mccants

© 2022 - 2024 — McMap. All rights reserved.