login($username, $password); if($login) { print "login OK\n"; $storage = new Zend_Mail_Storage_Imap($imap); download_users_emails($storage); } else { print "login FAILED at $server for $username\n"; } } catch (Zend_Mail_Protocol_Exception $e) { print "ERR connecting\n"; } function download_users_emails($storage) { global $imap; $count = 0; $folders = new RecursiveIteratorIterator($storage->getFolders(), RecursiveIteratorIterator::SELF_FIRST); foreach ($folders as $folder) { $__folder = preg_replace("/\W/", "_", $folder); if($folder->isSelectable()) { $storage->selectFolder($folder); $num = $storage->countMessages(); $__folder = preg_replace("/\W/", "_", $folder); print "processing $folder ($num messages)\n"; $count += saveMessages($storage, $__folder, $num); } } print "downloaded $count messages\n"; return $count; } function saveMessages($storage, $folder = '', $num = 0) { global $username; $count = 0; $messages = $storage->piler_batch_fetch(1, $num); foreach($messages as $k => $v) { $uuid = $storage->getUniqueId($k); $tmpname = "piler-" . $username . "-" . $folder . "-" . $k . "-" . $uuid . ".eml"; $f = fopen($tmpname, "w+"); if($f){ fwrite($f, $v['RFC822.HEADER'] . $v['RFC822.TEXT']); fclose($f); $count++; } } return $count; } function usage() { print "\nUsage: " . __FILE__ . "\n\n"; print "\t--webui \n"; print "\t--server \n"; print "\t--port [default: 143]\n"; print "\t--username \n"; print "\t--password \n"; print "\t--help\n\n"; exit; }