Page 1 of 1

Best way to determine the structure of a MIME mail?

Posted: Sun Jun 01, 2008 4:00 am
by vargadanis
Hi!

I am trying to write a class which would read out the body of a MIME message. I do not have problems with TEXT/PLAIN message as the imap_fetchbody(xx,xx,1) does the job perfictly or I could even use imap_body function.
When it comes to more complex Mails, I have trouble. I have not found any good idea on that this why I ask. :) Let's test the community :) Muhaha
This post will be long but do not quit yet! I just want to demonstrate what I don't know :)
Here is a mail:

Code: Select all

stdClass Object
(
    [type] => 1
    [encoding] => 0
    [ifsubtype] => 1
    [subtype] => MIXED
    [ifdescription] => 0
    [ifid] => 0
    [ifdisposition] => 0
    [ifdparameters] => 0
    [ifparameters] => 1
    [parameters] => Array
        (
            [0] => stdClass Object
                (
                    [attribute] => BOUNDARY
                    [value] => ----=_Part_17079_14032152.1212257923471
                )
 
        )
 
    [parts] => Array
        (
            [0] => stdClass Object
                (
                    [type] => 1
                    [encoding] => 0
                    [ifsubtype] => 1
                    [subtype] => ALTERNATIVE
                    [ifdescription] => 0
                    [ifid] => 0
                    [ifdisposition] => 0
                    [ifdparameters] => 0
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => BOUNDARY
                                    [value] => ----=_Part_17080_27976225.1212257923471
                                )
 
                        )
 
                    [parts] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [type] => 0
                                    [encoding] => 4
                                    [ifsubtype] => 1
                                    [subtype] => PLAIN
                                    [ifdescription] => 0
                                    [ifid] => 0
                                    [lines] => 27
                                    [bytes] => 1316
                                    [ifdisposition] => 1
                                    [disposition] => INLINE
                                    [ifdparameters] => 0
                                    [ifparameters] => 1
                                    [parameters] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [attribute] => CHARSET
                                                    [value] => ISO-8859-1
                                                )
 
                                        )
 
                                )
 
                            [1] => stdClass Object
                                (
                                    [type] => 0
                                    [encoding] => 4
                                    [ifsubtype] => 1
                                    [subtype] => HTML
                                    [ifdescription] => 0
                                    [ifid] => 0
                                    [lines] => 61
                                    [bytes] => 3025
                                    [ifdisposition] => 1
                                    [disposition] => INLINE
                                    [ifdparameters] => 0
                                    [ifparameters] => 1
                                    [parameters] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [attribute] => CHARSET
                                                    [value] => ISO-8859-1
                                                )
 
                                        )
 
                                )
 
                        )
 
                )
 
            [1] => stdClass Object
                (
                    [type] => 3
                    [encoding] => 3
                    [ifsubtype] => 1
                    [subtype] => VND.MS-POWERPOINT
                    [ifdescription] => 0
                    [ifid] => 0
                    [bytes] => 1151595
                    [ifdisposition] => 1
                    [disposition] => ATTACHMENT
                    [ifdparameters] => 1
                    [dparameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => FILENAME
                                    [value] => Fleming.ppt
                                )
 
                        )
 
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => NAME
                                    [value] => Fleming.ppt
                                )
 
                        )
 
                )
 
            [2] => stdClass Object
                (
                    [type] => 0
                    [encoding] => 3
                    [ifsubtype] => 1
                    [subtype] => PLAIN
                    [ifdescription] => 0
                    [ifid] => 0
                    [lines] => 37
                    [bytes] => 1839
                    [ifdisposition] => 1
                    [disposition] => ATTACHMENT
                    [ifdparameters] => 1
                    [dparameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => FILENAME
                                    [value] => disclaimer.txt
                                )
 
                        )
 
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => NAME
                                    [value] => disclaimer.txt
                                )
 
                        )
 
                )
 
        )
 
)
 
[/size]

This is a MULTIPART/MIXED mail. Is the text, the body entity of the mail, always the 1st one? How do I determine the "part_number" for imap_fetchbody() ?
To know, or not to know, that is the Question:
Because 'tis Number is the reason for suffer ...
:banghead:

Thanx a lot

Re: Best way to determine the structure of a MIME mail?

Posted: Thu Jun 05, 2008 9:13 am
by vargadanis
Just to give you something that I have done so far. It works quite good.

Code: Select all

       public function determineBody($num, $mstructure = NULL){
            $mnum = $num;
            if($mstructure == NULL)
              $mstructure = $this->getFetchStruct($mnum);            
            /*print "<pre>";
            print_r($mstructure);
            print "</pre>";*/
            $mime = strtoupper($this->_mtypes[$mstructure->type]."/".$mstructure->subtype);
            switch ($mime){
                case "TEXT/PLAIN":
                    $body = imap_fetchbody($this->_imap_connection, $mnum, "1");
                    break;
                case "MULTIPART/ALTERNATIVE":                
                    $this->_level = $this->_level."1";  //text - if 2 HTML                    
                    $body = imap_fetchbody($this->_imap_connection, $mnum, $this->_level);
                    break;
                case "MULTIPART/MIXED":                    
                    $this->_level = $this->_level."1.";
                    $this->determineBody($mstructure->parts[0]);                    
                    break;                
                case "MULTIPART/RELATED":
                    $this->_level = $this->_level."1.";
                    $this->determineBody($mstructure->parts[0]);
                    break;
                case "MULTIPART/DIGEST":                    
                case "MESSAGE/RFC822":                
                    print "RFC822";                
                default:
                    $toexamine = $mstructure;
                
            }