Code Challenge
The first Code Challenge for Hak5 Season 5 — the Gmail Procrastination Badge. What started as a tongue in cheek comment on twitter about needing to declare email bankruptcy lead to proof of concept Gmail Unread Badge code.
The code
<?php
$mbox = imap_open ("{imap.googlemail.com:993/imap/ssl}INBOX",
"gmailusername", "gmailpassword", OP_READONLY)
or die("can't connect: " . imap_last_error());
$check = imap_mailboxmsginfo($mbox);
?>
<?php
if ($check) {
print $check->Unread; //. "/" . $check->Nmsgs;
} else {
print "Failed";
}
/*
Special thanks to our friend Robin Wood aka Digininja
at http://www.digininja.org for the code snippet
*/
?>
It works. Not very quickly because I have 80 unread and 8000 read in my inbox, but it works. If you execute it from terminal it’ll take a minute and return unread messages. Edit: See code below for MUCH faster inbox reading.
<?php
$mbox = imap_open ("{imap.googlemail.com:993/imap/ssl}INBOX",
"gmailuser", "gmailpassword", OP_READONLY)
or die("can't connect: " . imap_last_error());
$status = imap_status($mbox, "{imap.googlemail.com:993/imap/ssl}INBOX", SA_ALL);
if ($status) {
echo "Messages: " . $status->messages . "\n";
echo "Recent: " . $status->recent . "\n";
echo "Unseen: " . $status->unseen . "\n";
echo "UIDnext: " . $status->uidnext . "\n";
echo "UIDvalidity:" . $status->uidvalidity . "\n";
} else {
echo "imap_status failed: " . imap_last_error() . "\n";
}
imap_close($mbox);
/* Thanks to Emeryth for pointing out imap_status
http://us.php.net/function.imap_status */
?>
The Rules
This code inspired my to start the first code challenge of the season so here are the rules:
- Must be PHP (Maybe we’ll do perl/python/whitespace next time)
- Must be a badge (for your blog, forum signature) of some sort that displays unread messages in a Gmail inbox
- Does not need to store or display anything other than unread count. Caching preferred but not necessary.
- May use additional libraries and other resources as long as they’re included with the submission
- Entries will be judged on creativity (Think imagemagick, ajax, wordpress plugin, twitter integration, emacs replacement)
- Submit entries to darren@hak5.org with “Code Challenge” in subject line. I’ll accept code in body, attached archive, or link to your hosted files.
- All entries are due by April 30th
The Prize
The winning entry will be demoed on a future episode of Hak5. Winner will receive deluxe Hak5 HakPack as well as Mario Lurig’s PHP Book PHP Reference: Beginner to Intermediate PHP5.
All entries will be posted for your enjoyment at the end of the contest.



Codedninja asked “the challenge how fast does script needs to be?”
imap can take a while to grab the unread messages from a large inbox (like mine) so using a cron job with some caching is fine.
mrwallace asked “Could you clarify the code challenge some more? Does is need to store the mails for offline use, … Things like that
”
the script doesn’t need to display or store anything other than unread count in the inbox but feel free to use your imagination and get creative with it. In this case feature creep is a good thing.
Hey Darren, me and Ninja are going to work on it together.
A little tip: using imap_status() instead of imap_mailboxmsginfo() is much faster.
@Emeryth, thanks I’ve updated the code example on this page to show imap_status. I tried it locally and was able to retrieve my inbox details in 2 seconds!
yup.. imap_status is faster..
instead of useing.. $status = imap_status($mbox,server}INBOX”, SA_ALL);
you can use.. $status = imap_status($mbox,server}INBOX”, SA_UNSEEN); all we need is the unread mails.. ;P
Lol, just send in my submission. Appearantly I wasn’t the only one going for imap_status. Btw, UNSEEN messages don’t include message that are marked as UNREAD after you have read them once.
I wonder if Darren could update the page with the count of submissions so far, im really interested in how many people are participating.
Oh yeah i forgot that…!! yeah.. im kind of interested too.. lol..
No wait it does include those..!
Appearantly it depends on the IMAP server you’re using. The STATUS command seems to be poorly documented on this.
It might be faster//more reliable to just use the ATOM feed for unread mail. It can be found at https://mail.google.com/mail/feed/atom/. Though you’d have to authenticate using basic http authentication and follow the redirect.
But once you’ve got the feed loaded there’s a element that has the number of unread emails in your inbox. The benefit to this is that it’s also easy to look at unread mails in specific labels by just tacking the label’s name onto the end of the url.
Well if you are going BeMasher’s route, just use the build in curl functions to get the data and use curl_setopt to set the following flags: CURLOPT_FOLLOWLOCATION and CURLOPT_AUTOREFERER.
Then also set CURLOPT_USERPWD to “gmailaddress:gmailpassword”.
I like how the 2nd code looks almost identical to the Example #1 off the PHP.net ‘imap_status’ documentation with a few very minor changes.
http://us.php.net/manual/en/function.imap-status.php
Joe, the first example is quite similar to the one for imap_mailboxmsginfo as well.
See Example 1 at http://us.php.net/imap_mailboxmsginfo
At least the credits for the first example should belong to php.net’s manual. The seconde code is debatable as it just points the php.net url as well.
I agree that credit should go to whomever did the actual code on PHP.net’s site.
The 1st comment for the ‘imap_status’ was given 2yrs ago so I’m going to assume it’s been there for at least 2yrs prior to being posted on here.
The example that you point out Martijn has an initial comment that’s over 7yrs old so it’s safe to assume that the 1st example here is just a watered down version of PHP.net’s example.
Credits should be given to the examples at PHP.net’s site.
Now, as for the contest… I know I’m going to sound like a n00b but here goes: what is a ‘badge’ with respect to this contest? It’s already the 29th so I probably won’t get anything submitted but I’d like to know for any future contests that refer to a ‘badge’.
You should put in your effort to submit something never the less. If it’s not finished, it could be halfly decent, you could just participate for the fun of it.
That said, a badge is similar to the plain english meaning of the word, but then webified. As a picture is worth thousand words, visit this site to get a basic idea: web20badges.com. The idea is to get creative, so don’t be led by the web20badges site, but give it a twist.
A very simple idea would be for instance an ascii art representation, or a voltage meter (or odometer) that indicates the postcount within a threshold.
I know it’s not the point, but this whole thing could be accomplished in about 12 lines of php by using gmail’s rss feed…
Well i just submitted one.. i think the contest is over, but hey, it was fun anyway, and i made the badge out of the hak5 logo from the site :p