Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Function Reference : Mailparse Functions

Mailparse Functions


Mailparse is an extension for parsing and working with email messages. It can deal with » RFC 822 and » RFC 2045 (MIME) compliant messages.

Mailparse is stream based, which means that it does not keep in-memory copies of the files it processes - so it is very resource efficient when dealing with large messages.


Mailparse requires the mbstring extension, and mbstring must be loaded before mailparse.

This extension has been moved to the » PECL repository and is no longer bundled with PHP as of PHP 4.2.0.


This » PECL extension is not bundled with PHP. Information for installing this PECL extension may be found in the manual chapter titled Installation of PECL extensions. Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: »

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. The DLL for this PECL extension may be downloaded from either the » PHP Downloads page or from »

Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

Table 157. Mailparse configuration options

Name Default Changeable Changelog
mailparse.def_charset "us-ascii" PHP_INI_ALL Available since PHP 4.1.0. Removed in PHP 4.2.0.

For further details and definitions of the PHP_INI_* constants, see the Appendix I, php.ini directives.

Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.


Table of Contents

mailparse_determine_best_xfer_encoding — Gets the best way of encoding
mailparse_msg_create — Create a mime mail resource
mailparse_msg_extract_part_file — Extracts/decodes a message section
mailparse_msg_extract_part — Extracts/decodes a message section
mailparse_msg_extract_whole_part_file — Extracts a message section including headers without decoding the transfer encoding
mailparse_msg_free — Frees a MIME resource
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 — Parses a file
mailparse_msg_parse — Incrementally parse data into buffer
mailparse_rfc822_parse_addresses — Parse RFC 822 compliant addresses
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

Code Examples / Notes » ref.mailparse


[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);
echo "<table>\n";
/* print a choice of sections */
foreach($struct as $st) {
       echo "<tr>\n";
       echo "<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);
       echo "\n";
       echo "<td>" . $info["content-type"] . "</td>\n";
       echo "<td>" . $info["content-disposition"] . "</td>\n";
       echo "<td>" . $info["disposition-filename"] . "</td>\n";
       echo "<td>" . $info["charset"] . "</td>\n";
       echo "</tr>";
echo "</table>";
/* 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);
       echo "<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>";;


Example how to handle mail content from a variable:
$buffer = [...] // Mail Content from pipe or whatever
$mail = mailparse_msg_create();
$struct = mailparse_msg_get_structure($mail);
foreach($struct as $st) {
$section = mailparse_msg_get_part($mail, $st);
$info = mailparse_msg_get_part_data($section);



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.

Change Language

Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Haru PDF Functions
hash Functions
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker