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
You don't have enough subscribers on your list- MailChimp proposals are created and sent to you when you send off a MailChimp email campaign to at least 500 subscribers. You sent an email with Mailchimp's "Time Warp" feature.
Make sure your link is really a link. To ensure your formatting is correct before you send, enter preview mode and use the link checker tool. If you're working with the new email builder, we'll mark broken links with an exclamation point icon as you add your content.
Current MailChimp status is up.
Upload issues If you're having trouble uploading an image to Mailchimp's Content Studio, review these common issues. The image is too large. If your image takes a long time to upload or won't upload, the file size could be too large. We recommend you use a photo-editing program to resize your image to 1MB or smaller.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With