MailChimp API – Subscribe User Email Using PHP

MailChimp let your users to subscribe and you can send them newsletters in bulk and manage your subscriber’s lists. Although MailChimp provide forms and lot of APIs and functions but for that you need to google and search, it takes time.

Here I’ll show you how you can simply write some code in to your site while keeping your users on site without going to MailChimp.

Here are the necessary steps

Sign Up on MailChimp

After successfull account creation and activation, Generate your API keys and List ID.

  1. Go to your Account Settings -> Extras -> API Keys
  2. Click on Create a Key button and you will have you API  key ready.
  3. After that go to Lists section, Create List, Visit your created lists under Settings -> List name & defaults you will find List ID.

Now you need to download API files

Download MailChimp API

I am using version2.0 API.


MailChimp API send request through CURL so make sure you have enabled CURL in your PHP.
Create a php file subscribe.php
Place this inside subscribe.php file.
$api_key = "YOUR_API_KEY";
$list_id = "YOUR_LIST_ID";

$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => htmlentities($_POST['email']) ) );

if ( ! empty( $subscriber['leid'] ) ) {
   echo "success";
    echo "fail";



Create a index.html, place following code
<div class="message"></div>
<form  role="form" method="post" id="subscribe">
    <input type="email"  id="email" name="email" placeholder="" value="">
    <button type="submit">SUBSCRIBE</button>


Add jquery <script src=“jquery.min.js” type=“text/javascript”></script>
$(document).ready(function() {
    $('#subscribe').submit(function() {
        if (!valid_email_address($("#email").val()))
            $(".message").html('The email address you entered was invalid. Please make sure you enter a valid email address to subscribe.');
            $(".message").html("<span style='color:green;'>Adding your email address...</span>");
                url: 'subscribe.php', 
                data: $('#subscribe').serialize(),
                type: 'POST',
                success: function(msg) {
                        $(".message").html('<span style="color:green;">You have successfully subscribed to our mailing list.</span>');
                      $(".message").html('The email address you entered was invalid. Please make sure you enter a valid email address to subscribe.');  
        return false;
function valid_email_address(email)
    var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
    return pattern.test(email);

Now you have done it, enjoy you coding with PHP and mailchimp API :) cheers :)

About the author

PHP | MVC | Codeignitor | Zend | Yii | Smarty | Android | Laravel Expert Senior Software Developer.I love my job and feel happy to working on new ideas and technologies.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Name (required)
Email (required)