
 George Cummins - 2007-04-12 15:31:00
When I first tested your class, binary attachments were handled properly, but attachments of type text/plain where corrupted because of incorrect decoding. Below is a modified version of your class which handles text attachments without corruption:
<?php
######################################
#Coded By Jijo Last Update Date[Jan/19/06]
#Modifications by glcummins Date[Apr/12/07]
#####################################
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";
		for ($i = 1; $i <= imap_num_msg($mbox); $i++)
		{
			$structure = imap_fetchstructure($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;
					$params = $part->dparameters;
					$filename=$part->dparameters[0]->value;
	
					$mege="";
					$data="";
					$mege = imap_fetchbody($mbox,$i,$n+1);
					$filename="$filename";
					$fp=fopen($filename,'w');
					$data=$this->getdecodevalue($mege,$part->type);
					fputs($fp,$data);
					fclose($fp);
				}
		
			}
		}
		imap_close($mbox);
	}
}
?>