1

I am printing out a structure

$structure = imap_fetchstructure($imap, $email_number);
print_r($structure);

I need to look inside the array and get [disposition] tag of my inline attachment.

I am trying out using:

$inline_attachment = $structure[1]->disposition;

if($inline_attachment == inline){
    echo "inline attachment found!!";
}

stdClass Object (
      [type] => 1
      [encoding] => 0
      [ifsubtype] => 1
      [subtype] => RELATED
      [ifdescription] => 0
      [ifid] => 0
      [ifdisposition] => 0
      [ifdparameters] => 0
      [ifparameters] => 1
      [parameters] => Array
          (
              [0] => stdClass Object
                  (
                      [attribute] => boundary
                      [value] => 047d7b624dd8b40cbc05172d65ae
                  )

          )

      [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] => 047d7b624dd8b40cb805172d65ad
                                  )

                          )

                      [parts] => Array
                          (
                              [0] => stdClass Object
                                  (
                                      [type] => 0
                                      [encoding] => 0
                                      [ifsubtype] => 1
                                      [subtype] => PLAIN
                                      [ifdescription] => 0
                                      [ifid] => 0
                                      [lines] => 1
                                      [bytes] => 11
                                      [ifdisposition] => 0
                                      [ifdparameters] => 0
                                      [ifparameters] => 1
                                      [parameters] => Array
                                          (
                                              [0] => stdClass Object
                                                  (
                                                      [attribute] => charset
                                                      [value] => UTF-8
                                                  )

                                          )

                                  )

                              [1] => stdClass Object
                                  (
                                      [type] => 0
                                      [encoding] => 0
                                      [ifsubtype] => 1
                                      [subtype] => HTML
                                      [ifdescription] => 0
                                      [ifid] => 0
                                      [lines] => 1
                                      [bytes] => 136
                                      [ifdisposition] => 0
                                      [ifdparameters] => 0
                                      [ifparameters] => 1
                                      [parameters] => Array
                                          (
                                              [0] => stdClass Object
                                                  (
                                                      [attribute] => charset
                                                      [value] => UTF-8
                                                  )

                                          )

                                  )

                          )

                  )

              [1] => stdClass Object
                  (
                      [type] => 5
                      [encoding] => 3
                      [ifsubtype] => 1
                      [subtype] => JPEG
                      [ifdescription] => 0
                      [ifid] => 1
                      [id] => 
                      [bytes] => 25378
                      [ifdisposition] => 1
                      **[disposition] => inline**
                      [ifdparameters] => 1
                      [dparameters] => Array
                          (
                              [0] => stdClass Object
                                  (
                                      [attribute] => filename
                                      [value] => 1.jpg
                                  )

                          )

                      [ifparameters] => 1
                      [parameters] => Array
                          (
                              [0] => stdClass Object
                                  (
                                      [attribute] => name
                                      [value] => 1.jpg
                                  )

                          )

                  )

          )

  )

What is the best way to achieve this? And Also How can I understand properly how to move and get any specific element in array?

echo var_export($structure);

            stdClass::__set_state(array(
   'type' => 1,
   'encoding' => 0,
   'ifsubtype' => 1,
   'subtype' => 'RELATED',
   'ifdescription' => 0,
   'ifid' => 0,
   'ifdisposition' => 0,
   'ifdparameters' => 0,
   'ifparameters' => 1,
   'parameters' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'attribute' => 'boundary',
       'value' => '047d7b624dd8b40cbc05172d65ae',
    )),
  ),
   'parts' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'type' => 1,
       'encoding' => 0,
       'ifsubtype' => 1,
       'subtype' => 'ALTERNATIVE',
       'ifdescription' => 0,
       'ifid' => 0,
       'ifdisposition' => 0,
       'ifdparameters' => 0,
       'ifparameters' => 1,
       'parameters' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'attribute' => 'boundary',
           'value' => '047d7b624dd8b40cb805172d65ad',
        )),
      ),
       'parts' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'type' => 0,
           'encoding' => 0,
           'ifsubtype' => 1,
           'subtype' => 'PLAIN',
           'ifdescription' => 0,
           'ifid' => 0,
           'lines' => 1,
           'bytes' => 11,
           'ifdisposition' => 0,
           'ifdparameters' => 0,
           'ifparameters' => 1,
           'parameters' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'attribute' => 'charset',
               'value' => 'UTF-8',
            )),
          ),
        )),
        1 => 
        stdClass::__set_state(array(
           'type' => 0,
           'encoding' => 0,
           'ifsubtype' => 1,
           'subtype' => 'HTML',
           'ifdescription' => 0,
           'ifid' => 0,
           'lines' => 1,
           'bytes' => 136,
           'ifdisposition' => 0,
           'ifdparameters' => 0,
           'ifparameters' => 1,
           'parameters' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'attribute' => 'charset',
               'value' => 'UTF-8',
            )),
          ),
        )),
      ),
    )),
    1 => 
    stdClass::__set_state(array(
       'type' => 5,
       'encoding' => 3,
       'ifsubtype' => 1,
       'subtype' => 'JPEG',
       'ifdescription' => 0,
       'ifid' => 1,
       'id' => '',
       'bytes' => 25378,
       'ifdisposition' => 1,
       'disposition' => 'inline',
       'ifdparameters' => 1,
       'dparameters' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'attribute' => 'filename',
           'value' => '1.jpg',
        )),
      ),
       'ifparameters' => 1,
       'parameters' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'attribute' => 'name',
           'value' => '1.jpg',
        )),
      ),
    )),
  ),
))



for($i = 0; $i < count($structure->parts); $i++) { 
if($structure->parts[$i]->disposition == inline) { 
foreach($structure->parts[$i]->dparameters as $object) { 
$src_cid = $object->value; 
}
 }
} 
8
  • It's really not clear as to what you are asking. Commented May 29, 2015 at 12:46
  • sorry for my english. what I need to do is to look in the structure and see if there is an object with [disposition] => inline. If yes get the [attribute] and [value] => 1.jpg Commented May 29, 2015 at 12:49
  • It generates a nested structure, where the object containing the disposition you highlighted is within another that itself could have a disposition. Do you want to recurse through the structure and get all of the disposition elements, or the first one? If there were multiple attachments, I believe you will have several. Commented May 29, 2015 at 13:30
  • And it would appear that [ifdisposition] => 1 in the same structure indicates there is a disposition property present. Commented May 29, 2015 at 13:31
  • Would you mind posting the output from echo var_export($that_array); in addition to the print_r() to make it easier for us to prototype a response? Commented May 29, 2015 at 13:32

1 Answer 1

1

Using a recursive function, you can traverse into the object's $parts arrays to check the value of ifdisposition == 1, and retrieve the dparameters[0]->value value.

This should be sufficient to get you started:

function getDispositions($obj) {
  // Check ifdisposition is 1
  if (isset($obj->ifdisposition) && $obj->ifdisposition == 1) {
    // Print the disposition
    echo "Disposition: {$obj->disposition}\n";
    // And print the first parameter's value
    echo "Filename: {$obj->dparameters[0]->value}\n";
    // You may need to loop here instead like
    //foreach ($obj->dparameters as $p) {
    //  echo $p->value;
    //}
  }
  // If this has a parts array
  if (isset($obj->parts)) {
    // Loop over each part and call the getDispositions()
    // function recursively to act on each nested part
    foreach ($obj->parts as $part) {
      getDispositions($part);
    }   
  }
}

// Call the function on your outer structure
getDispositions($structure);

This will print:

Disposition: inline
Filename: 1.jpg

Here is a demonstration, adding one additional filename so it prints two dispositions and filename values: http://codepad.viper-7.com/1zuxfo

Sign up to request clarification or add additional context in comments.

7 Comments

thank you very much for your explanation. As soon As i will be indoor I will test it in my script and let you know.
Thank you, this function help me a lot… and solved my question
Any Advise how I can send the values from this function as strings to change the src links of inline img ? codepad.viper-7.com/zBnuGD
@SNos An array reference to the function parameter builds up an array of all the disposition/filename pairs. Then you can loop over the array and do whatever string manipulations you need codepad.viper-7.com/sqLDXh
Thank you it works however, both of the images have the same file value. codepad.viper-7.com/6mlKTn
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.