Adding a “Subscribe to Newsletter” checkbox or radio button to an existing contact form is incredibly painless using the MailChimp API. Include one library file, and you’ll have access to all functions the API has to offer. In this example, I added a Yes/No radio selection. You could easily change this to be a checkbox. When “Yes” is selected, and the form is submitted, the contact form does its thing, and then the user is added to a MailChimp List.

The HTML

A radio button with the name “emailUpdates” is added to the form. The name fields are used when adding this person to your MailChimp subscriber list.

Include the MailChimp API PHP Wrapper

Download the PHP “wrapper” class for the MailChimp API. This is also available for several other languages. In the below script, this is the required file “MCAPI.class.php”.

The PHP

Add this to the bottom of the PHP file specified in the form action. In my case, this was my email script “sendmail.php”. Note that you should add this before any redirects at the end of the script. They would look like “header(“Location: blahblah”)”. The PHP below is a barely-modified version of the official example code for “listSubscribe”. If checks if “emailUpdates” is set to “Yes”. If it is, the submitted e-mail address and name are inserted into the list in MailChimp.

You will need to log into your MailChimp account to copy and paste your API Key and your List Unique ID.

Comments on this Article

  1. Marcello says:

    Thank you very much for your hints. Worked at first try to integrate the MailChimp API into my shopping cart.

    Great info 🙂

    Cheers Marcello

  2. Sarah says:

    Hi,
    Thank you for your tutorial. It is exactly what I need to do but unfortunately it is taking waaaay longer than 5 minutes 🙁 Here’s the url of the contact form I’m trying to add a MailChimp subscription button too: http://www.wileyvalentine.com/contact-us

    I believe I have the information right in the form area but the php is where I’m getting lost. I did download the MailChimp API files and put in my information and uploaded those but I’m not sure if I did it correctly. Let me know if I can provide you with any code that will be useful and I greatly appreciate any help that is offered.

    Thanks.

    • Josh says:

      It looks like your contact form is being directed to the same page (the form “action” is empty) to be processed. So you would need to find where the form’s $_POST variables are being used to send the email on the contact-us page, and add the PHP code there. Then make sure the MCAPI.class.php is in the same directory as the contact-us page (or the PHP template file that is loading it, if you are in a CMS). Hope that helps.

    • Josh says:

      Just a guess, judging by the hidden field; this might be within something like
      If ($_POST['process'] == 'true'){

    • Sarah says:

      Josh,

      Thank you for taking the time to reply. I really appreciate it! Moving the code up about 20 lines made all the difference and now an email subscription confirmation email is being sent once the form is submitted. Unfortunately though some wires are getting crossed. When the form is submitted, the information is coming to our design@wileyvalentine.com address which is correct. However in the from line it’s coming from the design account as well, not from the user’s email. And then the MailChimp subscription confirmation email is coming to the design address as well. Do you have any ideas why this is happening? Once again I appreciate any help you can offer.

      Thanks!

      • Josh says:

        I’m not sure why that is without looking at the code. Did you figure it out? I see the user’s email is correctly named ’email’ on your form, so the Mailchimp subscription ($_POST[’email’]) should be going there.

    • Sarah says:

      Yes I did! It was a minor mistake in the config.inc.php file that was causing all of the hassle. I have a few more kinks to work out but overall we’re good to go! Thanks again for a great tutorial and all of your help!

  3. El garch says:

    Hello,

    Thank you so much, works great for me.
    Is it possible to send multiple emails on the same time, something like that :

    $api->listSubscribe($list_id, $list_emails, $merge_vars) ?

    • Josh says:

      Looking at the API docs, it looks like it only takes a single email address (string), not an array. You could just call the function again for the other email address(es).

  4. Erwin says:

    Is there a way to disable subscription confirmation emails? I’m trying to do a Contact Form and I don’t want sending confirmation emails.

  5. Adam Davies says:

    This is great. I have implemented this into my contact form and it works great.

    Is there a way of having the option for 2 different lists in the same account so…..

    $list_id = $list1;
    
    if ($othermailing == "yes") {
     $list_id = $list1.$list2;
    }
    

    Can this be used in the subscribe function?

    Or can you only have the user join 2 mailing lists with the same form and using only one block of code.

    Thank you so far.

    Regards

    • Josh says:

      For adding to a separate list I’d try running the same function again, with the different list name. But..
      You may also want to look into using groups rather than separate lists, as you can have duplicate subscribers between lists; unsubscribes work separately for each list too. If it’s the same related list of subscribers, and you’re just organizing them based on what they’d like to receive, then groups are recommended by Mailchimp (http://kb.mailchimp.com/article/what-is-a-group-and-why-would-i-want-to-set-one-up). It looks like the listSubscribe method has a GROUPINGS parameter, that could be used to pass along the groups someone should be added to http://apidocs.mailchimp.com/api/1.3/listsubscribe.func.php

  6. Keith W. says:

    Josh You ROCK! This will help me greatly.

  7. joz says:

    hey josh, love it!! 🙂

    but, while trying to adapt it ive broke it.

    i think its due to including $error as part of the response after sending a msg.

    could you help me out pls? 😉

    • Josh Winn says:

      What is the error your are receiving, and what is the code you are having an issue with? (code in the comment can be posted in a PRE tag)

  8. Tony B says:

    Hi Josh,

    Cheers for the great post!
    Was just wondering if you knew how to integrate this into a bigcartel contact form? I think it would be the same concept as WordPress etc. Bigcartel doesn’t use PHP.

    Thank you and hope you can help!

  9. Rich says:

    Is there anyway to integrate it into ASP classic email form? I can’t find any support on the MailChimp site. I know its old school, but thats the way it is at the moment on my clients website.

    • Josh says:

      I do not know if they have an ASP example. You should check with Mailchimp support.

  10. Steven says:

    Your help would be so appreciated! I am trying to do this with a checkbox instead of radio buttons. I think I downloaded the correct php wrapper (Mailchimp.php)? I think I also put in the php correctly to my form file. I put in my ID’s and all but I just can’t get any email address to appear in Mailchimp. What am I doing wrong?

    • Steven says:

      I figured it out. Gotta use this code for API 2.0:

      include(‘Mailchimp.php’);

      $MailChimp = new MailChimp(‘API KEY’);
      $result = $MailChimp->call(‘lists/subscribe’, array(
      ‘id’ => ‘LIST ID’,
      ’email’ => array( ’email’ => $_POST[’email’] ),
      ‘merge_vars’ => array(
      ‘MERGE2’ => $_POST[‘name’] // MERGE name from list settings
      // there MERGE fields must be set if required in list settings
      ),
      ‘double_optin’ => false,
      ‘update_existing’ => true,
      ‘replace_interests’ => false
      ));

      if( $result === false ) {
      // response wasn’t even json
      }
      else if( isset($result->status) && $result->status == ‘error’ ) {
      // Error info: $result->status, $result->code, $result->name, $result->error
      }

  11. Michaela says:

    Hi Josh

    Thank you for the tutorial. I was wondering if I could do this with a Square Space contact form?

    Thank you
    Michaela

    • Josh says:

      As far as I know, not with their built-in contact form block. It would have to use all custom code, or Squarespace would need to add that feature; check with their support about that.

Comments are closed.