icon Get the most out of Surmunity, read our tips here! Need an interesting blog to read? You've got to read the Surpass Blog! | Welcome! Please register to access all of our features.

» Surpass Web Hosting Forums » Discussions » Email » Attachment Size Problem

Email General questions, webmail, mailing lists.

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
Old June 19th, 2008, 1:16 AM   #1 (permalink)
Registered User
Fresh Surpasser
 
Joined in Jun 2008
3 posts
Gave thanks: 0
Thanked 0 times
Attachment Size Problem

Hello

I have a code that reads email addreses (using IMAP Functions) and copy the attached file to a directory. The problem is that the Original file is for example 25KB, when it is copied through this code, it becomes 1KB, and of course damaged. ;((
why?
Please help
ballouta is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old June 20th, 2008, 1:39 AM   #2 (permalink)
the one who was
Super #1
 
patrickb's Avatar
 
Joined in Jul 2003
Lives in Memphis
1,967 posts
Gave thanks: 0
Thanked 3 times
You should post the code in question, as the cause of this could be any number of problems. My first thought is that something in the code is not correct and the attachment itself is not getting copied properly. Assuming everything is copied comes out to 1kB each time it could be the code itself, or even possibly a setting on the server causing the issue.
__________________
Patrick

Warnings: The program(s) might crash unexpectedly or behave otherwise strangely. (But of course, so do many commercial programs on Windows.) --www.gimp.org
patrickb is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
This user thanks patrickb for this great post!
Kayla (June 20th, 2008)
Old June 20th, 2008, 2:13 AM   #3 (permalink)
Registered User
Fresh Surpasser
 
Joined in Jun 2008
3 posts
Gave thanks: 0
Thanked 0 times
this is my code

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
?>
ballouta is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old June 20th, 2008, 7:20 PM   #4 (permalink)
the one who was
Super #1
 
patrickb's Avatar
 
Joined in Jul 2003
Lives in Memphis
1,967 posts
Gave thanks: 0
Thanked 3 times
Okis, I think there may be an issue here using the ZipArchive functions. Basically what I see in this code is first, it is always assumed that the file attachment is going to be a zip. Not sure of the content of the attachments you are dealing with, but that's a potential cause of this problem, and potential for a real headache in the future for sure.

The other thing I see is that there is no return value checking on the "$zip->extractTo('/stk/temp/');" and "$zip->close();" lines. Some return value checking here in the form of an if statement would be useful in tracking whether those two functions are successful. Replace those lines with something along this line:

Code:
if ($zip->extractTo('/stk/temp/')) {
	echo "Extraction ok";
} else {
	echo "Extraction failed";
}

if ($zip->close()) {
	echo "Closing ok";
} else {
	echo "Closing failed";
}
That alone should at least verify whether those two functions are successful which is critical in tracking the error.

Now the other twist, the extractTo function of ZipArchive will maintain the file structure of the zip archive, so if the zip had a file along the lines of "FOLDER/includedfile" then instead of writing 'includedfile' to your directory, it would write '/stk/tmp/FOLDER/includedfile'. Then the copy function would copy '/stk/tmp/FOLDER' to a file in the users directory. I am not sure of the behavior of the copy function when it is passed an actual directory as the filename, other than it may just copy the directory itself and it does not do any copying of the files in that directory. This would also explain the 1k file sizes as directories are always 1k in size on most *nix type OS's.

These are just some ideas for right now, I don't have my test bed server setup and no website as of right now to test on so I can't do any direct testing, but it should be something to get you started. Post back any findings or questions.
__________________
Patrick

Warnings: The program(s) might crash unexpectedly or behave otherwise strangely. (But of course, so do many commercial programs on Windows.) --www.gimp.org
patrickb is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old June 21st, 2008, 1:58 AM   #5 (permalink)
Registered User
Fresh Surpasser
 
Joined in Jun 2008
3 posts
Gave thanks: 0
Thanked 0 times
Notes

Dear Patric

Your notes are important, please note the following:
This is email is going to be used for a specific service, which means the coing messags will always hold ONE attachment and of type Zip.
this zip Archive will always have three files inside without any directory.

to be honest i am afraid and confused, is the problem from my code or surpass?

I have a reseller account, and I insisted on them to install the phpzip module for me, and they replied in their ticket that it was installed, yesterday night i was surpried that they are saying it is not!!
but how did it work, at least extrated 0KB files, and before they install it (as they said) the code used to tell me smthg like it is not installed or enabled.

The if verifications you added to me are very usefull.
Please keep in touch untill I recieve a reply from surpass.

thanks alot
ballouta is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old June 26th, 2008, 2:36 AM   #6 (permalink)
the one who was
Super #1
 
patrickb's Avatar
 
Joined in Jul 2003
Lives in Memphis
1,967 posts
Gave thanks: 0
Thanked 3 times
Hi, sorry it has been a while, been out of town and raising some animals so a bit busy with that and work.

What were the results of the if verifications? I would assume that if it is enabled it will work, otherwise it would tell you it isn't installed or enabled as before. If the functions are returning values, then it is installed/enabled.

I noticed in your code you commented out the deletion of the email messages. I assume you are checking them through a regular email client and are able to download the attachments to your computer and extract them normally? With that in mind, maybe it is a compatibility issue with the zip archives themselves. I don't know how updated the phpzip modules are, but formats do change and it is possible it can't correctly unzip the archives. That is a longshot there, but just another thought. The .zip format is very stable in format so I don't think that will be the issue honestly.

If you know how to code it, I would recommend starting simple first. Instead of extracting the zip file, go straight to copying the zip attachment to the directory where you are trying to extract it to You may also consider extracting it in that directory AFTER you have copied that zip to the target directory for the files. This will help to identify if there is a problem in the actual creation/copying process of the files. If the attachments can be copied to their target directory and are non-corrupt, then the issue is indeed somewhere within the phpzip module. Report back your results and I will assist further.
__________________
Patrick

Warnings: The program(s) might crash unexpectedly or behave otherwise strangely. (But of course, so do many commercial programs on Windows.) --www.gimp.org
patrickb is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On