Mailchimp is one of the most famous free online email marketing software. By sending email newsletter with mailchimp, it is also easy to integrate email signup form in web application as well as website. Today I am going to show you how to integrate custom web from with mailchimp by PHP.

Download Source Code

What you need?

Account with Mailchimp

( Beside premium account, there is a free sign up option where you can send 12000 emails to 2000 recipients per month)

jQuery
Mailchimp PHP API class

How to Start

Starting should be simply creating index.php file. The code may like follows-
Mailchimp API Integration with PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mailchimp API Integration with PHP</title>
</head>
<body>	
<h1>Mailchimp API Integration with PHP </h1> 
<div class="message"></div>
<form  role="form" method="post" id="subscribe">
Email :<input type="email"  id="email" name="email" placeholder="Write your email" > <br>
<button type="submit">Subscribe Now !</button>
</form>
</body>
</html>	

Java Script form checking

Now, let’s check whether form is filled up properly or now. Following code will check that part and send form data to PHP files in CURL mood. Best place to put this code inside tag.

This is highly important to make sure that, CURL is enabled in your server. Otherwise this will not work.

<script type="text/javascript">
$(document).ready(function() {
$('#subscribe').submit(function() {
if (!valid_email_address($("#email").val()))
{
$(".message").html('Please make sure you enter a valid email address.');
}
else
{
$(".message").html("<span style='color:green;'>Almost done, please check your email address to confirmation.</span>");
$.ajax({
url: 'check.php', 
data: $('#subscribe').serialize(),
type: 'POST',
success: function(msg) {
if(msg=="success")
{
$("#email").val("");
$(".message").html('<span style="color:green;">You have successfully subscribed to our mailing list.</span>');
}
else
{
$(".message").html('Please make sure you enter a valid email address.');  
}
}
});
}
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);
}
</script>

Before that, you have to include jQuery file in index.php file like follows-

<script src="jquery.js" type="text/javascript"></script>

Create check.php file

Now create check.php file where submitted form data will process to send server with the help of Mailchimp API Class . In this file, you have to include mailchimp class titled Mailchimp.php that you have downloaded.

<?php
$api_key = "Put your mailchimp API";
$list_id = "Put List ID";
require('Mailchimp.php');
$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";
}
else
{
echo "fail";
}
?>

This is basically configuration file where you have to put mailchimp account API and List ID.

How to get Account API and List ID?

For getting mailchimp account API, you have to login in mailchimp account and follow the steps given below

STEP 1: Go to your Account Settings -> Extras -> API Keys

STEP 2: Click on Create a Key button and sure enough that you will get your API key.

Now you have to choose in which list you want to store all of your subscribers information. In this case, you have to create a list in List option.

STEP 3: Go to Lists section, Create a new List.

STEP 4: enter created list and find Settings->List name & defaults. Then you will find List ID

That’s wonderful. You have successfully integrated Mailchimp API with custom PHP form. Now, visit your index.php page where you will get a simple html form. Hit Subscribe now button after putting your email. If everything is going smoothly, you will get a notification that you have received an email. Check your email where you will receive an activation link because mailchimp will not add any email address without confirmation from email owner.

However, after activation, you will receive another email confirmation that you have successfully enlisted in mailchimp list. Now, check your mailchimp list that you have received newly listed email.