<?php
require_once('lib/php-imap-master/src/ImapMailbox.php');
include("debuglib.php");
?>

<?php

// connect to gmail
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'tcga00@gmail.com';
$password = 'Peru.995';
$attachmentDir = dirname(__FILE__) . '/attachments';

$mailbox = new ImapMailbox('{imap.gmail.com:993/imap/ssl}INBOX', $username, $password, $attachmentDir, 'utf-8');

print("INBOX INFO: ===========================================<br />");
$mailboxInfo = $mailbox->getMailboxInfo();
print_a($mailboxInfo);


// Get some mail
// $mailsIds = $mailbox->searchAndSortMails('SUBJECT "Gmail"');
$mailsIds = $mailbox->searchAndSortMails('ALL');
if(!$mailsIds)
{
    die('Mailbox is empty');
}

print("START EMAIL UIDs: ===========================================<br />");
print_a($mailsIds);
// $mailbox->markMailsAsRead($mailsIds);
print("END EMAIL UIDs: ===========================================<br />");

$mailId = reset($mailsIds);

if (!empty($mailsIds))
{
    // for every email...
    foreach($mailsIds as $mailId)
    {
        echo '<br />EMAIL_NUMBER <hr />';
        print_a($mailId);

        //$mailbox->markMailAsUnread($mailId);

        // get header information
        $mailsInfo = $mailbox->getMailsInfo(array($mailId));

        //$mail = $mailbox->getMail($mailId);
        //$attachments = $mail->getAttachments();

        echo '<br />Mail Info <hr />';
        print_a($mailsInfo);
        echo '<hr />';

        // echo '<br />MESSAGE <hr />';
        // print_a($mail);
        // echo '<hr />';

        // echo '<br />ATTACHMENTS<hr />';
        // print_a($attachments);
        // echo '<hr />';
   }
}

show_vars();
?>
