Add a MailChimp subscribe feature to your contact form

MailChimp is a great email marketing service provider with an easy to use control panel and features like: campaign management (RSS-driven, A/B Split, Plain-text and regular), statistics, auto-responder and a complete set of list management tools. They offer different ways to place a subscriber form on your website or blog. If you to like add the MailChimp subscribe feature to your existing contact form you need to add some custom code. 

A subscribe box on your contact form, is a huge opportunity to get more subscribers for your newsletter. Most people will use this tiny subscribe box much faster than using a standard MailChimp subscribe form!

This tutorial is about how to use the MailChimp subscribe feature on your existing contact form. The tutorials steps should be easy enough to add the code to your own contact form, even if you’re not a PHP developer. This tutorial is a complete rewrite from an older version, because the old tutorial code didn’t worked with the current MailChimp API system (version 2). So if you already use the old code, you need to update!

MailChimp subscribe feature


  • If you don’t have a MailChimp account, register one at MailChimp. They offer a free account for the first 2.000 mailing list members and for 12.000 messages a month. Sign-up now for free and get a $30 bonus if you upgrade your account later.
  • Setup a mailing list via the MailChimp control panel and obtain the Unique ID (via Lists -> Your List -> Settings, bottom page).
  • Next you need an MailChimp API Key, create one from the control panel (Account -> Extras -> API Keys)
  • As an option create a custom field (Lists -> Your List -> Settings -> List Fields and *|MERGE|* Tags) and name it subscription_via. Don’t change the other variables. We use this additional merge field to store the information where a subscription happens with the contact form we build in this tutorial.

HTML  for the contact form 

First you need is contact form with form fields for name, e-mail address, a check-box for the subscription and of course a text-area for the sender’s message. I use a basic HTML example for better understanding. In the example below the form is processed by a script names “process.php”.

MailChimp API wrapper

The MailChimp API offers several ways to communicate with the MailChimp system. If you work mostly with PHP you can use the official API wrapper. I use for this tutorial the simplified MailChimp API Wrapper by Drew McLellan because this PHP class is much easier to use. Add the following code to your PHP mail script which is used to process your contact form, in our case it would be the file “process.php”. Choose a location in your code right after the mail is send, that could be after the use of the mail() function or the code your use from PHPMailer. 

The code from top to bottom:

  1. Include the MailChimp wrapper class (use the right path to prevent a PHP error)
  2. filter the form input using the filter_var() function
  3. Create a new object with your MailChimp API key as argument
  4. Define the merge vars: IP address, first name and the optional merge var we defined before in MailChimp
  5. Make an API call to the “lists/subscribe” end point using the merge vars and some other array values. Don’t forget to enter here the unique mailing list ID. The other values are some kind of default change them if necessary.

After API call is done, your script will receive a response from the MailChimp API system. This response is stored as an array inside the variable $response.

High Performance Cloud

With the code below were are able to check/validate the response. Add the code below the previous part.

If the response contains an array element with the key “euid”, the subscription was successful. The subscriber will receive an email message with the conformation link (double-optin). Otherwise we use the error code from other array elements to decide which (error) message the script needs to show. My example is using only code “214”, which means that a subscriber is already a member. Check the MailChimp API documentation (link below) for additional code values.

MailChimp subscribe tutorial code

As mentioned before this tutorial is about how to add the MailChimp subscribe feature to your existing contact form. The code below is just a concept how the HTML form could be processed. Your PHP mail script will look different and my advice is to test this modification on your test location first. 

I assembled a working example which is available as download here. If you’re interested to try the example first check the MailChimp demo here.

MailChimp resource links

MailChimp subscribe/list API – If you need further information about the API endpoint I’ve used for this tutorial.

Official PHP wrapper – The API wrapper for PHP5 maintained by MailChimp.

MailChimp error codes – The list of error codes MailChimp might return as a response for your subscription request. Note this list is created for the API version 1.3, but the codes should be valid for current version, too.

12 thoughts on “Add a MailChimp subscribe feature to your contact form

    • Hi,

      me too, check the tutorial code :)
      This tutorial is for the people didn’t know how to add a subscribe function to their contact form.

  1. Hi Olaf,

    thanks for the example, using your code it was much easier to understand how it works.
    I used your code for some older site with a hand-written script and now I’m looking for a similar function for WordPress. Is it possible to add this feature to a comment form as well? I see your blog has this function already, but this is a plugin right?

  2. hello,
    I used this and working fine.. Thanks :) but user need to accept email to subscribe. Can we add user’s email without subscribe email?

    • Hi,

      sure you can do this.
      Change the double-optin attribute to “false”: 'double_optin' => false,

      • thanks.. :) I will check that..

  3. I downloaded your form entered my Api key, list id, and email. I get a two lines Warning: unexpected character in input: “\” (ASCII=92) state=1 /the file location/contactform.php on line 19. and Parser error: syntax error, T_STRING in /the file location/ contactform.php on line 19.

    any suggestions?

    Thanks, Dave

    • Dave,

      looks like your PHP version is a bit outdated, this object declaration requires PHP 5.3 or higher:

  4. Thanks for the writeup. I am having a bit of trouble however. I downloaded the example files and replaced the API Key and the list ID values. I uploaded the folder to my server and tried filling in the form. I just receive a blank page. Nothing goes to mailchimp, i dont receive any validation errors like in your demo either. Any help would be appreciated.

    • Hi Darrell,
      I think your PHP version is too old. You need at least PHP version 5.3 or higher.
      Let me know if it’s something else.

  5. Hi Olaf,

    Thanks for this tutorial, I used the same API Wrapper and it worked well.
    But yesterday it stopped for no reason.
    The result code I get is 1, I guess that means the function runs well? I cannot find this information on the Mailchimp API page.


  6. Hi Alexis,

    does this problem still exists? What is the complete server response you get?

Comment Rules

Don’t post your code here, post your code block or snippet to pastebin and include the pastebin URL in your comment.

I delete all comments with non related links inside the comment text. Don't use keywords for the field of your real name (most people like to use your name for their answer). Keep your comment related to the topic, if your question is off-topic, please use the contact form instead.

Leave a Reply

Your email address will not be published. Required fields are marked *