MailChimp API Subscribe or Unsubscribe User Using PHP Script

Advertisement

MailChimp is one of the top email marketing services you can use out there, it’s freemium (it offer free and premium services), it allows you to use their skinned HTML or styled HTML code ready just copy and paste into your site and you’re done, but what if you need something more than their default provided form? Perhaps subscribe user on form submission without them knowing (evil move), this article is intended for those who looking to create custom user subscription or unsubscribe user using PHP script.

This script is handy for people who are looking for one of the following:

  • Marketing: Subscribe user after form submission
  • User engagement: Subscribe user on Ecommerce site registration
  • And more.

What We Need

  • MailChimp PHP API (download here)
    • a. We’ll use this API to connect to MailChimp
  • MailChimp API Key
    • a. Key can be found in your MailChimp Profile Dropdown Menu -> Profile -> Extras -> API Keys
    • b. Click Create A Key button to create a new API Key
    • c. Grab and save the key for later use
  • MailChimp List ID
    a. List ID can be found on your list of created Lists, Lists -> Click Create a List button if you have no List added yet, else, -> Click Dropdown Arrow (right side of each list) -> Settings -> Scroll to the bottom section and you’ll find Unique ID from there (copy and save it for later use)

So now we’re ready, are you?

We will not cover form submission here, so we assume form is submitted already and ready for subscription

Subscribe: PHP Code


// Link to the earlier download MailChimp API path
require('mailchimp/Mailchimp.php');

// MailChimp API key and List ID
$api_key = "8e15a0bebc782eaaf18838d40031324b-us2";
$list_id = "f7417c0019";

// Get user submitted data
$email      = $_POST['user_email'];
$first_name = $_POST['user_firstname'];
$first_name = $_POST['user_firstname'];

/*
 * If you want to filter user data do it here as early
 * Like check if user added valid Email address
 */

// Iniate MailChimp class
$Mailchimp        = new Mailchimp( $api_key );
$Mailchimp_Lists  = new Mailchimp_Lists( $Mailchimp );

$subscriber = $Mailchimp_Lists->subscribe(
  $list_id,
  array('email' => $email),                               // Specify the e-mail address you want to add to the list.
  array('FNAME' => $first_name, 'LNAME' => $last_name),   // Set the first name and last name for the new subscriber.
  'html',    // Specify the e-mail message type: 'html' or 'text'
  FALSE,     // Set double opt-in: If this is set to TRUE, the user receives a message to confirm they want to be added to the list.
  TRUE       // Set update_existing: If this is set to TRUE, existing subscribers are updated in the list. If this is set to FALSE, trying to add an existing subscriber causes an error.
);

if ( ! empty($subscriber['leid']) ) {
  // Email added successfully added
} else {
  // Error adding user
}

Unsubscribe: PHP Code


// Link to the earlier download MailChimp API path
require('mailchimp/Mailchimp.php');

// MailChimp API key and List ID
$api_key = "8e15a0bebc782eaaf18838d40031324b-us2";
$list_id = "f7417c0019";

// Get user submitted data
$email      = $_POST['user_email'];

/*
 * If you want to filter user data do it here as early
 * Like check if user added valid Email address
 */

// Iniate MailChimp class
$Mailchimp        = new Mailchimp( $api_key );
$Mailchimp_Lists  = new Mailchimp_Lists( $Mailchimp );

$unsubscribe = $Mailchimp_Lists->unsubscribe(
  $list_id,
  array('email' => $email),      // Specify the e-mail address you want to add to the list.
  TRUE,       // Flag to completely delete the member from your list instead of just unsubscribing, default to false
  FALSE,      // Flag to send the goodbye email to the email address, defaults to true
  FALSE       // Set update_existing: If this is set to TRUE, existing subscribers are updated in the list. If this is set to FALSE, trying to add an existing subscriber causes an error.
);

if( $unsubscribe ) {
  // User unsubscribe successfully
} else {
  // Error
}

That’s it happy marketing 🙂

Advertisement