In case you feel the need to kill all emails from before a certain year on your Gmail account, this would work:
<?php
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
echo "parsing ini file...\n";
$ini = parse_ini_file('g.ini');
$user = $ini['user'];
$pass = $ini['pass'];
$year = $ini['year'];
echo "account {$user} - killing msgs from before {$year}\n\n";
echo "connecting...\n";
$imap = imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert}[Gmail]/All Mail", $user, $pass) or die("can't connect: " . imap_last_error() . "\n");
echo "checking current mailbox...\n";
$mbox = imap_check($imap);
echo "fetching overview...\n";
$flaggedForDelete = 0;
for($n = 0; $n < $mbox->Nmsgs; $n++) {
echo "processing {$n} of {$mbox->Nmsgs}...\r";
$hdr = imap_fetchheader($imap, $n);
preg_match('/^Date: (.*?)$/m', $hdr, $matches);
$date = date_parse(strtotime($matches[1]));
if($date['year'] < $year) {
imap_delete($imap, $msg->msgno);
$flaggedForDelete++;
}
}
echo "expunging mailbox ({$flaggedForDelete} messages flagged)... ";
echo ((imap_expunge($imap)) ? "ok" : "failed!")."\n";
imap_close($imap);
?>