hi guys i'm busy trying to get a class working to download my email attachments via IMAP. It's working for the most part BUT with some emails, the attachments don't download correctly, only the file is created but the result file is 0kb. I believe the script is not correctly detecting the file type of some of these emails and as such am seeking your advice as to how I can get it detecting them correctly. I've attached info of filetypes of 2 emails which attachments end up as 0kb files and the code of my script below class readattachment { function getdecodevalue($message,$coding) { switch ($coding) { case 1: $message = imap_8bit($message); break; case 2: $message = imap_binary($message); break; case 4: $message = imap_qprint($message); break; case 0: case 3: case 5: $message = imap_base64($message); break; } return $message; } function getdata($host, $login, $password, $savedirpath) { $mbox = imap_open($host, $login, $password) or die("Unable to connect to the server using the details provided.<br />The server error message was: <em> " . imap_last_error() . "</em>"); $message = array(); $message["attachment"]["type"][0] = "text"; $message["attachment"]["type"][1] = "multipart"; $message["attachment"]["type"][2] = "message"; $message["attachment"]["type"][3] = "application"; $message["attachment"]["type"][4] = "audio"; $message["attachment"]["type"][5] = "image"; $message["attachment"]["type"][6] = "video"; $message["attachment"]["type"][7] = "other"; $total_msg = imap_num_msg($mbox); for ($i = $total_msg; $i >= $total_msg-3; $i--) { $structure = imap_fetchstructure($mbox, $i , FT_UID); $header = imap_fetch_overview($mbox, $i , FT_UID); $parts = $structure->parts; for ($n = 1; $n < count($parts); $n++) { $message["pid"][$n] = ($n); $part = $parts[$n]; if ($part->disposition == "ATTACHMENT") { $message["type"][$n] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype); $message["subtype"][$n] = strtolower($part->subtype); $ext=$part->subtype; echo "PART TYPE: $ext"; $params = $part->dparameters; $filename=$part->dparameters[0]->value; $mege=""; $data=""; $mege = imap_fetchbody($mbox,$i,$n+1); $filename="$filename"; echo $filename; $fp=fopen($savedirpath.$filename,'w'); $data=$this->getdecodevalue($mege,$part->type); fputs($fp,$data); fclose($fp); } } } imap_close($mbox); } } PHP: and here is the details & fetchstructure output (via print_r) of an email which does not download correctly (0kb file result)