Software Developer

Schedule a MailChimp Campaign With Batch Delivery in WordPress

This is how you can send a MailChimp campaign with “batch delivery” using the MailChimp API version 3. This uses PHP and it works in WordPress. In order to send a MailChimp campaign using batch delivery, it has to be scheduled rather than sent immediately.

The MailChimp help articles state that “batch delivery” is only available for paid account holders. However, free MailChimp account holders can still use batch delivery by using the MailChimp API to code their own solution.

This example shows you how to send (schedule) an existing campaign, so you need to already have the campaign ID. To get the campaign ID of a campaign that you already created in MailChimp, log in to your MailChimp account. Go to the “Campaigns” page. In the list of campaigns, find the one you want, and hover over the right side of it until the downward arrow appears. Click on the downward arrow, then click “View Email” to preview the campaign. While previewing, look at the address bar and find the part of the address that looks like this:

id=10axaza7f7

Everything after the equal sign is the campaign ID. Save that number since you will need to insert it into the code below. (NOTE: This campaign ID is NOT the same ID number that appears in the address bar while you are creating or editing the campaign.)

All of the following code is PHP. In WordPress, you can add it to your functions.

This first function is the same that I use in another MailChimp tutorial (Create and Send a MailChimp Campaign), so if you’re already using that code, you don’t have to add this first function again.

You must replace YOUR_API_KEY with your own MailChimp API key on line 15.

/**
 * Make a request to MailChimp API v3
 *
 * @param string $endpoint The MailChimp API endpoint for this request WITHOUT starting slash
 * @param string $type Type of request, whether GET, POST, PATCH, PUT, or DELETE
 * @param array $body The request body parameters
 *
 * @return $response object The response object
 * 
 */
function isa_mailchimp_api_request( $endpoint, $type = 'POST', $body = '' ) {
 
    // Configure --------------------------------------
 
    $api_key = 'YOUR_API_KEY';
 
    // 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;
 
    $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 ) );
 
    return $response;
}

The function above is just a wrapper for making any kind of request to the MailChimp API. Now, to schedule the campaign to be sent with batch delivery, use the following code. See below for the explanation and the necessary changes that you have to make to the code.

$campaign_id = '1a111a1aa1';

$body = array(
		
	'schedule_time' => '2017-08-22T16:00:00+00:00',// Required. UTC. Only allowed on the quarter-hour (:00, :15, :30, :45). The schedule time must be in the future.
		
	'batch_delivery' => array(

		'batch_count' => 3 // Optional. The number of batches for the campaign send. Can only be from 2 through 26.

		'batch_delay' => 60, // Optional. The delay, in minutes, between batches. Can be one of the following: 5, 10, 15, 20, 25, 30, 60.
			
		)
	
);

$schedule_campaign = isa_mailchimp_api_request( "campaigns/$campaign_id/actions/schedule", 'POST', $body );

// Log errors

if ( ! empty( $schedule_campaign->errors ) && ! empty( $schedule_campaign->errors[0] ) ) {
	foreach( $schedule_campaign->errors as $each ) {
		error_log( sprintf( 'Error scheduling MailChimp campain: %s', $each->message ) );
	}
} elseif ( empty( $schedule_campaign ) ) {

	// Campaign was scheduled successfully.

	error_log('MailChimp campaign was scheduled successfully.');

}

On line 1, change 1a111a1aa1 to your own campaign ID.

On line 5, change 2017-08-22T12:00:00+00:00 to your desired schedule date and time. This will be the date and time that the campaign will be sent to the first batch. MailChimp only allows this time to be on the quarter-hour. This means the minutes part of the time has to be either :00, :15, :30, or :45. So, examples of acceptable times are 12:00, 12:15, 12:30, 12:45, and 13:00. Examples of times that are not acceptable are 12:35, 12:11, 12:50, and 13:10. The time must be a UTC time. You can see the current UTC time at https://time.is/UTC. The schedule time must be at least one minute in the future.

On line 9 is where you set your desired number of batches (batch_count). Batch delivery splits your list of subscribers into batches. Then it sends your campaign to only one batch at a time, rather than to the whole list at once. This is where you set how many batches to split your list into. On line 9, change 3 to your desired number of batches. This is only allowed to be from 2 through 26.

On line 11 is where you set your desired delay time between batches (batch_delay). This tells MailChimp how long to wait between sending each batch. This is where you designate that number of minutes. On line 11, change 60 to the number of minutes that you want to wait between batches. This can only be one of the following: 5, 10, 15, 20, 25, 30, 60.

This example will log any errors. If the campaign is scheduled successfully, this message will be logged: “MailChimp campaign was scheduled successfully.”

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]