Dynamically send MailChimp newsletter via PHP

Today, I am going to demonstrate how to dynamically create, manage and send MailChimp campaign to a List via PHP. You will also learn how to insert dynamic content in editable content area inside template before sending email newsletter.

In case you are looking to add subscriber to MailChimp List using API, here’s article for you: Subscribe users to MailChimp List using PHP.

For this tutorial, we are using MailChimp API v3 wrapper written by Drew McLellan for our purpose. It is Super-simple, minimum abstraction MailChimp API v3 wrapper.

Let’s start our adventure with MailChimp.

What do you need?

First of all, you need MailChimp API key. You need to find or generate API key. For this, you have go to MailChimp API keys page by clicking your profile name to expand the Account Panel, and choosing  Account >> Extras drop-down >> API key. From this page, you can copy existing key or create new key.

After having API key, you need to create a list or using existing one. Then add some subscribers to the list. We need ID of this List to send newsletter. To find the List ID, navigate to the Lists page, click the drop-down menu next to the list you want to work with, and then choose Settings. Scroll to find the unique List ID field. The string of letters and numbers is the list ID. 

Finally, we need to create a template. In order to create a template, navigate to Templates page and click the Create Template button. If you want to insert content dynamically in the template, select ‘Code Your Own’. To add editable content area in the template, add attributes like mc:edit=”body” in the container. Save the template and note down the Template ID.

Creating a new MailChimp campaign

You can create a new campaign using POST /campaigns API call. Here’s how to do it with PHP:

use \DrewM\MailChimp\MailChimp;

$MailChimp = new MailChimp($your_api_key);

// Create or Post new Campaign
$result = $MailChimp->post("campaigns", [
    'type' => 'regular',
    'recipients' => ['list_id' => $list_id],
    'settings' => ['subject_line' => $newsletter_subject_line,
           'reply_to' => $reply_to,
           'from_name' => $from_name
          ]
    ]);
  
$response = $MailChimp->getLastResponse();
$responseObj = json_decode($response['body']);	

Manage Campaign Content

After creating Campaign, you can add dynamic content to the campaign. To insert dynamic content, you should send GET request with template_id  and sections to be inserted as follows:
GET /campaigns/{campaign_id}/content

// Manage Campaign Content
$html = file_get_contents('url_of_dynamic_content');
$result = $MailChimp->put('campaigns/' . $responseObj->id . '/content', [
      'template' => ['id' => $template_id, 
        'sections' => ['body' => $html]
        ]
      ]);

Send Campaign

Finally, we send newsletter to the List by calling:
POST /campaigns/{campaign_id}/actions/send

// Send Campaign		
$result = $MailChimp->post('campaigns/' . $responseObj->id . '/actions/send');

In this way, you can send MailChimp newsletter using PHP. Isn’t is so simple ? 🙂

In upcoming days, I am going to share how to send newsletter using Amazon Simple Email Service (Amazon SES).

12 Comments

Leave a Reply