die("handmadeimap_earliest_index_since_time() failed: " . handmadeimap_get_error() . "\n");
                    }
                    print "{$searchresult}:{$totalcount}\n";
                }
            }
        }
    }
    handmadeimap_close_connection($connection);
} else {
    if ($protocol == 'pop3s') {
        $connection = create_pop3_connection($mailserver, $port, $user, $password);
        if ($action == 'list') {
            die('Action "list" not supported for POP3');
        }
        if ($action == 'totalcount') {
            $totalcount = handmadepop3_get_message_count($connection);
            echo $totalcount;
        } else {
            if ($action == 'fetchheaders') {
                $fetchresult = handmadepop3_fetch_message_headers($connection, $earliesttime);
                if (!handmadepop3_was_ok()) {
                    die("FETCH failed: " . handmadepop3_get_error() . "\n");
                }
                $messagexml = message_headers_to_xml($fetchresult, 'received');
                print '<?xml version="1.0" encoding="ISO-8859-1"?><messagelist>' . "\n";
                //'
                print $messagexml;
                print "</messagelist>\n";
            }
        }
        handmadepop3_close_connection($connection);
function handmadepop3_fetch_message_headers($pop3, $earliesttime = 0)
{
    $totalcount = handmadepop3_get_message_count($pop3);
    $result = array();
    for ($index = 1; $index <= $totalcount; $index += 1) {
        $retrieveerror = $pop3->RetrieveMessage($index, $headerlines, $dummy, 0);
        if (!empty($retrieveerror)) {
            handmadepop3_set_error($retrieveerror);
            return null;
        }
        $headermap = array();
        foreach ($headerlines as $headerline) {
            $headerparts = explode(':', $headerline, 2);
            if (count($headerparts) != 2) {
                continue;
            }
            $headername = strtolower($headerparts[0]);
            $headercontent = $headerparts[1];
            $headermap[$headername] = $headercontent;
        }
        if (isset($headermap['date'])) {
            $unixtime = strtotime($headermap['date']);
            if ($unixtime < $earliesttime) {
                break;
            }
        }
        $result[$index] = $headermap;
    }
    return $result;
}