// Connect to the POP3 server & open the mailbox
$oPop3 = new Net_POP3();
$oPop3->connect(MAILBOX_SERVER, MAILBOX_SERVER_PORT);
$oPop3->login(MAILBOX_ACCOUNT, MAILBOX_PASSWORD);
// Read all the messages from the mailbox and tries to create a new ticket for each one
// Note: it is expected that the sender of the email exists a valid contact as a 'Contact'
// in iTop (identified by her/his email address), otherwise the ticket creation will fail
$iNbMessages = $oPop3->numMsg();
for ($index = 1; $index <= $iNbMessages; $index++) {
    $params['include_bodies'] = true;
    $params['decode_bodies'] = true;
    $params['decode_headers'] = true;
    $params['crlf'] = "\r\n";
    $aHeaders = $oPop3->getParsedHeaders($index);
    $aSender = GetSender($aHeaders);
    $oDecoder = new Mail_mimeDecode($oPop3->getRawHeaders($index) . $params['crlf'] . $oPop3->getBody($index));
    $oStructure = $oDecoder->decode($params);
    $sSubject = $aHeaders['Subject'];
    // Search for the text/plain body part
    $iPartIndex = 0;
    $bFound = false;
    $sTextBody = '';
    //echo "<pre>\n";
    //print_r($oStructure);
    //echo "</pre>\n";
    if (!isset($oStructure->parts) || count($oStructure->parts) == 0) {
        $sTextBody = $oStructure->body;
    } else {
        // Find the first "part" of the body which is in text/plain
        while ($iPartIndex < count($oStructure->parts) && !$bFound) {
            //echo "<p>Reading part $iPartIndex</p>\n";