note 18206 added to function.imap-fetchbody

From: Date: Mon, 14 Jan 2002 18:58:49 +0000
Subject: note 18206 added to function.imap-fetchbody
Groups: php.notes 
Request: Send a blank email to php-notes+get-24804@lists.php.net to get a copy of this message
If you are trying to fetch the body part and don't know what to fetch, here is a function that will return all the body parts as a single string with each part separated by '|' character. Followed by a fuction which will retrieve the structure of that part. You can then parse the return string by explode() and call imap_fetchbody() on each of the parts. Chunks of this code was stolen from the following site: http://www.bitsense.net/PHPNotes/IMAP/imap_fetchstructure.asp <pre> #Function Gets all the subparts , but does not get part: '' function GetParts($this_part, $part_no) { //If it is type message(2)=msg envelope, //Not sure about this assumption : the if statement //double check with RFC822 on this one. if($this_part->type==2){ //and there is only 1 subpart of the envelope //and that subpart is multipart //otherwise will generate excessive '.1' and //send wrong part_no to imap_fetchbody if($this_part->parts[0]->type==1){ $this_part=$this_part->parts[0]; } } if (count($this_part->parts)>0) { for ($i = 0; $i < count($this_part->parts); $i++) { if ($part_no != "") { $newpartno = $part_no . '.' . ($i +1); } else $newpartno = $i+1; $partstringary[$i] = GetParts($this_part->parts[$i], $newpartno); }//for $partstring = implode('|', $partstringary); }//if $partstring = concat_ws('|', $part_no , $partstring); //echo $partstring ; return $partstring; } ######################################### #Companion function go to GetParts(); #Given the partno - returns the structure ######################################### function GetSubPartStructure($mbox, $msgno, $partno){ $msg_struct = imap_fetchstructure($mbox, $msgno); $partsary = explode ('.', $partno); $subpartstruct = $msg_struct; for ($i=0; $i< count($partsary); $i++){ //Not sure about this, if assumption // you should probably double check with RFC822 on this one. if($subpartstruct->type==2){ if($subpartstruct->parts[0]->type==1){ $subpartstruct=$subpartstruct->parts[0]; } } $subpartstruct = $subpartstruct->parts[$partsary[$i]-1]; } return $subpartstruct; } function decodemsg($msgstring, $encoding){ //Write your own function to decode, //or lots of examples within PHP already. return $decodemsg; } #Usage: $msg_struct = imap_fetchstructure($mbox, $msgno); $msgparts= GetParts($msg_struct, $part_no); $partsary = explode($msgparts, '|'); for($i=0; $i<count($parsary);$i++){ $substruct = GetSubPartStructure($mbox, $msgno, $partsary[$i]); if($substruct->type==''){ //then this is type TEXT $msg = imap_fetchbody($mbox, $msgno, $msgpartsary[$i]); //Do the decoding $msg = decodemsg($msg, $struct->encoding); echo $msg; //or do whatever you want with it. } else{ //Not TEXT //Do whatever you want //i.e. write the attachment...etc } } </pre> -- http://www.php.net/manual/en/function.imap-fetchbody.php http://master.php.net/manage/user-notes.php?action=edit+18206 http://master.php.net/manage/user-notes.php?action=delete+18206 http://master.php.net/manage/user-notes.php?action=reject+18206

« previous php.notes (#24804) next »