PHP: mailparse_msg_extract_part - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mailparse_msg_free mailparse_msg_extract_part_file

Last updated: Fri, 26 Jan 2007

view this page in

# mailparse_msg_extract_part

(4.1.0 - 4.1.2 only, PECL)

mailparse_msg_extract_part -- Extracts/decodes a message section

## Описание

void mailparse_msg_extract_part ( resource rfc2045, string msgbody [, callback callbackfunc] )

Внимание
Эта функция является ЭКСПЕРИМЕНТАЛЬНОЙ. Поведение этой функции, ее имя и относящаяся к ней документация могут измениться в последующих версиях PHP без уведомления. Используйте эту функцию на свой страх и риск.

Внимание
К настоящему времени эта функция еще не была документирована; для ознакомления доступен только список аргументов.

If callbackfunc is not specified, the contents will be sent to "stdout".

add a note User Contributed Notes
mailparse_msg_extract_part
Will
04-Jan-2007 12:12
`In mailparse version 2.1.1 (and perhaps earlier), when using mailparse_msg_extract_part() with a callback function, it breaks the data it passes to it into 4kB chunks and calls the callback function for each chunk. So, for example, if it's extracting a 41kB MIME part, the callback function you define will be called 11 times, each time with the next chunk of data. Here's some quick-and-dirty code that shows one way to handle this:
<?php
$message = file_get_contents ("email.txt"); // Pull in the e-mail.
function catch_part ($part)
{
$GLOBALS["part_data"] .= $part; // Append the data onto any previously extracted data.
}
mailparse_msg_extract_part ("1.1", $message, "catch_part"); // Extract MIME part 1.1
echo $GLOBALS["part_data"]; // Print out the extracted part.
?>
There's probably a much better way of dealing with this, but hey. It's what I got.`
php at cdauth dot de
07-Jun-2006 12:01
`substr() uses the string length, not the position as third argument. The corrected version of the following code line:
<?php
$parts[$s] = substr($file_txt, $starting_pos_body, $ending_pos_body-$starting_pos_body);
?>`
steve at tequilasolutions dot com
06-Apr-2006 10:36
`Unless I've missed something obvious:
get_structure returns array(1,1.1,1.1.2) etc but its not easy to get the contents of each part as mailparse_msg_extract_part() and mailparse_msg_extract_part_file() just return the lot. However get_part_data will return the string offsets so you know where to chop the message so you can get the contents of the parts.
Only issue is get_part_data returns:
[starting-pos] => 0
[starting-pos-body] => 1412
[ending-pos] => 14989
[ending-pos-body] => 14989
Unless I'm missed something else, theres a bug here as ending-pos is the same as ending-pos-body so it won't chop the contents cleanly, leaving the:
------=_NextPart_000_0069_01C659A6.9072E590--
...as supposedly part of the section contents.
$file = "..../mail"; // path of your mail
$file_txt = implode("",file($file));
$parse = mailparse_msg_parse_file($file);
$structure = mailparse_msg_get_structure($parse);
// chop message parts into array
$parts = array();
foreach ($structure as $s){
print "Part $s\n";
print "--------------------------------------\n";
$part = mailparse_msg_get_part($parse, $s);
$part_data = mailparse_msg_get_part_data($part);
print_r($part_data);
$starting_pos_body = $part_data['starting-pos-body'];
$ending_pos_body = $part_data['ending-pos-body'];
$parts[$s] = substr($file_txt,$starting_pos_body,$ending_pos_body); // copy data into array
print "[".$parts[$s]."]";
print "\n------------------------------------\n";
}`
add a note
mailparse_msg_free mailparse_msg_extract_part_file

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST