Hello thanks for your reply
I really hope that u can help me solving this problem, i have been working on it several days.
note that this code was taken from google somewhere... so i do not care if you wanna provide a code that reads the inbox and copy the attached files correctly, for later i prefer to be able to read some strings in the message body also.
thanks alot
Code:
</head>
<?php
include('CMS/global.inc.php');
$user= $_GET['user'];
function WriteToFile($file,$contents) {
// IF the contents is empty lets just make it one space
if ($contents=='' || empty($contents))
$contents = " ";
// $file is the full path to the file
$fh = fopen($file, "w") or die("Could not open file!");
fwrite($fh, $contents) or die("Could not write to file");
fclose($fh);
}
$SaveFileDirectory = "/home/gsclteam/public_html/";
//check for new messages
$mailbox = imap_open("{localhost:143/notls}INBOX", "Email", "PASSWORD")
or die("Can't connect: " . imap_last_error());;
// Check messages
$check = imap_check($mailbox);
// viewing number of messages in inbox
// $chec = imap_mailboxmsginfo($mailbox);
$headers = @imap_headers($mailbox) or die("Inbox Empty!");
$numEmails = sizeof($headers); echo "You have $numEmails messages in your mailbox <br><br><br>";
//$j=1;
$header = imap_header($mailbox, 1);
$from = $header->from;
foreach ($from as $id => $object) {
$fromname = $object->personal;
echo "<b>From Name:</b> $fromname<br>";
$fromaddress = $object->mailbox . "@" . $object->host;
echo "<b>From Address:</b> $fromaddress<br>";
}
$subject = strip_tags($header->subject);
echo "<b>Subject:</b>$subject </br>";
$date = $header->date;
echo "<b>Date:</b>$date</br> ";
if ($check->Nmsgs=='0') {
echo "There are no messages that need to be downloaded";
} else {
$emailIndex = 1;
/* Lets find the email body */
$struct = imap_fetchstructure($mailbox, $emailIndex);
$parts = $struct->parts;
$i = 0;
if (!$parts) { /* Simple message, only 1 piece */
$attachment = array(); /* No attachments */
$Email_Comments = imap_body($mailbox, $emailIndex);
} else { /* Complicated message, multiple parts */
$endwhile = false;
$stack = array(); /* Stack while parsing message */
$Email_Comments = ""; /* Content of message */
$attachment = array(); /* Attachments */
while (!$endwhile) {
if (!$parts[$i]) {
if (count($stack) > 0) {
$parts = $stack[count($stack)-1]["p"];
$i = $stack[count($stack)-1]["i"] + 1;
array_pop($stack);
} else {
$endwhile = true;
}
}
if (!$endwhile) {
/* Create message part first (example '1.2.3') */
$partstring = "";
foreach ($stack as $s) {
$partstring .= ($s["i"]+1) . ".";
}
$partstring .= ($i+1);
if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */
$attachment[] = array("filename" => $parts[$i]->parameters[0]->value,
"filedata" => @imap_fetchbody($mailbox, $emailIndex, $partstring));
} elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */
$Email_Comments .= imap_fetchbody($mailbox, $emailIndex, $partstring);
echo "<b>Email Body:</b> $Email_Comments <br/><br/>";
$daouk=$Email_Comments; // my code
$emailIndex +=1;
}
}
if ($parts[$i]->parts) {
$stack[] = array("p" => $parts, "i" => $i);
$parts = $parts[$i]->parts;
$i = 0;
} else {
$i++;
}
} /* while */
} /* complicated message */
foreach ($attachment as $attach) {
WriteToFile($SaveFileDirectory . $attach['filename'], imap_base64($attach['filedata']));
echo "Attachment found: " . $attach['filename'] . "<br>";
$myf=$attach['filename'];
}
} // end else
//Step 1
//@mkdir("stk/temp", 0700);
//step 3
// Temp already exists
$zip = new ZipArchive;
if ($zip->open("$myf") === TRUE) {
$zip->extractTo('/stk/temp/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
//if (!copy("/home/gsclteam/public_html/$myf", "/home/gsclteam/public_html/stk/$myf")) { echo "failed to copy $file...\n"; }
//unlink("/home/gsclteam/public_html/$myf");
$date = date("y-m-d");
//Step 4
if ($handle = opendir('stk/temp'))
{
while (false != ($file=readdir($handle)))
{
if ( ($file != ".") && ($file != "..") )
{
$array = explode( '.' , $file );
if ($array[1] = 'ask')
{
@copy("stk/temp/$array[0].$array[1]", "stk/$user/$array[0].$array[1]");
@copy("stk/temp/$array[0]nss.rpl", "stk/$user/$array[0]nss.rpl");
@copy("stk/temp/$array[0].rpl", "stk/$user/$array[0].rpl");
I do some queries here m and they are working properly,,,,
}
else {
//echo "No data found for the User $ user";
}
}//end if we found the .ask file in the uploaded table
echo "<br><br><br>";
}
}
} // end open Directory
//imap_delete($mailbox, 1);
//imap_expunge($mailbox);
//} // end for
?>