About Chris Morrell

I am a Philadelphia web designer and developer who focuses on PHP development and usable design. I am also the Director of IT for the International Association of Certified Home Inspectors.

Please Note: My site fell victim to a Wordpress security flaw a few weeks ago, and I'm just getting everything back to normal. Please bear with me.

I am currently not accepting any new clients.

Other Sites/Clients

Contact Me

If you need to get in touch with me, my name is Chris and my domain name is cmorrell.com. Think about it.

Galahad MailChimp Synchronizer

Galahad MailChimp Synchronizer

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';

$username = 'MAILCHIMPUSERNAME';
$password = 'MAILCHIMPPASSWORD';
$list = 'LISTID';

$db = array(
	'host' => 'localhost',
	'username' => 'DBUSERNAME',
	'passwd' => 'DBPASSWORD',
	'dbname' => 'demo',
);
$cols = array(
	'email' => 'EMAIL',
	'first_name' => 'FNAME',
	'last_name' => 'LNAME',
);

$existsQuery = 'SELECT COUNT(user_id) FROM user WHERE email = ?';
$selectQuery = 'SELECT email, first_name, last_name FROM user WHERE mailinglist = 1';

try {
    $synchronizer = new Galahad_MailChimp_Synchronizer_Mysqli($username, $password, $db);
    $synchronizer->sync($list, $selectQuery, $existsQuery, $cols);
    echo "\nSynced:\n";

    // Print out log of sync actions
    foreach ($synchronizer->getBatchLog() as $entry) {
        echo "\n - {$entry}";
    }

    // Any individual errors
    $errors = $synchronizer->getBatchErrorLog();
    if ($errors) {
        echo "\nErrors:\n";
        foreach ($errors as $batch => $batchErrors) {
            echo "\n{$batch}:";
            foreach ($batchErrors as $error) {
                echo "\n - {$error['message']}";
            }
        }
    }

} catch (Galahad_MailChimp_Synchronizer_Exception $e) {
    echo "\nError syncing:\n\n";
    echo $e->getMessage();
}

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.

Galahad MailChimp Synchronizer [62.27 KB]

8 Comments »

8 Responses to “Galahad MailChimp Synchronizer”

  1. troy

    Thanks for the tip. We are looking for this solution to integrate in a ecommerce site. I will let you know how it goes. -T

  2. Dave

    Works great! Kudos

  3. Elan

    I get the following errors… suggestion?

    ————————————————-

    Started. 1. Method: login 2. Method: listMembers

    Warning: Invalid argument supplied for foreach() in /data/20/1/143/60/1958875/user/2138878/htdocs/mailchimp/Galahad/MailChimp/Synchronizer.php on line 170

    Fatal error: Class ‘Galahad_MailChimp_Synchronizer_Exception’ not found in /data/20/1/143/60/1958875/user/2138878/htdocs/mailchimp/Galahad/MailChimp/Synchronizer/Mysqli.php on line 171

  4. Chris Morrell

    Good call. Add this to the top of the Mysqli.php file:

    /** @see Galahad_MailChimp_Synchronizer_Exception */
    require_once dirname(__FILE__) . '/Exception.php';

    I’ll update the library in a bit.

  5. Elan

    Now I get this response… and I am 90% sure I wrote the MySQL in ‘example-mysqli.php’ correctly:

    $existsQuery = ‘SELECT COUNT(id) FROM #__user WHERE email IS NOT NULL’;
    $query = ‘SELECT email, name, username FROM #__user’;
    $cols = array(
    ‘email’ => ‘EMAIL’,
    ‘name’ => ‘FNAME’,
    ‘username’ => ‘LNAME’,

    ——————————————–

    Started. 1. Method: login 2. Method: listMembers

    Fatal error: Uncaught exception ‘Galahad_MailChimp_Synchronizer_Exception’ with message ‘Error getting users from DB: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1′ in /data/20/1/143/60/1958875/user/2138878/htdocs/mailchimp/Galahad/MailChimp/Synchronizer/Mysqli.php:176 Stack trace: #0 /data/20/1/143/60/1958875/user/2138878/htdocs/mailchimp/Galahad/MailChimp/Synchronizer.php(210): Galahad_MailChimp_Synchronizer_Mysqli->getUsers(‘d1bea72165′, 0, 500) #1 /data/20/1/143/60/1958875/user/2138878/htdocs/mailchimp/Galahad/MailChimp/Synchronizer.php(108): Galahad_MailChimp_Synchronizer->processLocalUsers(‘d1bea72165′) #2 /data/20/1/143/60/1958875/user/2138878/htdocs/mailchimp/Galahad/MailChimp/Synchronizer/Mysqli.php(162): Galahad_MailChimp_Synchronizer->sync(‘d1bea72165′) #3 /data/20/1/143/60/1958875/user/2138878/htdocs/mailchimp/example-mysqli.php(53): Galahad_MailChimp_Synchronizer_Mysqli->sync(‘d1bea72165′, ‘SELECT email, n…’ in /data/20/1/143/60/1958875/user/2138878/htdocs/mailchimp/Galahad/MailChimp/Synchronizer/Mysqli.php on line 176

  6. Chris Morrell

    I’m pretty sure that’s an error in your PHP query, but go ahead and try this getUsers function and let me know what error you get:

    protected function getUsers($listId = null, $batchNumber)
    	{
    		$start = $batchNumber * $this->_batchSize;
    		$result = $this->_db->query($this->_query . " LIMIT {$start}, {$this->_batchSize}");
    
    		if (false === $result) {
    			throw new Galahad_MailChimp_Synchronizer_Exception('Error getting users from DB.  MySQL said: "' .
    				$this->_db->error . '".  Query: "' . $this->_query . " LIMIT {$start}, {$this->_batchSize}\".");
    		}
    
    		$return = array();
    		while ($row = $result->fetch_assoc()) {
    			$user = array();
    			foreach ($this->_mergeColumns as $localColumn => $mcColumn) {
    				if (isset($row[$localColumn])) {
    					$user[$mcColumn] = $row[$localColumn];
    				}
    			}
    
    			$return[] = $user;
    		}
    
    		return $return;
    	}
    
  7. Elan

    The previous reply I made to your code was actually my fault and should be deleted! The ‘Galahad_MailChimp_Synchronizer_Exception’ works great now and it was my SQL statement that was the problem… not your code.

    In fact I would like to not only apologize, but also extend a heartfelt and sincere gratitude for your development of this code. There is no one else offering this priceless code for the MailChimp API for free.

    I am a broke, journalist/novice web designer and was able to get this code to work on my own. When I needed help, the support you gave me was very friendly and then you offering further support was above and beyond the “call of duty”.

    If you have a donate button please let me know :-)

    Just a side note: the code for ‘getUsers’ listed above had the ‘>’ or ‘carrot’ sign replaced with html character ‘>’.

  8. Elan

    When a user already exists in my database as well as on my MailChimp list, and where updates as well as no-updates exist, I get the below WARNING. Is this okay (only indicating a user already exists) or is there something wrong? Everything seems to work okay, including ListUnsubscribe when a user has been removed from my database, but I am worried that as the list becomes bigger this may become a burden on the server. What do you think?:

    ———————————————–

    Started. 1. Method: login 2. Method: listMembers

    Warning: mysqli_stmt::bind_param() [mysqli-
    stmt.bind-param]: Number of variables doesn’t
    match number of parameters in prepared statement in /data/20/1/143/60/1958875/user/2138878/htdocs
    /mailchimp_text/Galahad/MailChimp/Synchronizer
    /Mysqli.php on line 208

    3. Method: listBatchSubscribe Done.

Leave a Reply

Additional comments powered by BackType

Comments & Trackbacks

You can leave a response, or trackback from your own site. Follow any responses to this entry through the RSS 2.0 feed.

@inxilpro

  • Just got home after running 10 miles. I don't think my legs work anymore. Jesus. 1 day ago
  • I don't get it. I have 8 x 3 GHz cores and 10 GB of RAM. How can compressing a 80-minute movie still take over an hour? 1 day ago
  • Ooh, that's sexy: http://bit.ly/btAqlc (form labels—yeah, I'm that much of a geek). 3 days ago
  • More updates...
Copyright © Chris Morrell, Powered by WordPress, Entry RSS Feed / Comment RSS Feed