MailChimp is an email marketing service that is used for designing, sending and managing newsletters. One cool thing about MailChimp is that it offers a variety of ways to easily use its service in your website or project. For example, using a drag-and-drop feature you can create signup forms without writing a single line of code. However, if you want to have the full control over your project let’s put aside the beginner solutions and dive into the code instead. In this post you will learn how to use MailChimp API in PHP.
Add Individual Subscribers to a List
As it’s stated here, MailChimp API almost includes all the data and functionalities of the web application. Obviously, going through all the functionalities is out of scope of this post. Instead, I’m going to just explain how to add individual subscribers to a List.
1. Download MailChimp API wrapper for PHP
You can explore all the API wrappers for different programming languages here or download the latest official wrapper for PHP from here. After downloading, unzip the file and copy the content of
src folder (Mailchimp.php and Mailchimp folder) somewhere in your project. At the time of writing this post, the latest version of MailChimp API is 2.0. It is advisable first to get familiar with it by having a look at the documentation.
2. Create an API key and find the list ID
In order to interact with a MailChimp list you must have an API key and the list ID. So, assuming you’ve already got an account and a list with MailChimp, click on your name and then under “My Account” select “Account Settings”:
Now, click on “Extras” dropdown and select “API keys”:
Now, you can create a new API key if you haven’t got any. The next step is to acquire the list ID. So click on “Lists” menu and on the right hand side of the list’s row select “Settings” using the dropdown:
In the new page click on “List name & defaults” where you can see the list ID.
3. Coding part
$list_id = 'your_list_id';
$api_key = 'your_api_key';
$MailChimp = new MailChimp($api_key);
$result = $MailChimp->call('lists/subscribe', array(
'id' => $list_id,
'email' => array( 'email' => $email ),
'merge_vars' => array(
'FNAME' => $name,
'LNAME' => $lastname,
'CNAME' => $company,
'PHNUM' => $phone
'double_optin' => false,
'update_existing' => true,
'send_welcome' => false
Here in the preceding code, I assigned the API key and list ID to $api_key and $list_id variables respectively. Then I included the API wrapper using
include_once('Mailchimp.php') in the project and instantiated an object ($MailChimp) from MailChimp class. In the final step, I called “call” method and passed all the parameters to it including list ID, name of the method (
subscribe) and the subscriber’s information. The good thing about MailChimp is that you can define your own custom fields for subscribers in the list and include them in the API using
merge_vars. For instance, here there are 4 custom fields:
PHNUM. Other parameters include:
double_optin: By default an email is sent to the subscriber. You can disable it by setting it to false.
update_existing: If you set this parameter on, it overwrites the last values. Otherwise it raises an error saying that the email already exists.
send_welcome: Other than subscription confirmation, you can also disable the welcome email which is sent to a new subscriber.
Note that in some cases, for example if someone enters an invalid email like “firstname.lastname@example.org”, you will get a fatal error:
Uncaught exception ‘Mailchimp_ValidationError’ with message ‘This email address looks fake or invalid. Please enter a real email address’
In this case you need to come up with your own error handling and implement try and catch statements in the wrapper.
MailChimp API v2.0 Documentation for method
subscribe can be found here.