Mailchimp错误:错误请求 - 您的广告系列尚未准备好发送

时间:2017-09-15 11:13:50

标签: php wordpress rest mailchimp mailchimp-api-v3.0

我正在使用以下教程来创建广告系列并使用Php在MailChimp中发送电子邮件。

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

我的代码片段

    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;

        }

    }

}

我更新了所有值,包括API KEY,列表ID,模板ID等,但我仍然遇到错误

这是错误对象

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 )

我在Mail Chimp中检查过,在那里创建了广告系列但是它们被保存为草稿。

以下是我的API日志

点击下面的链接可以看到API日志 https://drive.google.com/file/d/0BwIWuJmCDI1vNHgtVm9TQm1FMVU/view?usp=drivesdk

我可以制作广告系列,为广告系列设置模板,但我无法发送电子邮件。我的域名也通过Mailchimp使用指南进行验证和验证。 请检查并建议解决方案

1 个答案:

答案 0 :(得分:1)

虽然“您的广告系列尚未准备好发送”消息不是很有帮助,但您可以在MailChimp本身检查更详细的消息。编辑API创建的草稿,然后导航到最终的确认步骤。您会看到大多数项目通过的核对清单,但也会有一个项目说明广告系列失败的原因。

当我尝试复制该问题时,广告系列无法发送,因为模板中有一些默认的占位符文字保持不变。由于您发布的代码只设置了一个块的内容,这可能与您遇到的问题相同。

希望这有帮助!