Mailchimp Error: Bad Request - Your Campaign is no

2019-05-22 16:31发布

问题:

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   = 'info@newslume.com';
    $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

回答1:

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!