Skip to content

Instantly share code, notes, and snippets.

@joeynovak
Last active August 29, 2015 14:22
Show Gist options
  • Save joeynovak/7420ea00f981aedce3d1 to your computer and use it in GitHub Desktop.
Save joeynovak/7420ea00f981aedce3d1 to your computer and use it in GitHub Desktop.
PHP To Empty a Gmail folder that has WAY too many messages...
<?php
//Change lines 6 and 7 to have your email address and password, and folder name.
//Install stunnel to use this, the default config has gmail imap already setup.
$messagesToDeleteAtOnce = 100;
$socket = fsockopen("127.0.0.1", 143);
waitForResponse($socket);
sendCommand($socket, "a login youremailaddresshere password");
sendCommand($socket, "a select foldername");
while(true){
sendCommand($socket, "a MOVE 1:$messagesToDeleteAtOnce [Gmail]/Trash");
}
function sendCommand($socket, $command){
echo $command . "\r\n";
fputs($socket, $command . "\r\n");
return waitForResponse($socket);
}
function waitForResponse($socket){
$responseReceived = false;
$response = '';
do {
$response.=fread($socket,1000);
$s=socket_get_status($socket);
if(strpos($response, "a BAD") !== false || strpos($response, "a OK") !== false || strpos($response, "* BAD") !== false || strpos($response, "* OK") !== false){
while(socket_get_status($socket)['unread_bytes'] > 0) {
$response.=fread($socket,1000);
}
$responseReceived = true;
}
} while (!$responseReceived);
if(!isResponseGood($response)) throw new Exception("Ack! Bad Response:\n" . $response);
echo $response;
return $response;
}
function isResponseGood($response){
return strpos($response, "* OK") !== false || strpos($response, "a OK") !== false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment