Displaying a Gmail ATOM Feed In Google Reader

So, not too long back, I thought I’d solve the “bacn” issue by having my notification emails automatically labelled “bacn” in Gmail, and then automatically archived. Trouble is, I’d then need to remember to check that label from time to time, so that I wouldn’t miss something I might actually want to know (ie, a Basecamp project has a new note, etc).

The obvious solution seemed to be an RSS feed; GMail does automatically supply feeds of all your mail, or of just one label; perfect!

It turns out, not so perfect. Because your email is, for obvious reasons, secure, getting at the feed requires authentication. Well, I’ve been using Google Reader (for so long that I really don’t want to bother switching), and Google Reader does not support authentication for feeds.

That’s right: Google’s RSS reader can’t read Gmail’s own feeds.

So, I finally got sick of reading my bacn label from time to time, and set up Magpie RSS in a directory on another server.

Now, Magpie reads RSS or ATOM feeds (Gmail is ATOM, I believe), but it returns an Object. So to have Google Reader read the feed, I basically just had to parse it back into a feed again: the script I used is below:

<?php

require_once('rss_fetch.inc');

$url = 'https://username:password@mail.google.com/mail/feed/atom/bacn/';
$rss = fetch_rss( $url );

$count = 0; // init counter for header elements

foreach ($rss->channel as $element) {
        $el[$count] = $element;
        $count++; //increment counter
}

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<feed xmlns=\"http://www.w3.org/2005/Atom\">\n";

echo "<title>$el[0]</title>\n";
echo "<tagline>$el[1]</tagline>\n";
echo "<fullcount>$el[2]</fullcount>\n";
echo "<link href='$el[3]' />\n";
echo "<modified>$el[4]</modified>\n";
echo "<description>$el[5]</description>\n";

foreach ($rss->items as $item) {
        echo "<entry>\n";
        $title = $item['title'];
        $link = $item['link'];
	$summary = $item['summary'];
        $modified = $item['modified'];
        $issued = $item['issued'];
        $id = $item['id'];
        $author_name = $item['author_name'];
        $author_email = $item['author_email'];
        $description = $item['description'];
        echo "<title>$title</title>\n";
        echo "<summary>$summary</summary>\n";
        echo "<link href=\"$link\" />\n";
        echo "<modified>$modified</modified>\n";
        echo "<issued>$issued</issued>\n";
        echo "<id>$id</id>\n";
        echo "<author_name>$author_name</author_name>\n";
        echo "<author_email>$author_email</author_email>\n";
        echo "<description>$description</description>\n";
        echo "</entry>\n";
}
echo "</feed>\n";
?>

Now, unless Google changes the format of their feeds, you ought to be able to use the above script as is; this assumes that you replace “username:password” with your own. You should change “bacn” to whatever label you have used.

Now, whether it’s really that much easier to check the feed in Google reader or the check the label in Gmail is debatable; but I routinely look through my feeds, so to me, it seems easier. If you like it, use it.