LXVI. mailparse Functions



本扩展模块是实验性的。本模块的行为,包括其函数的名称以及其它任何关于此模块的文档可能会在没有通知的情况下随 PHP 以后的发布而改变。使用本扩展模块风险自担。

本扩展已被移动到 PECL 库中且自以下版本起不再被绑定到 PHP 中:4.2.0.


PECL 扩展未绑定于 PHP 中。 进一步信息例如新版本,下载,源程序,维护者信息以及更新日志可以在此找到: http://pecl.php.net/package/mailparse.

In order to use these functions you must compile PHP with mailparse support by using the --enable-mailparse configure option.

Windows users will enable php_mailparse.dll inside of php.ini in order to use these functions. 可以从 PHP 下载页面或者 http://snaps.php.net/ 下载此 PECL 扩展的 DLL 文件。

mailparse_determine_best_xfer_encoding --  Figures out the best way of encoding the content read from the file pointer fp, which must be seek-able
mailparse_msg_create -- Returns a handle that can be used to parse a message
mailparse_msg_extract_part_file -- Extracts/decodes a message section, decoding the transfer encoding
mailparse_msg_extract_part --  Extracts/decodes a message section
mailparse_msg_free -- Frees a handle allocated by mailparse_msg_create()
mailparse_msg_get_part_data -- Returns an associative array of info about the message
mailparse_msg_get_part -- Returns a handle on a given section in a mimemessage
mailparse_msg_get_structure -- Returns an array of mime section names in the supplied message
mailparse_msg_parse_file -- Parse file and return a resource representing the structure
mailparse_msg_parse -- Incrementally parse data into buffer
mailparse_rfc822_parse_addresses --  Parse addresses and returns a hash containing that data
mailparse_stream_encode --  Streams data from source file pointer, apply encoding and write to destfp
mailparse_uudecode_all --  Scans the data from fp and extract each embedded uuencoded file

add a note add a note User Contributed Notes
iwarner at triangle-solutions dot com
21-May-2004 11:31
Also dont forget to LOAD mbstring before you load mailparse

example in the php.ini place in this order:


Or you will get an error.

boris at gamate dot com
12-Sep-2003 09:11
Example how to handle mail content from a variable:


= [...] // Mail Content from pipe or whatever

$mail = mailparse_msg_create();
$struct = mailparse_msg_get_structure($mail);

$struct as $st) {
$section = mailparse_msg_get_part($mail, $st);
$info = mailparse_msg_get_part_data($section);

toffe at dev dot null dot se
11-May-2003 01:50
To install mailparse on later versions of php, 4.3.1 in my case, you have to compile it with --enable-mbstring then do:

# pear install mailparse

This should download/compile/install the mailparse module...
To use it, either put extension=mailparse.so in your php.ini or use dl("mailparse.so"); in each script you need the mailparse capabilities in.

/C A
wberrier at yahoo dot com
09-Jul-2002 03:45
[Authors note:
The tarball for 4.2.x can be found here:
and contains a script called try.php that demonstrates the usage of these functions.

I've pasted the contents of the file below:

 * This is a simple email viewer.
 * make sure that $filename points to a file containing an email message and
 * load this page in your browser.
 * You will be able to choose a part to view.
 * */

$filename = "uumsg";

/* parse the message and return a mime message resource */
$mime = mailparse_msg_parse_file($filename);
/* return an array of message parts - this contsists of the names of the parts
 * only */
$struct = mailparse_msg_get_structure($mime);

/* print a choice of sections */
foreach($struct as $st) {
"<td><a href=\"$PHP_SELF?showpart=$st\">$st</a></td>\n";
/* get a handle on the message resource for a subsection */
$section = mailparse_msg_get_part($mime, $st);
/* get content-type, encoding and header information for that section */
$info = mailparse_msg_get_part_data($section);
"<td>" . $info["content-type"] . "</td>\n";
"<td>" . $info["content-disposition"] . "</td>\n";
"<td>" . $info["disposition-filename"] . "</td>\n";
"<td>" . $info["charset"] . "</td>\n";

/* if we were called to display a part, do so now */
if ($showpart)  {
/* get a handle on the message resource for the desired part */
$sec = mailparse_msg_get_part($mime, $showpart);

"<table border=1><tr><th>Section $showpart</th></tr><tr><td>";
/* extract the part from the message file and dump it to the output buff
         * */
mailparse_msg_extract_part_file($sec, $filename);
$contents = ob_get_contents();
/* quote the message for safe display in a browser */
echo nl2br(htmlentities($contents)) . "</td></tr></table>";;