Mailchimp integration with php

Integrating mailchimp form with php

As i said in my earlier post Introduction to mailchimp , Mailchimp allows one to send newsletters to their list subscribers. In this tutorial we will explain how to add a customizable form and add subscribers to your Mailchimp subscribers list.

First of all go to http://www.mailchimp.com/ and signup for a new account. After the signup and login formalities you will get a dashboard page which will look similar to the below image.

 

as

Click on the list on the top bar and in the new page opened click on create list. In the new list page fill in the details like list name, default list from address etc..

So now we have created a new list in Mailchimp. Before we start the php coding section we need some informations to get started. First you need to find the list id where the form needs to add subscribers to.

To find this Click on the lists page, then to the right far side of the recently create list , you will see a drop down menu. Click on that and go to settings. Scroll down to the bottom of the page, you will find a box with an id saying unique id for list newsletter. Save that id. you need that in future.

2

Go to https://bitbucket.org/mailchimp/mailchimp-api-php/downloads and download the Mailchimp php api files. Extract the files and keep the files from src folder of the downloaded files in a folder in your current project. Thats the files we need to call the Mailchimp api.

 

Next we need a Mailchimp API key. In your Mailchimp account there is a dropdown menu on top with your account name. Click on that and select account. In the account page , click on Extras, API keys. In the new page click on the button create a key. It will generate a new key . You can see the API key in the section above the button. Copy the API key and save it for future use and make sure the status shows a green color next to the API key on the api page.

3

Next lets create mailchimpform.html page where the form to retrieve the user email is located.


<form id="mcform" method="post" action="mailchimp_add.php">
<input type="email" name="email" placeholder="Enter your email" required="required"/>
<input type="submit" value="Subscribe"/>
</form>

The above form will submit a post request to the page mailchimp_add.php with email as post field.

Next is the main page where the user submitted email is retrieved and is sent to the mailchimp along with the list id to which the email has to be added

mailchimp_add.php


<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$api_key = "< YOUR API KEY GOES HERE >";
$list_id = "< YOUR LIST ID GOES HERE >";
$subemail=$_POST['email'];
require('src/Mailchimp.php');
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$subscriber = $Mailchimp_Lists->subscribe( $list_id,array('email' => htmlentities($subemail)));
if ( ! empty( $subscriber['leid'] ) ) {
  // message to show when the subscriber has successfully subscribed
}
else
{
 // message to show when the subscription failed
}
}
else {
echo 'Unauthorized access';
}
?>

So this piece of code gets the email from the form and passes the email to the mailchimp. Remember to replace the <YOUR API KEY GOES HERE> and <YOUR LIST ID GOES HERE> with your api key and list id.

Hope this helps to integrate this small service to your projects. Comment below on any queries and tutorial requests. Will be updating with more articles soon.

Leave a Reply