The Galahad MailChimp Synchronizer lets you synchronize an existing mailing list with MailChimp. The synchronization is one-directional (from existing to MailChimp) so is most useful for keeping MailChimp synced with something like a membership database or customer list. A base class is provided, so that you can write any custom logic necessary for your particular database, and two implementations are also included (MySQLi and Array). The Array implementation lets you sync an array of e-mail addresses with MailChimp (mostly useful for testing, or very small lists). The MySQLi implementation lets you sync a MySQL database with MailChimp, assuming there is no major processing that has to happen outside of the database. Here’s a quick example of how you might use the MySQLi implementation:
require_once 'Galahad/MailChimp/Synchronizer/Mysqli.php';
require_once 'MailChimp/MCAPI.class.php';
$apiKey = 'ENTER YOUR API KEY HERE';
$list = 'ENTER YOUR LIST ID HERE';
$db = array(
'host' => 'localhost',
'username' => 'root',
'passwd' => '',
'dbname' => 'mailchimp',
);
$cols = array(
'first_name' => 'FNAME',
'last_name' => 'LNAME',
'email' => 'EMAIL',
);
$selectQuery = 'SELECT email, first_name, last_name FROM `user`';
$existsQuery = 'SELECT COUNT(id) FROM `user` WHERE email = ?';
try {
$synchronizer = new Galahad_MailChimp_Synchronizer_Mysqli($apiKey, $db);
$synchronizer->sync($list, $selectQuery, $existsQuery, $cols);
echo "\nSuccess";
} catch (Galahad_MailChimp_Synchronizer_Exception $e) {
echo "\nError syncing: ", $e->getMessage();
}
A more complete example is included with the library files.
Keep in mind that if you get any individual errors (such as an invalid e-mail address or trying to sync someone who manually unsubscribed) the MailChimp API console will show errors for the listBatchSubscribe and listBatchUnsubscribe operations.
This is a pretty early version of the code, so you should test it thoroughly before using it.
Updated: Oct 5th, 2010 — Updated it to use the new API keys rather than a username and password, and also updated some API calls to match the latest version of the API.
Galahad MailChimp Synchronizer [96.14 kB]And, by popular demand:
Pingback: "Galahad MailChimp Synchronizer" by Chris Morrell