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;
}
}
}
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