note 4880 modified in imap_fetchbody by ronabop
| From: | ronabop@php.net | Date: | Fri, 11 Aug 2000 15:23:27 +0000 |
| Subject: | note 4880 modified in imap_fetchbody by ronabop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-1169@lists.php.net to get a copy of this message | ||
Here's a routine that yields the part of a message with a particular MIME type:
<PRE>
function get_mime_type(&$structure)
{
$primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE",
"APPLICATION", "AUDIO", "IMAGE", "VIDEO",
"OTHER");
if($structure->subtype)
{
return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype;
}
return "TEXT/PLAIN";
}
function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false)
{
if(!$structure)
{
$structure = imap_fetchstructure($stream, $msg_number);
}
if($structure)
{
if($mime_type == get_mime_type($structure))
{
if(!$part_number)
{
$part_number = "1";
}
$text = imap_fetchbody($stream, $msg_number, $part_number);
if($structure->encoding == 3)
{
return imap_base64($text);
}
else if($structure->encoding == 4)
{
return imap_qprint($text);
}
else
{
return $text;
}
}
if($structure->type == 1) /* multipart */
{
while(list($index, $sub_structure) = each($structure->parts))
{
if($part_number)
{
$prefix = $part_number . '.';
}
$data = get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));
if($data)
{
return $data;
}
}
}
}
return false;
}
// get plain text
$data = get_part($stream, $msg_number, "TEXT/PLAIN");
// get HTML text
$data = get_part($stream, $msg_number, "TEXT/HTML");
</PRE>
Right now only the first part with the matching MIME type is returned. A more useful version would
create an array and return all matching parts (for GIFs, for instance).
http://www.php.net/manual/function.imap-fetchbody.php