Integrating Moodle with MailChimp

Although Moodle does support group mailing, it lacks any of the newer bells and whistles that are common to email marketing, such as campaign management tools and the ability to generate metrics. I've recently started using MailChimp for this kind of stuff and highly recommend it for the combination of being powerful and flexible at the same time. Although it doesn't offer any plug and play path for Moodle integration, it does expose an API so getting both systems to work together is fairly easy.

So lets say we want to automatically subscribe any new Moodle users to a MailChimp list. We could generate a list of the users and then import that into MailChimp, but why work harder when we can work smarter? Lets start by examining the Moodle sign up process. When a new user signs up they will receive a confirmation email asking them to validate their account. That's achieved by visiting /login/confirm.php. So the obvious place to make the MailChimp API call is from within the confirmation file. Waiting until after confirmation also ensures that we don't get fake users subscribing to the list. Have a look in confirm.php for the following line:

if ($confirmed == AUTH_CONFIRM_OK)

Anything inside that if block will only occur for a newly validated user.

you'll notice that the user data still isn't loaded. Look for the following function call inside the if block:

get_complete_user_data(...

at this point you have all the data you'll need.

Ok, so now we know where to make the call from, lets look at the MailChimp API. You can download the API file and a bunch of other really useful example files here:

http://apidocs.mailchimp.com/downloads/#php

There are really only two files that you'll need;

MCAPI.class.php (the API itself)
and
config.inc.php (your configuration file)

Configuration is fairly trivial. You just add your API key, your campaign number and the list number. Note that the list number is fairly hard to find in MailChimp, so the fastest way to grab it is to go to the list page and just copy id=1234567 from the querystring in the address bar of your browser, (obviously 1234567 will be different for every list).

Now we'll add the API call to our confirmation page. Values from Moodle are in bold.

require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey

$api = new MCAPI($apikey);

$merge_vars = array('FNAME'=>($USER->firstname), 'LNAME'=>($USER->lastname));

$retval = $api->listSubscribe( $listId, ($USER->email), $merge_vars );

Easy huh? Not so fast. Sure you've just ensured users get subscribed automatically, but they are going to receive three emails... one from Moodle, one welcome email from MailChimp and then a third confirmation email from MailChimp. That's probably enough to get them thinking you're a spammer and to unsubscribe immediately. MailChimp prefers this double confirmation step but you can get around it by modifying the API call.

Lets go back to the MCAPI.class.php file and have a look for the listSubscribe function.

function listSubscribe($id, $email_address, $merge_vars=NULL, $email_type='html', $double_optin=true, $update_existing=false, $replace_interests=true, $send_welcome=true)

You'll see two parameters that can be changed. Make the following change:

$double_optin=false and $send_welcome=false

There you go; now the only email that your users receive will be the initial Moodle confirmation mail. Play around with MailChimp for a while and you'll see just how useful it is.