How to do MailChimp Integration in Your Website

Are you looking to integrate the MailChimp newsletter on your website? MailChimp is one of the popular email marketing service, which manages the subscribers for your website. It demands to interact with the Mailchimp API to add this service to your website. In this article, we study about MailChimp integration on a website with the help of PHP.

Integrating MailChimp into the website is very simple. You just require to add some piece of code and you will be able to manage your subscribers easily.

I have written an article in the past which also explains integrating MailChimp into your website. In that article, I wrote the plain PHP code to interact with the MailChimp API. In this article, I’ll use the GitHub library for the MailChimp integration.

Get MailChimp API Key And Audience ID

For getting started, grab an API key and Audience ID of your MailChimp account. The API key is mandatory while giving a call to MailChimp API. The Audience ID is being used for adding subscribers to the specific list.

Login to your MailChimp account. Under the user icon, select the Account.

account-link

Click on Extra->API keys.

Click API Keys

Under the Your API keys section, click on Create A Key and copy your API key.

API Key

Now you have your API key ready. Next, get an Audience ID to which you need to add your subscribers. For this, click on the Audience menu and then select the option Settings from the Manage Audience drop-down.

audience

Under the Settings click on the Audience name and defaults.

audience-name-defaults

On the next page, you will find your Audience ID.

Audience ID

Add Subscriber To MailChimp List

You are now ready with your API key and list id. Next, you need to install this GitHub library using Composer.

Open the terminal in your project root directory and run the command:

composer require drewm/mailchimp-api

Upon installing the library, include the package environment in your PHP file. We do that in the following way.

<?php
require_once "vendor/autoload.php";

use \DrewM\MailChimp\MailChimp;

$api_key = 'YOUR_API_KEY';
$list_id = 'LIST_ID';

$MailChimp = new MailChimp($api_key);

Make sure to replace placeholders with the actual values.

To add a subscriber to the list, we have to pass the email address and status. There are two types of status values.

  • subscribed : This status adds the email address directly to the MailChimp list.
  • pending : A confirmation email will send to a user. After confirmation, the user will be added to the list.

We can add a subscriber to the specified list through API using the code below:

//subscribe a user
$result = $MailChimp->post("lists/$list_id/members", [
     'email_address' => 'PASS_EMAIL_ADDRESS_HERE',
     'status'        => 'subscribed',
]);

if(isset($result['id'])) {
    echo 'User subscribed successfully.';
}

You may want to add audience fields for the subscriber. These audience fields are used to store extra information about the user. Let’s say you want to add values for the fields FNAME, and LNAME which are default in MailChimp. In that case, after the subscription code, add the code below.

$subscriber_hash = MailChimp::subscriberHash('PASS_EMAIL_ADDRESS_HERE');
$MailChimp->patch("lists/$list_id/members/$subscriber_hash", [
    'merge_fields' => ['FNAME'=>'FIRST_NAME', 'LNAME'=>'LAST_NAME']
]);

The user can also assign tags to the subscribers. You can organize contacts with tags. Let’s say I want to add a ‘Blogger’ tag to the subscriber, so the code will be as follows.

$subscriber_hash = MailChimp::subscriberHash('PASS_EMAIL_ADDRESS_HERE');
$MailChimp->post("lists/$list_id/members/$subscriber_hash/tags", [
    'tags' => [
                ['name' => 'Blogger', 'status' => 'active'],
            ]
]);

You can assign multiple tags by using more array elements. If you want to remove the tag then pass the status value as ‘inactive’.

Delete A Subscriber From MailChimp List

For deleting a subscriber from your MailChimp list, you need to pass the md5 hash of the email to the delete() method of the MailChimp instance. The library provides a function subscriberHash that creates the md5 hash of the email address.

//delete a subscriber
$subscriber_hash = $MailChimp->subscriberHash('PASS_EMAIL_ADDRESS_HERE');
$result = $MailChimp->delete("lists/$list_id/members/$subscriber_hash");

That’s it! It’s all about adding or deleting subscribers of MailChimp. I hope you understand MailChimp integration on a website with PHP. Please share your thoughts in the comment below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “How to do MailChimp Integration in Your Website

Leave a Reply

Your email address will not be published.