You are here

Use Python + Gmail to connect your email list to Twitter, Flickr, and everything

Online social media is growing in complexity

The role of online social media continues to grow in importance for political organizations. Facebook, MySpace, YouTube, Flickr, Twitter, and dozens of other sites need to be managed, tracked, and integrated. This takes a lot of staff time, but luckily these services continue to expand their support for external programs that can manipulate data or perform actions. APIs (application programming interfaces) allow different websites or computers to talk together and do things in the background. Even sites which don't provide official APIs can often be automated using third-party tools.

Loading emails into Gmail contacts fosters integration

In this article I'll show one simple script that uses Python and a third-party library (libgmail) to load a file of emails into gmail contacts. It's very common for web applications to support interfacing with gmail accounts to add users, so once your emails are in gmail it's simple to import them into Flickr, Twitter, etc.

The benefits of data integration

Why do this? Well for one, it helps organizations bootstrap their social media relationships. Normally your email list will be the largest medium for contacting members, so syncing that list into other services can really jump start your campaigns there. Rather than start from scratch you can build off your existing members, connect with them in different ways, and expand your online communication offerings. Even better, integrate social network synchronization into your data workflow: e.g. on a nightly (or hourly) basis sync new email subscribers into gmail, then sign into your socnets and add any new accounts you find. The "add any new accounts" step can be automated as well and will be explored in a later article.

Running the code

Python is the scripting glue for, and I use libgmail to interface with Gmail. Libgmail requires the mechanize module if you haven't already installed it, but if you have easy_install it's a single command. Here are the commands (I'm on OS X):

Example output:

It will take a while to load the emails into gmail, but once they are there it's just a few clicks to add all your members on Flickr, Twitter, or any other service that can import from gmail contacts!

Following our volunteers on Twitter

I tested this concept by loading 5,215 volunteer records into gmail. Of those, 590 could be found on Twitter (11.3% match rate) - not too shabby. And because the contacts are stored in gmail, I can always import again, say once a month, and add members who have recently joined. It would be interesting to see people try this for their own members and see what the match rates are for different services and organizations.

Final notes

  • Create a new gmail account specifically for this data sync - don't use an existing account.
  • Every 2,000 records or so gmail will stop processing records and the script will abort. Just run it again and it will resume where it left off.
  • Twitter seems to only add up to 200 followers at a time, but you can repeat as needed.
  • Because it is slow, this method works best as part as incremental workflow on new records. For loading existing email records it may be easier to use gmail's import contacts via csv feature. The problem with the csv method is that it's limited to 3,000 contacts per file, so in order to handle more than a few thousand records you'll want an automated solution.
  • In theory, once you import email records into a service, you will want to capture the respective username & id for that email record. Then you can integrate that into your main database and use it for future messaging (potentially via that service's API). This would be automated.
  • Feel free to fork and make improvements to It's easy to do on github. Or post feedback here.
  • Tip of the hat to KF for thinking of an email -> Twitter data sync in the first place. Good call!

And here's the code


Have you seen any pushback from users on using some of the integrated bells and whistles through their Facebook page, twitter account, etc? I’m starting to see a shift in what people want – or trust – in apps and plug-ins. We’re trying to encourage our partners to go to their people and engage them where they are, instead of assuming they have to drive people back to their own site/blog. Feeding our current “me-centric” obsession.

Your discussion on volunteers on Twitter was very interesting! That's a MUCH higher match rate than I'd expect!!

Hmm I don't think we've seen much user push back yet, but we don't have a lot of staff time to put to online organizing right now (hence some of my interest at automation). But yeah, definitely agree that engaging the users where they are is ideal, while also building out a site that provides solutions and encourages users to stick around.

Sadly on the match rate I don't yet have a script to extract which users were actually matched, otherwise I could look at the demographics of matched users. We have a young, online-oriented volunteer base so I can see why the match rate is pretty good.