I am trying to get a certain part of an email...

The mail is laid out as follows:

Mail type 1 (multipart)
Subtype Related
Part 0 - Subtype Alternative
0.1 - Subtype Plain
0.2 - Subtype HTML
Part 1 - Subtype GIF

I can get the whole message body with imap_body but I only want to get the HTML portion

I have tried
imap_fetchbody($this->stream,$this->msg_uid, 0.2,FT_UID)
But that brings back nothing....

Any help / ideas ?

    I'm kinda new in this myself, in fact i have many doubts and maybe we can help eachother...

    I think that when you use multipart, part 0 is somekind of header, the parts themself start at 1, so, you should trie with part 1.2 instead of 0.2.

    That worked for me just fine...

    now for my doubts... you are now trying to read email, but have you composed any "multipart" based emails using imap_mail_compose or any other method?

      Not as yet - I still have that joy to come.... too hard at work trying to read as many types of message that I can....

      Just to clarify, from the example above, this :

      imap_fetchbody($this->stream,$this->msg_uid,1.2,FT_UID)

      should get me the HTML portion.... ?

        Thanks for that - I'll have to keep trying...

        I had seen the website before from a link on PHP.net, a good site I must admit - good for attachment handling, although it is not exhaustive... Probably because the MIME standard has changed since...

          OK - thought I would post this here to help anyone else....

          This reads and shows all but 1 message type - RELATED !! I can't seem to be able to get the image to show in the message if it was sent with it and have a reference like CID:image.gif - If anyone knows how to do this - please let me know... Thanks.

          But here is the get message function that gets almost everything else - not pretty and quite long, but does the job. Needs some cleaning up as well, but I didn't want to do all the work for you 😉

          Is posted as for some reason the file isn't counted as a text file ??? Very odd...
          Posted in its own message as post is too long....

            Seriously amended code...

            function getMsg($download_action, $mail_action) 
                {
            
                if(!$this->msg_uid) {
                $this->buildErrorMsg($this->ERR_STR_MSG_UID_INVALID, imap_last_error());
                return false;
            }
            
            $msg_no = imap_msgno($this->stream, $this->msg_uid);
            
            $headers = @imap_header($this->stream, $msg_no);
            if(!$headers) {
                $this->buildErrorMsg($this->ERR_STR_MSG_NO_INVALID, imap_last_error());
                return false;
            }
            
            $arr["date"] = gmstrftime ("%b %d %Y %H:%M:%S", strtotime($headers->date));
            $arr["raw_from"] = $this->decodeHeader($headers->fromaddress);
            $arr["raw_cc"] = $this->decodeHeader($headers->ccaddress);
            $arr["from"] = $this->makeAddress($headers->from, "$mail_action&msg_uid=$this->msg_uid&mailbox=$this->mailbox");
            $arr["cc"] = $this->makeAddress($headers->cc, "$mail_action&msg_uid=$this->msg_uid&mailbox=$this->mailbox");
            $arr["subject"] = $this->decodeHeader($headers->subject);
            if(empty($arr["subject"])) $arr["subject"] = $this->STR_NO_SUBJECT;
            
            $arr["message_id"] = $headers->message_id;
            $arr["references"] = $headers->references;
            //from here - need to work out the MIME type etc and produce the download link accordingly.
            	$struct = @imap_fetchstructure($this->stream, $this->msg_uid, FT_UID);
            	$arr["struct"]=$struct;
            	$arr["num_parts"] = count($struct->parts) - 1;
            	$html = 0;
            	/* this will give a list of all necessary mime type headers to sort out the message & attachments.
            	echo "<font color=green>type ".$struct->type."\n<br>";
            	echo "encoding ".$struct->encoding."\n<br>";
            	echo "subtype ".$struct->subtype."\n<br>";
            	echo "description ".$struct->description."\n<br>";
            	echo "id ".$struct->id."\n<br>";
            	echo "lines ".$struct->lines."\n<br>";
            	echo "bytes ".$struct->bytes."\n<br>";
            	echo "disposition ".$struct->disposition."\n<br>";
            	echo "ifdparameters ".$struct->ifdparameters."\n<br>";
            	echo "dparameters ".$struct->dparameters."\n<br>";
            	echo "dparameters=>filename ".$struct->dparameters[0]->attribute."\n<br>";
            	echo "dparameters=>value ".$struct->dparameters[0]->value."\n<br>";
            	echo "parameters ".count($struct->parameters->value)."\n<br>";
            	echo "parts ".count($struct->parts)."\n<br></font><br>";
            	echo "subs ".$struct->parts[0]->subtype;
            	echo imap_fetchbody($this->stream, $this->msg_uid, "1.2", FT_UID);
            
            /*This is the main message body handling area*/
            /*********************************************/
            if (($struct->parts !=0 && strtolower($struct->disposition) != "attachment") || $struct->bytes!=0) {
            	if ($struct->type == 1) //multipart message
            	{
            		if (strtolower($struct->subtype)=="alternative")
            		{
            			for($i=0; $i< count($struct->parts); $i++) {
            				$type[$i]=$struct->parts[$i]->subtype;
            			}
            			if (strtolower($type[0])=="html")
            			{
            				$text=imap_fetchbody($this->stream, $this->msg_uid, "1", FT_UID);
            				if ($struct->parts[1]->encoding ==3) {
            					$arr["body"] = imap_base64($text);
            					$html = 1;
            				}
            				else if ($struct->parts[1]->encoding == 4 ) {
            					$arr["body"] = imap_qprint($text);
            					$html = 1;
            				}
            				else if ($struct->parts[1]->encoding == 0) {
            					$arr["body"] = $text;
            					$html = 1;
            				}
            			}
            			elseif (strtolower($type[1])=="html")
            			{
            				$text=imap_fetchbody($this->stream, $this->msg_uid, "2", FT_UID);
            				if ($struct->parts[1]->encoding ==3) {
            					$arr["body"] = imap_base64($text);
            					$html = 1;
            				}
            				else if ($struct->parts[1]->encoding == 4 ) {
            					$arr["body"] = imap_qprint($text);
            					$html = 1;
            				}
            				else if ($struct->parts[1]->encoding == 0) {
            					$arr["body"] = $text;
            					$html = 1;
            				}
            			}
            		}
            		if (strtolower($struct->parts[0]->subtype)=="alternative")
            		{
            			for($i=0; $i< count($struct->parts); $i++) {
            				$type[$i]=$struct->parts[$i]->parts[1]->subtype;
            			}
            			if (strtolower($type[0])=="html")
            			{
            				$text=imap_fetchbody($this->stream, $this->msg_uid, "1.2", FT_UID);
            				if ($struct->parts[0]->parts[1]->encoding ==3) {
            					$arr["body"] = imap_base64($text);
            					$html = 1;
            				}
            				else if ($struct->parts[0]->parts[1]->encoding == 4 ) {
            					$arr["body"] = imap_qprint($text);
            					$html = 1;
            				}
            				else if ($struct->parts[0]->parts[1]->encoding == 0) {
            					$arr["body"] = $text;
            					$html = 1;
            				}
            			}
            			elseif (strtolower($type[1])=="html")
            			{
            				$text=imap_fetchbody($this->stream, $this->msg_uid, "1.2", FT_UID);
            				if ($struct->parts[0]->parts[1]->encoding ==3) {
            					$arr["body"] = imap_base64($text);
            					$html = 1;
            				}
            				else if ($struct->parts[0]->parts[1]->encoding == 4 ) {
            					$arr["body"] = imap_qprint($text);
            					$html = 1;
            				}
            				else if ($struct->parts[0]->parts[1]->encoding == 0) {
            					$arr["body"] = $text;
            					$html = 1;
            				}
            			}
            		}
            	}
            	else {
            		if($struct->parts[0]->encoding == 3) {
            	    		$arr["body"] = imap_base64(imap_fetchbody($this->stream, $this->msg_uid, 1, FT_UID));
            	    		if(strtolower($struct->parts[0]->subtype) == 'html') $html = 1;
            		} else if($struct->parts[0]->encoding == 4) {
            	    		$arr["body"] = imap_qprint(imap_fetchbody($this->stream, $this->msg_uid, 1, FT_UID));
            	    		if(strtolower($struct->parts[0]->subtype) == 'html') $html = 1;
            		} else {
            	    		if($struct->encoding == 3) {
            	    			$arr["body"] = imap_base64(imap_fetchbody($this->stream, $this->msg_uid, 1, FT_UID));
            	    			if(strtolower($struct->subtype) == 'html') $html = 1;
            			} else if($struct->encoding == 4) {
            	    			$arr["body"] = imap_qprint(imap_fetchbody($this->stream, $this->msg_uid, 1, FT_UID));
            	    			if(strtolower($struct->subtype) == 'html') $html = 1;
            			} else { 
            	    			$arr["body"] = imap_fetchbody($this->stream, $this->msg_uid, 1, FT_UID);
            	    			if(strtolower($struct->subtype) == 'html') $html = 1;		
            			}
            		}
            	}
            }
            //this next line is the test line to print all the body text as one text document
            //$arr["body"]=imap_body($this->stream,$this->msg_uid,FT_UID);
            //remove the above line in reality
            if(!$html) {
                    $arr["body"] = str_replace("\r\n", "<BR>", $arr["body"]);
                $arr["body"] = eregi_replace( "http://([-a-z0-9\_\./~@?=%(&amp;)|]+)", "<A HREF=\"http://\\1\">[url]http://\\1[/url]</A>", $arr["body"]);
                $arr["body"] = eregi_replace( "ftp://([-a-z0-9\_\./~@?=%&amp;]+)", "<A HREF=\"ftp://\\1\">[url]ftp://\\1[/url]</A>", $arr["body"]);
                $arr["body"] = eregi_replace( "([-a-z0-9\_\.]+)@([-a-z0-9\_\.]+)", "<A HREF=\"$PHP_SELF?$mail_action&email=\\1@\\2\">\\1@\\2</A>", $arr["body"]);
            }
            /*End of the message body handling area*/
            /***************************************/
            
            /*This is the start of all attachment handling in the main message */
            /******************************************************************/
            if ($struct->ifdparameters)
            {
            	for($i=0; $i< count($struct->dparameters); $i++) {
            		if (strtolower($struct->dparameters[$i]->attribute) == "filename"){
            			$filename= $this->decodeHeader($struct->dparameters[$i]->value);
            			$arr["parts"][$i] = $this->build_url("$download_action&mailbox=$this->mailbox&msg_uid=$this->msg_uid&part_no=$i&filename=$filename", $filename);
            		}		
            	}
            }
            $num=count($struct->parts);
            for($i=0; $i< $num; $i++) {
            	if (strtolower($struct->parts[$i]->disposition)=="attachment")
            	{
            		for($z=0; $z< count($struct->parts[$i]->dparameters); $z++) {
            			if (strtolower($struct->parts[$i]->dparameters[$z]->attribute) == "filename"){
            				$filename= $this->decodeHeader($struct->parts[$i]->dparameters[$z]->value);
            				$arr["parts"][$i] = $this->build_url("$download_action&mailbox=$this->mailbox&msg_uid=$this->msg_uid&part_no=$i&filename=$filename", $filename);		
            			}
            		}
            	}
            	else
            	{
            		foreach($struct->parts[$i]->parameters as $attr)
            		if(strtolower($attr->attribute) == 'name') {
            			$filename = $this->decodeHeader($attr->value);
            			break;
            		}
            		if ($filename){
                    		$arr["parts"][$i] = $this->build_url("$download_action&mailbox=$this->mailbox&msg_uid=$this->msg_uid&part_no=$i&filename=$filename", $filename);
                		}
                	}
            }
            /*The attachment handling stops here*/
            /************************************/
            return $arr;
            }   
            
            

            Cuts out about 50 lines or so....

              Write a Reply...