I need a solution to get messages from IMAP server transfered over to my
server. Once the transfer complete I want to be able to read them. And one
more thing this script has needs to run from cron.

So far I figured out to get messages from the IMAP . Here is my code in the
nutshell.
Email message and attachements get transfered correctly however when trying
to read the file created with "mail -f file" it tells me that there is 0
messages in that file even though I know there is at least 10 messages.

how can I write these emails to the file and be able to read them later.
????

<CODE>

* Connecting to Mailserver $mbox is a connection link */
$mbox = imap_open("$MAILSERVER$MAILBOX",$IMAPUSER,$IMAPPA SS);


if (!$mbox) {
/* If $mbox does not exist it means that connection
to the server was not established
we need to report this issue */
$error = "phpMailArchive IMAP CONNECTION FAILED";
$err_body .= 'phpMailArchive was not able to connect to' . $MAILSERVER ."
\n\n ERROR: \n". imap_last_error() . "\r\n";
report_error($error,$err_body);
imap_close($inbox);
exit;
}

$total = imap_num_msg($mbox); //Get Total Number of messages
echo "Total Messages: ". $total . "<br>";

for($msgid=1; $msgid <= $total; $msgid++) {

/* first all vars from the previous mail got killed */
$structure = NULL;
$headers = NULL;
$xp_id = NULL;
$xp_time_unix = NULL;
$xp_date_year = NULL;
$xp_date_month = NULL;
$xp_date_day= NULL;
$xp_time_iso = NULL;
$xp_date_iso = NULL;
$xp_from_full = NULL;
$xp_from_host = NULL;
$xp_md5 = NULL;
$xp_header_raw = NULL;
$parts = NULL;
$parts_count = NULL;
$xp_full_raw = NULL;
$xp_body_raw = NULL;
$xp_fetch_body_text= NULL;
$alert_state = NULL;
$alert_host = NULL;
$alert_check = NULL;

//$headers = imap_header($mbox, $msgid);
$headers = imap_headerinfo ($mbox, $msgid);


/* initiate most of our vars */
$xp_id = $headers->message_id;
$xp_time_unix = $headers->udate;
$xp_date_year = date("Y", $xp_time_unix);;
$xp_date_month = date("F", $xp_time_unix);;
$xp_date_day= date("d", $xp_time_unix);;
$xp_from_full = $headers->from;


/* TEST TO SEE IF DIRECTORY ALREADY EXISTS ... IF NOT CREATE IT
UNFORCHANETELY WE HAVE TO TEST FOR ALL MONTH AND YEAR DIRECTORIES
SEPARATELY OR MKDIR FUNCTION WILL FAIL */
if(!is_dir("$BASEDIR/$xp_date_year/$xp_date_month")) {
if(!is_dir("$BASEDIR/$xp_date_year")) {
mkdir("$BASEDIR/$xp_date_year", 0777);
}
mkdir("$BASEDIR/$xp_date_year/$xp_date_month", 0777);
}

// GET FULL HEADER & EMAIL BODY AND WRITE IT TO THE FILE
$xp_full_raw = imap_fetchheader($mbox, $msgid);
$xp_body_raw = imap_body($mbox, $msgid);
$handle = fopen("$BASEDIR/$xp_date_year/$xp_date_month/$xp_date_day",
"w+");
fwrite($handle, $xp_full_raw . $xp_body_raw);
fclose($handle);
}

</CODE>