Problem Parsing message/rfc822 messages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
-=Haseeb=-
Forum Newbie
Posts: 2
Joined: Mon Mar 24, 2003 12:31 pm

Problem Parsing message/rfc822 messages

Post by -=Haseeb=- »

hi,
i have paresed entire messaage into an array. everything was ok untill i
encountered a message/rfc822 subtype.it was a farwaded message.now my
array contains everything from images to headers in a subscript 'TEXT'.
the actual array returned is

Array
(
[Date] => Tue, 18 Mar 2003 13:28:07 +0000 (GMT)
[Subject] => Fwd: somesubject
[FR] =>
[FU] =>
[FA] =>
[FD] =>
[FX] =>
[FF] =>
[To] => someone <someone@somewhere.com>,................
[From] => "SomeOne" <some@someone.net>
[CC] =>
[BCC] =>
[Type] => multipart/mixed
[Encoding] => 7bit
[IsDesc] => 0
[IsId] => 0
[Bytes] =>
[IsDisp] => 0
[IsDPara] => 0
[IsPara] => 1
[Para] => Array
(
[0] => Array
(
[Atr] => boundary
[Val] => 0-1759809281-1047994087=:91467
)

)

[IsParts] => 1
[Parts] => Array
(
[0] => Array
(
[PartNo] => 1
[Type] => multipart/alternative
[Encoding] => 7bit
[IsDesc] => 0
[IsId] => 0
[Bytes] =>
[IsDisp] => 0
[IsDPara] => 0
[IsPara] => 1
[Para] => Array
(
[0] => Array
(
[Atr] => boundary
[Val] => 0-1446890343-1047994087=:91467
)

)

[IsParts] => 1
[Parts] => Array
(
[0] => Array
(
[PartNo] => 1.1
[Type] => text/plain
[Encoding] => 8bit
[IsDesc] => 0
[IsId] => 0
[Bytes] => 136
[IsDisp] => 0
[IsDPara] => 0
[IsPara] => 1
[Para] => Array
(
[0] => Array
(
[Atr] => charset
[Val] => iso-8859-1
)

)

[IsParts] => 0
)

[1] => Array
(
[PartNo] => 1.2
[Type] => text/html
[Encoding] => 8bit
[IsDesc] => 0
[IsId] => 0
[Bytes] => 471
[IsDisp] => 0
[IsDPara] => 0
[IsPara] => 1
[Para] => Array
(
[0] => Array
(
[Atr] => charset
[Val] => iso-8859-1
)

)

[IsParts] => 0
)

)

)

[1] => Array
(
[PartNo] => 2
[Type] => message/rfc822
[Encoding] => 8bit
[IsDesc] => 0
[IsId] => 0
[Bytes] => 177048
[IsDisp] => 0
[IsDPara] => 0
[IsPara] => 0
[IsParts] => 0
)

)

)

the subscript 'IsParts' shows weather this message has further parts or
not. as there is no part of last subs soo i try to get the body of the
part speacified.but it returns all the message plus images.what should i
do know.need to deliver this asap.help me.
Haseeb :?:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe I missed the point but a mime-part rfc822 is a complete mail. Extract it and let your script parse this part 'again'.
-=Haseeb=-
Forum Newbie
Posts: 2
Joined: Mon Mar 24, 2003 12:31 pm

Re:maybe I missed

Post by -=Haseeb=- »

here is the code

Code: Select all

function GiveDetail($pIMAP,$nMsgNo)
		&#123;
			
			global $arrType,$arrEncoding;

			$i=$nMsgNo;

			$arrHeaders=imap_headers($pIMAP);

			$nMailBoxSize=sizeof($arrHeaders);
			
			unset($arrHeaders);

			$arrMyArr=array();

			$oHI=imap_headerinfo($pIMAP,$i);
			
			
			(@$oHI->Date)? $arrMyArr&#1111;'Date']=$oHI->Date:$arrMyArr&#1111;'Date']='';

			(@$oHI->Subject)? $arrMyArr&#1111;'Subject']=$oHI->Subject:$arrMyArr&#1111;'Subject']='No Subject';

			$arrMyArr&#1111;'FR']=$oHI->Recent;

			$arrMyArr&#1111;'FU']=$oHI->Unseen;

			$arrMyArr&#1111;'FA']=$oHI->Answered;

			$arrMyArr&#1111;'FD']=$oHI->Deleted;

			$arrMyArr&#1111;'FX']=$oHI->Draft;

			$arrMyArr&#1111;'FF']=$oHI->Flagged;

			($oHI->toaddress)?	$arrMyArr&#1111;'To']=$oHI->toaddress:$arrMyArr&#1111;'To']='Not Speacified';
			
			$arrMyArr&#1111;'To']=str_replace("<","&lt",$arrMyArr&#1111;'To']);
			
			$arrMyArr&#1111;'To']=str_replace(">","&gt",$arrMyArr&#1111;'To']);

			($oHI->fromaddress)?	$arrMyArr&#1111;'From']=$oHI->fromaddress:$arrMyArr&#1111;'From']='Not Speacified';

			$arrMyArr&#1111;'From']=str_replace("<","&lt",$arrMyArr&#1111;'From']);
			
			$arrMyArr&#1111;'From']=str_replace(">","&gt",$arrMyArr&#1111;'From']);

			if (@$oHI->ccaddress)
				&#123;
					$arrMyArr&#1111;'CC']=$oHI->ccaddress;
					
					$arrMyArr&#1111;'CC']=str_replace("<","&lt",$arrMyArr&#1111;'CC']);
			
					$arrMyArr&#1111;'CC']=str_replace(">","&gt",$arrMyArr&#1111;'CC']);
				&#125;
			else
				$arrMyArr&#1111;'CC']='';

			if (@$oHI->bccaddress)
				&#123;
					$arrMyArr&#1111;'BCC']=$oHI->bccaddress;
					
					$arrMyArr&#1111;'BCC']=str_replace("<","&lt",$arrMyArr&#1111;'BCC']);
			
					$arrMyArr&#1111;'BCC']=str_replace(">","$gt",$arrMyArr&#1111;'BCC']);
				&#125;
			else
				$arrMyArr&#1111;'BCC']='';

			$oS=imap_fetchstructure($pIMAP,$i);

			$arrMyArr&#1111;'Type']=strtolower($arrType&#1111;(int)$oS->type]."/".$oS->subtype);
			
			$arrMyArr&#1111;'Encoding']=$arrEncoding&#1111;(int)$oS->encoding];

			$arrMyArr&#1111;'IsDesc']=$oS->ifdescription;

			if ($arrMyArr&#1111;'IsDesc'])
				&#123;
					$arrMyArr&#1111;'Desc']=$oS->description;
				&#125;
					
			$arrMyArr&#1111;'IsId']=$oS->ifid;

			if ($arrMyArr&#1111;'IsId'])
				&#123;
					$arrMyArr&#1111;'Id']=$oS->id;
				&#125;
					
			(@$oS->bytes)? $arrMyArr&#1111;'Bytes']=$oS->bytes:$arrMyArr&#1111;'Bytes']='';

			$arrMyArr&#1111;'IsDisp']=(int)$oS->ifdisposition;
			
			if ($oS->ifdisposition)
				&#123;
					
					// this will give attachment when there is any attachment
					($oS->disposition)? $arrMyArr&#1111;'Disposition']=$oS->disposition:$arrMyArr&#1111;'Disposition']='';

				&#125;

			$arrMyArr&#1111;'IsDPara']=(int)$oS->ifdparameters;
			
			if ($oS->ifdparameters)
				&#123;

					for ($nPCount=0;$nPCount<sizeof($oS->dparameters);$nPCount++)
						&#123;
							$arrMyArr&#1111;'DPara']&#1111;$nPCount]&#1111;'Atr']=$oS->dparameters&#1111;$nPCount]->attribute;
							$arrMyArr&#1111;'DPara']&#1111;$nPCount]&#1111;'Val']=$oS->dparameters&#1111;$nPCount]->value;
						&#125;

				&#125;
			
			// this will give an array of object with attribute: filename and value: 'file name of the attachment'
			
			$arrMyArr&#1111;'IsPara']=(int)$oS->ifparameters;

			if ($oS->ifparameters)
				&#123;
					for ($nPCount=0;$nPCount<sizeof($oS->parameters);$nPCount++)
						&#123;
							$arrMyArr&#1111;'Para']&#1111;$nPCount]&#1111;'Atr']=$oS->parameters&#1111;$nPCount]->attribute;
							$arrMyArr&#1111;'Para']&#1111;$nPCount]&#1111;'Val']=$oS->parameters&#1111;$nPCount]->value;
						&#125;

				&#125;

			// this will give an array of objects which will have all the same parameters as of top level objects
			
			$arrMyArr&#1111;'IsParts']=((@sizeof($oS->parts) > 1)? '1':'0');
			
			if (@sizeof($oS->parts) > 1)
				&#123;

					CheckParts($oS->parts,&$arrMyArr&#1111;'Parts'],'');

				&#125;

			return $arrMyArr;

		&#125;
	function CheckParts($arroParts,$arrParts,$nPartSep)
		&#123;
			global $arrType,$arrEncoding;
			
			$nSizeParts=sizeof($arroParts);
			
			for ($i=0;$i<$nSizeParts;$i++)
				&#123;
					if ($nPartSep!='')
						$arrParts&#1111;$i]&#1111;'PartNo']=$nPartSep.'.'.($i+1);
					else
						$arrParts&#1111;$i]&#1111;'PartNo']=$i+1;
					
					$arrParts&#1111;$i]&#1111;'Type']=strtolower($arrType&#1111;(int)$arroParts&#1111;$i]->type]."/".$arroParts&#1111;$i]->subtype);
					
					$arrParts&#1111;$i]&#1111;'Encoding']=$arrEncoding&#1111;(int)$arroParts&#1111;$i]->encoding];

					$arrParts&#1111;$i]&#1111;'IsDesc']=$arroParts&#1111;$i]->ifdescription;

					if ($arrParts&#1111;$i]&#1111;'IsDesc'])
						&#123;
							$arrParts&#1111;$i]&#1111;'Desc']=$arroParts&#1111;$i]->description;
						&#125;
					
					$arrParts&#1111;$i]&#1111;'IsId']=$arroParts&#1111;$i]->ifid;

					if ($arrParts&#1111;$i]&#1111;'IsId'])
						&#123;
							$arrParts&#1111;$i]&#1111;'Id']=$arroParts&#1111;$i]->id;
							$arrParts&#1111;$i]&#1111;'Id']=str_replace("<","",$arrParts&#1111;$i]&#1111;'Id']);
							$arrParts&#1111;$i]&#1111;'Id']=str_replace(">","",$arrParts&#1111;$i]&#1111;'Id']);
						&#125;
					
					$arrParts&#1111;$i]&#1111;'Bytes']=@$arroParts&#1111;$i]->bytes;
					
					$arrParts&#1111;$i]&#1111;'IsDisp']=(int)$arroParts&#1111;$i]->ifdisposition;

					if ($arroParts&#1111;$i]->ifdisposition)
						&#123;
							$arrParts&#1111;$i]&#1111;'Disp']=$arroParts&#1111;$i]->disposition;
						&#125;
					
					
					$arrParts&#1111;$i]&#1111;'IsDPara']=(int)$arroParts&#1111;$i]->ifdparameters;

					if ($arroParts&#1111;$i]->ifdparameters)
						&#123;
							for ($nPCount=0;$nPCount<sizeof($arroParts&#1111;$i]->dparameters);$nPCount++)
								&#123;
									$arrParts&#1111;$i]&#1111;'DPara']&#1111;$nPCount]&#1111;'Atr']=$arroParts&#1111;$i]->dparameters&#1111;$nPCount]->attribute;
									$arrParts&#1111;$i]&#1111;'DPara']&#1111;$nPCount]&#1111;'Val']=$arroParts&#1111;$i]->dparameters&#1111;$nPCount]->value;
								&#125;
						&#125;
					
					$arrParts&#1111;$i]&#1111;'IsPara']=(int)$arroParts&#1111;$i]->ifparameters;

					if ($arroParts&#1111;$i]->ifparameters)
						&#123;
							for ($nPCount=0;$nPCount<sizeof($arroParts&#1111;$i]->parameters);$nPCount++)
								&#123;
									$arrParts&#1111;$i]&#1111;'Para']&#1111;$nPCount]&#1111;'Atr']=$arroParts&#1111;$i]->parameters&#1111;$nPCount]->attribute;
									$arrParts&#1111;$i]&#1111;'Para']&#1111;$nPCount]&#1111;'Val']=$arroParts&#1111;$i]->parameters&#1111;$nPCount]->value;
								&#125;
						&#125;
					
					
					$arrParts&#1111;$i]&#1111;'IsParts']=((@sizeof($arroParts&#1111;$i]->parts) > 1)? '1':'0');
					
					if (@sizeof($arroParts&#1111;$i]->parts) > 1)
						&#123;

							CheckParts($arroParts&#1111;$i]->parts,&$arrParts&#1111;$i]&#1111;'Parts'],$arrParts&#1111;$i]&#1111;'PartNo']);

						&#125;
					
				&#125;
		&#125;
$pIMAP=OpenImap("login","password");
	
	
$arrMyArr=GiveDetail($pIMAP,$nMsgNo);

echo "<pre>";
	print_r($arrMyArr);
echo "</pre>";
now please tell me what should i do or what i am doing wrong
Haseeb
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

take a look at the user contributed note of theUNDERSCOREVoodooChileADGMXdotNET at http://www.php.net/manual/en/function.i ... ucture.php and run the script-snippet on a forwarded mail to see the structure of such a message.
Post Reply