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; 
}
 }
} 
chiwangc
  • 3,566
  • 16
  • 26
  • 32
SNos
  • 3,430
  • 5
  • 42
  • 92
  • It's really not clear as to what you are asking. – Jonathan May 29 '15 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 – SNos May 29 '15 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. – Michael Berkowski May 29 '15 at 13:30
  • And it would appear that `[ifdisposition] => 1` in the same structure indicates there is a `disposition` property present. – Michael Berkowski May 29 '15 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? – Michael Berkowski May 29 '15 at 13:32
  • Hi Michael I would like to get all the disposition and get values of all the inline ones. – SNos May 29 '15 at 13:39
  • attached the resolute from echo var_export($structure); – SNos May 29 '15 at 13:42
  • I was testing with 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; } }} – SNos May 29 '15 at 13:43

1 Answers1

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

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • 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. – SNos May 29 '15 at 14:49
  • Thank you, this function help me a lot… and solved my question – SNos May 29 '15 at 23:47
  • Excellent, glad I could help. – Michael Berkowski May 30 '15 at 00:35
  • Any Advise how I can send the values from this function as strings to change the src links of inline img ? http://codepad.viper-7.com/zBnuGD – SNos May 30 '15 at 16:09
  • @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 http://codepad.viper-7.com/sqLDXh – Michael Berkowski May 30 '15 at 16:50
  • Thank you it works however, both of the images have the same file value. http://codepad.viper-7.com/6mlKTn – SNos May 30 '15 at 17:57
  • Oh, you are using the replacements differently than I realized, using the array of replacements against one original string. You need to use `preg_replace_callback()` for that, which is more complicated. http://codepad.viper-7.com/b642mu See also http://stackoverflow.com/questions/9416175/preg-replace-with-array-replacements – Michael Berkowski May 30 '15 at 19:48
  • yesss.. Thank you so much.. it works great. I would really understand more about regex and this kind of functions. Any good site ? – SNos May 30 '15 at 20:23