note 4880 modified in function.imap-fetchbody by felipe

From: Date: Sun, 20 Jul 2008 23:39:14 +0000
Subject: note 4880 modified in function.imap-fetchbody by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142358@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: <?php 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"); ?> 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). --was-- 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://php.net/manual/en/function.imap-fetchbody.php

« previous php.notes (#142358) next »