Location: PHPKode > scripts > MIME E-mail message parser > mime-e-mail-message-parser/mime_parser_class.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Class: MIME parser</title>
</head>
<body>
<center><h1>Class: MIME parser</h1></center>
<hr />
<ul>
<p><b>Version:</b> <tt>@(#) $Id: mime_parser.php,v 1.60 2008/07/17 00:54:24 mlemos Exp $</tt></p>
<h2><a name="table_of_contents">Contents</a></h2>
<ul>
<li><a href="#2.1.1">Summary</a></li>
<ul>
<li><a href="#3.2.0">Name</a></li>
<li><a href="#3.2.0.0">Author</a></li>
<li><a href="#3.2.0.1">Copyright</a></li>
<li><a href="#3.2.0.2">Version</a></li>
<li><a href="#3.2.0.3">Purpose</a></li>
<li><a href="#3.2.0.4">Usage</a></li>
</ul>
<li><a href="#4.1.1">Variables</a></li>
<ul>
<li><a href="#5.2.11">error</a></li>
<li><a href="#5.2.12">error_position</a></li>
<li><a href="#5.2.13">mbox</a></li>
<li><a href="#5.2.14">decode_headers</a></li>
<li><a href="#5.2.15">decode_bodies</a></li>
<li><a href="#5.2.16">extract_addresses</a></li>
<li><a href="#5.2.17">address_headers</a></li>
<li><a href="#5.2.18">message_buffer_length</a></li>
<li><a href="#5.2.19">ignore_syntax_errors</a></li>
<li><a href="#5.2.20">warnings</a></li>
</ul>
<li><a href="#6.1.1">Functions</a></li>
<ul>
<li><a href="#7.2.3">Decode</a></li>
<li><a href="#9.2.4">Analyze</a></li>
</ul>
</ul>
<p><a href="#table_of_contents">Top of the table of contents</a></p>
</ul>
<hr />
<ul>
<h2><li><a name="2.1.1">Summary</a></li></h2>
<ul>
<h3><a name="3.2.0">Name</a></h3>
<p>MIME parser</p>
<h3><a name="3.2.0.0">Author</a></h3>
<p>Manuel Lemos (<a href="mailto:mlemos-at-acm.org">mlemos-at-acm.org</a>)</p>
<h3><a name="3.2.0.1">Copyright</a></h3>
<p>Copyright &copy; (C) Manuel Lemos 2006 - 2008</p>
<h3><a name="3.2.0.2">Version</a></h3>
<p>@(#) $Id: mime_parser.php,v 1.60 2008/07/17 00:54:24 mlemos Exp $</p>
<h3><a name="3.2.0.3">Purpose</a></h3>
<p>Parse MIME encapsulated e-mail message data compliant with the RFC 2822 or aggregated in mbox format.</p>
<h3><a name="3.2.0.4">Usage</a></h3>
<p>Use the function <tt><a href="#function_Decode">Decode</a></tt> function to retrieve the structure of the messages to be parsed. Adjust its parameters to tell how to return the decoded body data. Use the <tt>SaveBody</tt> parameter to make the body parts be saved to files when the message is larger than the available memory. Use the <tt>SkipBody</tt> parameter to just retrieve the message structure without returning the body data.</p>
<p> If the message data is an archive that may contain multiple messages aggregated in the mbox format, set the variable <tt><a href="#variable_mbox">mbox</a></tt> to 1.</p>
<p><a href="#table_of_contents">Table of contents</a></p>
</ul>
</ul>
<hr />
<ul>
<h2><li><a name="variables"></a><a name="4.1.1">Variables</a></li></h2>
<ul>
<li><tt><a href="#variable_error">error</a></tt></li><br />
<li><tt><a href="#variable_error_position">error_position</a></tt></li><br />
<li><tt><a href="#variable_mbox">mbox</a></tt></li><br />
<li><tt><a href="#variable_decode_headers">decode_headers</a></tt></li><br />
<li><tt><a href="#variable_decode_bodies">decode_bodies</a></tt></li><br />
<li><tt><a href="#variable_extract_addresses">extract_addresses</a></tt></li><br />
<li><tt><a href="#variable_address_headers">address_headers</a></tt></li><br />
<li><tt><a href="#variable_message_buffer_length">message_buffer_length</a></tt></li><br />
<li><tt><a href="#variable_ignore_syntax_errors">ignore_syntax_errors</a></tt></li><br />
<li><tt><a href="#variable_warnings">warnings</a></tt></li><br />
<p><a href="#table_of_contents">Table of contents</a></p>
<h3><a name="variable_error"></a><li><a name="5.2.11">error</a></li></h3>
<h3>Type</h3>
<p><tt><i>string</i></tt></p>
<h3>Default value</h3>
<p><tt>''</tt></p>
<h3>Purpose</h3>
<p>Store the message that is returned when an error occurs.</p>
<h3>Usage</h3>
<p>Check this variable to understand what happened when a call to any of the class functions has failed.</p>
<p> This class uses cumulative error handling. This means that if one class functions that may fail is called and this variable was already set to an error message due to a failure in a previous call to the same or other function, the function will also fail and does not do anything.</p>
<p> This allows programs using this class to safely call several functions that may fail and only check the failure condition after the last function call.</p>
<p> Just set this variable to an empty string to clear the error condition.</p>
<p><a href="#variables">Variables</a></p>
<h3><a name="variable_error_position"></a><li><a name="5.2.12">error_position</a></li></h3>
<h3>Type</h3>
<p><tt><i>int</i></tt></p>
<h3>Default value</h3>
<p><tt>-1</tt></p>
<h3>Purpose</h3>
<p>Point to the position of the message data or file that refers to the last error that occurred.</p>
<h3>Usage</h3>
<p>Check this variable to determine the relevant position of the message when a parsing error occurs.</p>
<p><a href="#variables">Variables</a></p>
<h3><a name="variable_mbox"></a><li><a name="5.2.13">mbox</a></li></h3>
<h3>Type</h3>
<p><tt><i>bool</i></tt></p>
<h3>Default value</h3>
<p><tt>0</tt></p>
<h3>Purpose</h3>
<p>Specify whether the message data to parse is a single RFC 2822 message or it is an archive that contain multiple messages in the mbox format.</p>
<h3>Usage</h3>
<p>Set this variable to 1 if it is it is intended to parse an mbox message archive.<br />
 mbox archives may contain multiple messages. Each message starts with the header <tt>From</tt>. Since all valid RFC 2822 headers must with a colon, the class will fail to parse a mbox archive if this variable is set to 0.</p>
<p><a href="#variables">Variables</a></p>
<h3><a name="variable_decode_headers"></a><li><a name="5.2.14">decode_headers</a></li></h3>
<h3>Type</h3>
<p><tt><i>bool</i></tt></p>
<h3>Default value</h3>
<p><tt>1</tt></p>
<h3>Purpose</h3>
<p>Specify whether the message headers should be decoded.</p>
<h3>Usage</h3>
<p>Set this variable to 1 if it is necessary to decode message headers that may have non-ASCII characters and use other character set encodings.</p>
<p><a href="#variables">Variables</a></p>
<h3><a name="variable_decode_bodies"></a><li><a name="5.2.15">decode_bodies</a></li></h3>
<h3>Type</h3>
<p><tt><i>bool</i></tt></p>
<h3>Default value</h3>
<p><tt>1</tt></p>
<h3>Purpose</h3>
<p>Specify whether the message body parts should be decoded.</p>
<h3>Usage</h3>
<p>Set this variable to 1 if it is necessary to parse the message bodies and extract its part structure.</p>
<p><a href="#variables">Variables</a></p>
<h3><a name="variable_extract_addresses"></a><li><a name="5.2.16">extract_addresses</a></li></h3>
<h3>Type</h3>
<p><tt><i>bool</i></tt></p>
<h3>Default value</h3>
<p><tt>1</tt></p>
<h3>Purpose</h3>
<p>Specify whether the message headers that usually contain e-mail addresses should be parsed and the addresses should be extracted by the <tt><a href="#function_Decode">Decode</a></tt> function.</p>
<h3>Usage</h3>
<p>Set this variable to 1 if it is necessary to extract the e-mail addresses contained in certain message headers.</p>
<p> The headers to be parsed are defined by the <tt><a href="#variable_address_headers">address_headers</a></tt> variable.</p>
<p> The parsed addresses are returned by the <tt>ExtractedAddresses</tt> entry of the <tt><a href="#argument_Decode_decoded">decoded</a></tt> argument of the <tt><a href="#function_Decode">Decode</a></tt> function.</p>
<p><a href="#variables">Variables</a></p>
<h3><a name="variable_address_headers"></a><li><a name="5.2.17">address_headers</a></li></h3>
<h3>Type</h3>
<p><tt><i>array</i></tt></p>
<h3>Default value</h3>
<p><tt>array()</tt></p>
<h3>Purpose</h3>
<p>Specify which headers contain addresses that should be parsed and extracted.</p>
<h3>Usage</h3>
<p>Change this variable if you need to extract e-mail addresses from a different list of message headers.</p>
<p> It must be set to an associative array with keys set to the names of the headers to be parsed including the colon. The array values must be set to a boolean flag to tell whether the headers with the respective name should be parsed. The header names must be in lower case.</p>
<p> By default the class addresses from the headers: 'from:', 'to:', 'cc:', 'bcc:', 'return-path:', 'reply-to:' and 'disposition-notification-to:'.</p>
<p><a href="#variables">Variables</a></p>
<h3><a name="variable_message_buffer_length"></a><li><a name="5.2.18">message_buffer_length</a></li></h3>
<h3>Type</h3>
<p><tt><i>int</i></tt></p>
<h3>Default value</h3>
<p><tt>8000</tt></p>
<h3>Purpose</h3>
<p>Maximum length of the chunks of message data that the class parse at one time.</p>
<h3>Usage</h3>
<p>Adjust this value according to the available memory.</p>
<p><a href="#variables">Variables</a></p>
<h3><a name="variable_ignore_syntax_errors"></a><li><a name="5.2.19">ignore_syntax_errors</a></li></h3>
<h3>Type</h3>
<p><tt><i>bool</i></tt></p>
<h3>Default value</h3>
<p><tt>1</tt></p>
<h3>Purpose</h3>
<p>Specify whether the class should ignore syntax errors in malformed messages.</p>
<h3>Usage</h3>
<p>Set this variable to 0 if it is necessary to verify whether message data may be corrupted due to to eventual bugs in the program that generated the message.</p>
<p> Currently the class only ignores some types of syntax errors. Other syntax errors may still cause the <tt><a href="#function_Decode">Decode</a></tt> to fail.</p>
<p><a href="#variables">Variables</a></p>
<h3><a name="variable_warnings"></a><li><a name="5.2.20">warnings</a></li></h3>
<h3>Type</h3>
<p><tt><i>array</i></tt></p>
<h3>Default value</h3>
<p><tt>array()</tt></p>
<h3>Purpose</h3>
<p>Return a list of positions of the original message that contain syntax errors.</p>
<h3>Usage</h3>
<p>Check this variable to retrieve eventual message syntax errors that were ignored when the <tt><a href="#variable_ignore_syntax_errors">ignore_syntax_errors</a></tt> is set to 1.</p>
<p> The indexes of this array are the positions of the errors. The array values are the corresponding syntax error messages.</p>
<p><a href="#variables">Variables</a></p>
<p><a href="#table_of_contents">Table of contents</a></p>
</ul>
</ul>
<hr />
<ul>
<h2><li><a name="functions"></a><a name="6.1.1">Functions</a></li></h2>
<ul>
<li><tt><a href="#function_Decode">Decode</a></tt></li><br />
<li><tt><a href="#function_Analyze">Analyze</a></tt></li><br />
<p><a href="#table_of_contents">Table of contents</a></p>
<h3><a name="function_Decode"></a><li><a name="7.2.3">Decode</a></li></h3>
<h3>Synopsis</h3>
<p><tt><i>bool</i> Decode(</tt><ul>
<tt>(input and output) <i>array</i> </tt><tt><a href="#argument_Decode_parameters">parameters</a></tt><tt>,</tt><br />
<tt>(output) <i>array</i> </tt><tt><a href="#argument_Decode_decoded">decoded</a></tt></ul>
<tt>)</tt></p>
<h3>Purpose</h3>
<p>Parse and decode message data and retrieve its structure.</p>
<h3>Usage</h3>
<p>Pass an array to the <tt><a href="#argument_Decode_parameters">parameters</a></tt> parameter to define whether the message data should be read and parsed from a file or a data string, as well additional parsing options. The <tt><a href="#argument_Decode_decoded">decoded</a></tt> returns the data structure of the parsed messages.</p>
<h3>Arguments</h3>
<ul>
<p><tt><b><a name="argument_Decode_parameters">parameters</a></b></tt> - Associative array to specify parameters for the message data parsing and decoding operation. Here follows the list of supported parameters that should be used as indexes of the array:</p>
<p> <tt>File</tt></p>
<p> Name of the file from which the message data will be read. It may be the name of a file stream or a remote URL, as long as your PHP installation is configured to allow accessing remote files with the <tt>fopen()</tt> function.</p>
<p> <tt>Data</tt></p>
<p> String that specifies the message data. This should be used as alternative data source for passing data available in memory, like for instance messages stored in a database that was queried dynamically and the message data was fetched into a string variable.</p>
<p> <tt>SaveBody</tt></p>
<p> If this parameter is specified, the message body parts are saved to files. The path of the directory where the files are saved is defined by this parameter value. The information about the message body part structure is returned by the <tt><a href="#argument_Decode_decoded">decoded</a></tt> argument, but it just returns the body data part file name instead of the actual body data. It is recommended for retrieving messages larger than the available memory. The names of the body part files are numbers starting from '1'.</p>
<p> <tt>SkipBody</tt></p>
<p> If this parameter is specified, the message body parts are skipped. This means the information about the message body part structure is returned by the <tt><a href="#argument_Decode_decoded">decoded</a></tt> but it does not return any body data. It is recommended just for parsing messages without the need to retrieve the message body part data.</p>
<p><tt><b><a name="argument_Decode_decoded">decoded</a></b></tt> - Retrieve the structure of the parsed message headers and body data.</p>
<p> The argument is used to return by reference an array of message structure definitions. Each array entry refers to the structure of each message that is found and parsed successfully.</p>
<p> Each message entry consists of an associative array with several entries that describe the message structure. Here follows the list of message structure entries names and the meaning of the respective values:</p>
<p> <tt>Headers</tt></p>
<p> Associative array that returns the list of all the message headers. The array entries are the header names mapped to lower case, including the end colon. The array values are the respective header raw values without any start or trailing white spaces. Long header values split between multiple message lines are gathered in single string without line breaks. If an header with the same name appears more than once in the message, the respective value is an array with the values of all of the header occurrences.</p>
<p> <tt>DecodedHeaders</tt></p>
<p> Associative array that returns the list of all the encoded message headers when the <tt><a href="#variable_decode_headers">decode_headers</a></tt> variable is set. The array entries are the header names mapped to lower case, including the end colon. The array values are also arrays that list only the occurrences of the header that originally were encoded. Each entry of the decoded header array contains more associative arrays that describe each part of the decoded header. Each of those associative arrays have an entry named <tt>Value</tt> that contains the decoded header part value, and another entry named <tt>Encoding</tt> that specifies the character set encoding of the value in upper case.</p>
<p> <tt>ExtractedAddresses</tt></p>
<p> If the <tt><a href="#variable_extract_addresses">extract_addresses</a></tt> variable is set to 1, this entry is set to an associative array with the addresses found in the headers specified by the <tt><a href="#variable_address_headers">address_headers</a></tt> variable.</p>
<p> The parsed addresses found on each header are returned as an array with the format of the <a href="rfc822_addresses_class.html#argument_ParseAddressList_addresses">addresses</a> argument of the <a href="rfc822_addresses_class.html#function_ParseAddressList">ParseAddressList</a> function of the <a href="rfc822_addresses_class.html">RFC 822 addresses</a> class.</p>
<p> <tt>Parts</tt></p>
<p> If this message content type is multipart, this entry is an array that describes each of the parts contained in the message body. Each message part is described by an associative array with the same structure of a complete message definition.</p>
<p> <tt>Body</tt></p>
<p> String with the decoded data contained in the message body. If the <tt>SaveBody</tt> or <tt>SkipBody</tt> parameters are defined, the <tt>Body</tt> entry is not set.</p>
<p> <tt>BodyFile</tt></p>
<p> Name of the file to which the message body data was saved when the <tt>SaveBody</tt> parameter is defined.</p>
<p> <tt>BodyLength</tt></p>
<p> Length of the current decoded body part.</p>
<p> <tt>BodyPart</tt></p>
<p> Number of the current message body part.</p>
<p> <tt>FileName</tt></p>
<p> Name of the file for body parts composed from files.</p>
<p> <tt>FileNameCharacterSet</tt></p>
<p> Character set encoding for file parts with names that may include non-ASCII characters.</p>
<p> <tt>FileNameLanguage</tt></p>
<p> Language of file parts with names that may include non-ASCII characters.</p>
<p> <tt>FileDisposition</tt></p>
<p> Disposition of parts that files. It may be either <tt>'inline'</tt> for file parts to be displayed with the message, or <tt>'attachment'</tt> otherwise.</p>
</ul>
<h3>Return value</h3>
<p>This function returns 1 if the specified message data is parsed successfully. Otherwise, check the variables <tt><a href="#variable_error">error</a></tt> and <tt><a href="#variable_error_position">error_position</a></tt> to determine what error occurred and the relevant message position.</p>
<p><a href="#functions">Functions</a></p>
<h3><a name="function_Analyze"></a><li><a name="9.2.4">Analyze</a></li></h3>
<h3>Synopsis</h3>
<p><tt><i>bool</i> Analyze(</tt><ul>
<tt>(input and output) <i>array</i> </tt><tt><a href="#argument_Analyze_message">message</a></tt><tt>,</tt><br />
<tt>(output) <i>array</i> </tt><tt><a href="#argument_Analyze_results">results</a></tt></ul>
<tt>)</tt></p>
<h3>Purpose</h3>
<p>Analyze a parsed message to describe its contents.</p>
<h3>Usage</h3>
<p>Pass an array to the <tt><a href="#argument_Analyze_message">message</a></tt> parameter with the decoded message array structure returned by the <tt><a href="#function_Decode">Decode</a></tt> function. The <tt><a href="#argument_Analyze_results">results</a></tt> returns details about the type of message that was analyzed and its contents.</p>
<h3>Arguments</h3>
<ul>
<p><tt><b><a name="argument_Analyze_message">message</a></b></tt> - Pass an associative array with the definition of an individual message returned by the <tt><a href="#argument_Decode_decoded">decoded</a></tt> argument of the <tt><a href="#function_Decode">Decode</a></tt> function..</p>
<p><tt><b><a name="argument_Analyze_results">results</a></b></tt> - Returns an associative array with the results of the analysis. Some types of entries are returned for all types of analyzed messages. Other entries are specific to each type of message.</p>
<p> <tt>Type</tt></p>
<p> Type of message that was analyzed. Currently it supports the types: <tt>binary</tt>, <tt>text</tt>, <tt>html</tt>, <tt>video</tt>, <tt>image</tt>, <tt>audio</tt>, <tt>zip</tt>, <tt>pdf</tt>, <tt>postscript</tt>, <tt>ms-word</tt>, <tt>ms-excel</tt>, <tt>ms-powerpoint</tt>, <tt>ms-tnef</tt>, <tt>odf-writer</tt>, <tt>signature</tt>, <tt>report-type</tt>, <tt>delivery-status</tt> and <tt>message</tt>.</p>
<p> <tt>SubType</tt></p>
<p> Name of the variant of the message type format.</p>
<p> <tt>Description</tt></p>
<p> Human readable description in English of the message type.</p>
<p> </p>
<p> </p>
<p> </p>
<p> <b>From message headers:</b></p>
<p> <tt>Encoding</tt></p>
<p> Character set encoding of the message part.</p>
<p> <tt>Subject</tt></p>
<p> The message subject.</p>
<p> <tt>SubjectEncoding</tt></p>
<p> Character set encoding of the message subject.</p>
<p> <tt>Date</tt></p>
<p> The message date.</p>
<p> <tt>From</tt></p>
<p> <tt>To</tt></p>
<p> <tt>Cc</tt></p>
<p> <tt>Bcc</tt></p>
<p> Array of e-mail addresses found in the <tt>From</tt>, <tt>To</tt>, <tt>Cc</tt>, <tt>Bcc</tt>.</p>
<p> Each of the entries consists of an associative array with an entry named <tt>address</tt> with the e-mail address and optionally another named <tt>name</tt> with the associated name.</p>
<p> </p>
<p> </p>
<p> <b>For content message parts:</b></p>
<p> </p>
<p> <tt>Data</tt></p>
<p> String of data of the message part.</p>
<p> <tt>DataFile</tt></p>
<p> File with data of the message part.</p>
<p> <tt>DataLength</tt></p>
<p> Length of the data of the message part.</p>
<p> </p>
<p> </p>
<p> </p>
<p> <b>For message with embedded files:</b></p>
<p> </p>
<p> <tt>FileName</tt></p>
<p> Original name of the file.</p>
<p> <tt>ContentID</tt></p>
<p> Content identifier of the file to be used in references from other message parts.</p>
<p> For instance, an HTML message may reference images embedded in the message using URLs that start with the 'cid:' followed by the content identifier of the embedded image file part.</p>
<p> <tt>Disposition</tt></p>
<p> Information of whether the embedded file should be displayed inline when the message is presented, or it is an attachment file.</p>
<p> </p>
<p> </p>
<p> <b>For composite message:</b></p>
<p> </p>
<p> <tt>Attachments</tt></p>
<p> List of files attached to the message.</p>
<p> <tt>Alternative</tt></p>
<p> List of alternative message parts that can be displayed if the main message type is not supported by the program displaying the message.</p>
<p> <tt>Related</tt></p>
<p> List of message parts related with the main message type.</p>
<p> It may list for instance embedded images or CSS files related with an HTML message type.</p>
<p> </p>
<p> </p>
<p> <b>For bounced messages or other types of delivery status report messages:</b></p>
<p> </p>
<p> <tt>Recipients</tt></p>
<p> List of recipients of the original message.</p>
<p> Each entry contains an associative array that may have the entries: <tt>Recipient</tt> with the original recipient address, <tt>Action</tt> with the name action that triggered the delivery status report, <tt>Status</tt> with the code of the status of the message delivery.</p>
<p> <tt>Response</tt></p>
<p> Human readable response sent by the server the originated the report.</p>
<p> </p>
</ul>
<h3>Return value</h3>
<p>This function returns 1 if the specified message is analyzed successfully. Otherwise, check the variables <tt><a href="#variable_error">error</a></tt> and <tt><a href="#variable_error_position">error_position</a></tt> to determine what error occurred.</p>
<p><a href="#functions">Functions</a></p>
<p><a href="#table_of_contents">Table of contents</a></p>
</ul>
</ul>

<hr />
<address>Manuel Lemos (<a href="mailto:mlemos-at-acm.org">mlemos-at-acm.org</a>)</address>
</body>
</html>
Return current item: MIME E-mail message parser