MailChimp is one of the most popular email marketing services available on the web. There are several other services like Constant Contact and AWeber, but MailChimp provides 2000 subscribers for 0$ which is perfect for startup projects.

Few months ago my task was to integrate a small website with MailChimp’s API using PHP. Before I start with implementationonal details, I want to share with you the following facts:

  1. You can’t add a subscriber to the MailChimp list using API, if user didn’t confirmed his/her subscription to the list, meaning you can’t add bunch of random emails to your list
  2. MailChimp API works with HTTPS but that doesn’t mean you have to install certificate on your website

I will separate integration with MailChimp into three steps installation, implementation and testing.

Here are also some useful links:

  • You can find all available MailChimp’s API libraries on this link.
  • You can download MailChimp’s PHP library on this link.
  • You can see all available API methods here.

Installation

  • Download the library from the following link.
  • In the .rar file find the folder src, and extract its content to your website’s folder (in my case it is C:\wamp\www\MyTestProject).
  • Import Mailchimp.php into your project using require_once PHP function.
  • Now you can create new Mailchimp object using the following command:
  • $mailChimp = new Mailchimp("YOUR API KEY");
  • In your MailChimp account create new API key, by opening your account profile page, then clicking Extras->API keys.
  • Here is example of the API key: 1891634555wee80aceaakeld1ab707c-us3.
  • $mailChimp = new Mailchimp("1891634555wee80aceaakeld1ab707c-us3");

Implementation

In the implementation I will show you how to add user to a subscription list in the MailChimp. Here is definition of the “lists/subscribe” method:

You can read more detailed explanation of the method on the following link.

As you can see from mathod definition, we have to provide the following fields:

Field name Field explanation
id ID of the list available in the MailChimp
email Customer’s email
update_existing weather or not to update existing record
send_welcome Send welcome email to customer

As you can see, the Mailchimp object has one method called call. The first parameter is the MailChimp’s API method name like “lists/subscribe“, and the second parameter is PHP array with key=>value pairs.

As first parameter do not pass full url like: https://us2.api.mailchimp.com/2.0/lists/subscribe, because the first part of the link is already generated in Mailchimp constructor.

Testing

If everything runs without error, method call will return you some values. Here is example value:

These values can be stored in your database, and uniquely identifies customer in the MailChimp.

Known errors

If you during runtime encounter the following error:

API call to: lists/subscribe.json failed: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

then you have to perform the following edits in Mailchimp.php class:

  • Download .pem file from this location http://curl.haxx.se/ca/cacert.pem
  • Copy this .pem file in the folder where Mailchimp.php file is located
  • Open Mailchimp.php file, and add the following code after line 201: curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');

 

How to integrate your website with Mailchimp API using PHPhttp://code-epicenter.com/wp-content/uploads/2015/03/mailchimp-logo.pnghttp://code-epicenter.com/wp-content/uploads/2015/03/mailchimp-logo-150x150.pngAmir DuranLibrariesPHPProgrammingAPI,MailChimp,PHP
MailChimp is one of the most popular email marketing services available on the web. There are several other services like Constant Contact and AWeber, but MailChimp provides 2000 subscribers for 0$ which is perfect for startup projects. Few months ago my task was to integrate a small website with MailChimp's...