#!/usr/bin/php

<?php
/*
Gmail "spam mark-as-read"

by Tuinslak
www.tuinslak.be

v0.02 :: 31/10/2008 . ignores google certif, use at own risk
v0.01 :: 26/08/2008 . init php codez
*/

// Config starts here

// Gmail user & pass
$user = "username";
$pass = "password";

// Debugging - Outputs a list of mailboxes and status on the imap server (1/0)
$listmailbox = 0;

// Advanced config :o

// "Path" of the (spam) mailbox and/or its name.
// default should be ok with Gmail.
$path = "[Google Mail]/Spam";

// End of config

$open = imap_open ("{imap.gmail.com:993/imap/ssl/novalidate-cert}$path", $user, $pass, "", 1) or die(imap_last_error() . "<br /><br />Connection failed.");

// debug
if($listmailbox) {
	echo "<b>Mailboxes</b>:<br />";
	$folders = imap_listmailbox($open,  "{  Gmail  }", "*");

	if ($folders == false) {
    		echo "Call failed<br />\n";
	} else {
    		foreach ($folders as $val) {
        		echo $val . "<br />\n";
    		}
	}

	echo "<br /><b>Status</b>:<br />";
	$status = imap_status($open, "{imap.gmail.com}$path", SA_ALL);
	if ($status) {
		echo "Messages:   " . $status->messages    . "<br />\n";
		echo "Recent:     " . $status->recent      . "<br />\n";
		echo "Unseen:     " . $status->unseen      . "<br />\n";
  		echo "UIDnext:    " . $status->uidnext     . "<br />\n";
  		echo "UIDvalidity:" . $status->uidvalidity . "<br />\n";
	} else {
  		echo "imap_status failed: " . imap_last_error() . "\n";
	}
}

// Mark as read
$search = imap_search($open, 'UNSEEN');
// print out the array containing $search info
//print_r($search);

for ($i = 0; $i < sizeof($search); $i++) {
	$read = imap_setflag_full($open, $search[$i], '\\Seen');
}

// and close it down !
imap_close($open);

?>

