Gmail automatic “mark (spam) as read”

I hate having unread mail. I hate having spam. And most of all, I hate spam that asks for attention.

When receiving spam in Gmail, you get the annoying bold Spam (12353434) with ‘12353434’ being the number of unread spam messages.

With the introduction of Gmail Labs I’d hope to see an “automaticly mark spam message as seen” feature, which, well, I haven’t found yet.

Being tired of all these increasing spam numbers, and my daily efforts to “select all unread + mark as read”, I ended up writing a small PHP file which connects to the Gmail (using IMAP, so I asume you’ll need IMAP enabled in your settings..) and marks all spam messages as read. Running this script locally with a curl or cron every X time will solve most of your problems, I hope.

Download the file here (phps) or here (txt).

Or just copy paste it!

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

by Tuinslak
www.tuinslak.be

v0.01 :: 26/08/2008 . init php codez
*/

// Config starts here

// Gmail user & pass
$user = "YOU";
$pass = "hiddensecretz";

// 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}$path", $user, $pass, "", 1) or die(imap_last_error() . "

Connection failed.");

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

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

        echo "Status:";
        $status = imap_status($open, "{imap.gmail.com}$path", 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";
        }
}

// 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);

// EOF
?>

If you plan to execute it from shell, don’t forget to add a shebang “#!/usr/bin/php” on line 1 and chmod +x gmail_spamreader.php.

Edit: Please add a comment if you’re using it, just for statistics. 🙂


Posted by

in

, , , ,

Comments

2 responses to “Gmail automatic “mark (spam) as read””

  1. […] I’ve “updated” my previous php-app. […]

  2. test avatar
    test

    for statistics. Thanks

Leave a Reply…