imap_bodystruct

(PHP 3 >= 3.0.4, PHP 4, PHP 5)

imap_bodystruct --  Read the structure of a specified body section of a specific message

Description

object imap_bodystruct ( resource stream_id, int msg_no, string section )

警告

本函数暂无文档,仅有参数列表。


add a note add a note User Contributed Notes
giraldo at nitrogendesign dot com
18-Jun-2004 06:59
Here is a great snippet of code from our php email application (www.nitrogenlabs.com). This function will give you the filenames of each attachment even if the attachment is in the first part of the body...

<?php

function getAttachments($mbox,$msgNum){
 
$structure = imap_fetchstructure($mbox,$msgNum);
 
$contentParts = count($structure->parts);
  if(
$contentParts > 1){
   for(
$i=1; $i<=$contentParts; $i++){
         if(
$structure->parts[$i]->type > 0){
      
$attachments .= $structure->parts[$i]->description;
     }
   }
  }
  return
$attachments;
}
  }
  return
$attachments;
}

?>
omeko at dolfijn dot nl
08-Oct-2003 08:58
The above mentioned suggestions and code for handling attachments have some serious flaws; especially concering mails containing attachments sent bij PHP-applications [hello Manuel!] or SteveJobbs-aslongasitlookscool-clients.
After some extended reverse engineering I came to the following conclusions:
1) Don't count on a "inline" or "attachment" disposition to mark a bodypart containing attachment data.
2) Never assume your attachment is NOT in the first bodypart
(i.e. default [$key]+2 constructions to start a download)
3) Do not assume your filename is in parameters and most certainly not in parameters[0] or parameters[1]

The way to go is to loop through ALL  bodyparts and look for a parameters-attribute=="NAME" and - if not there - for a dparameters-attribute=="FILENAME" (note the "d" in front of parameters here!) and get the value to get the name of the attachment.

Please notice APPLEDOUBLE-encoded emails put its attachments in a parts array inside the parts array; meaning your loop has to go one level deep if (stristr($structure->parts[1]->subtype,"appledouble")).

Mark the part where you found the NAME or FILENAME attribute and increase +1 for your download routine. ie.:

$nr=$partno+1;
imap_fetchbody($mbox,$msgno,$nr);

If appledouble-encoded $nr is constructed like $partno1+1.".".$partno2+1

I won't publish the code here because it's a mess and I'm ashamed of it and I'm not sure if it covers APPLESINGLE and I don't feel like cleaning it up unless someone wants an article on it and I'm gonna be bloody payed to write it.

Yes, this IS a hint.

Good luck, lads.
php-dev(at)steffer(dot)dk
14-Sep-2002 10:43
Since I have gotten many request of my code I desided to write an article on attachment handling so tune in on:
http://www.linuxscope.net/articles/mailAttachmentsPHP.html

:-)
developers(at)steffer(dot)dk
10-Aug-2002 11:43
After playing around with the imap_bodystruct() function I found out, that it behaves like imap_fetchstructure, but as I can see only for the body-part of the message, no headers here.
You can do following (is tested):

$bodystruct = imap_bodystruct($mbox,$msgno,$partNumber)

$bodystruct->type
- returns an integer [0 - 7] -

$bodystruct->encoding
- returns an integer [0 - 5] -

$bodystruct->bytes
- return size of message in bytes -

$bodystruct->parameters
- returns an array with pairs of 'attribute' and 'value'

When writing attachment-handling for a mail client you will be able to fetch the filename of the attachment this way.

$struct = imap_fetchstructure($mbox,$msgno);
$contentParts = count($struct->parts);

if ($contentParts >= 2) {
   for ($i=2;$i<=$contentParts;$i++) {
         $att[$i-2] = imap_bodystruct($mbox,$msgno,$i);
   }
   for ($k=0;$k<sizeof($att);$k++) {
       if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value == "US-ASCII") {
           if ($att[$k]->parameters[1]->value != "") {
         $attachments[$k] = $att[$k]->parameters[1]->value;
   }
       } elseif ($att[$k]->parameters[0]->value != "iso-8859-1" && $att[$k]->parameters[0]->value != "ISO-8859-1") {
             $attachments[$k] = $att[$k]->parameters[0]->value;
       }
   }
}
I have written a mail client that can handle most attachment-types if anyone interested in the source for the attachment-handling I could mail it to you.
But be warned it's no beauty-code since i've been fighting with lag of the documentation available on this issue.
terence at vol dot net
23-Feb-2002 07:29
I am not sure if I have missed something but var_dump shows that im_bodystruct does not return parts array. Use im_fetchstructure instead.
vlad ( at php dot net)
12-Jan-2002 06:04
This is one of the functions that can not accept FT_UID flag, because of the limitations of the underlying imap client library. If you need that, use something like this:

imap_bodystruct($stream_id, imap_msgno($stream_id, $msg_uid_no), $section);

($msg_uid_no is the UID of the message).