Version: @(#) $Id: mime_parser.php,v 1.60 2008/07/17 00:54:24 mlemos Exp $
Manuel Lemos (mlemos-at-acm.org)
Copyright © (C) Manuel Lemos 2006 - 2008
@(#) $Id: mime_parser.php,v 1.60 2008/07/17 00:54:24 mlemos Exp $
Parse MIME encapsulated e-mail message data compliant with the RFC 2822 or aggregated in mbox format.
Use the function Decode 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 SaveBody parameter to make the body parts be saved to files when the message is larger than the available memory. Use the SkipBody parameter to just retrieve the message structure without returning the body data.
If the message data is an archive that may contain multiple messages aggregated in the mbox format, set the variable mbox to 1.
Store the message that is returned when an error occurs.
Check this variable to understand what happened when a call to any of the class functions has failed.
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.
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.
Just set this variable to an empty string to clear the error condition.
Point to the position of the message data or file that refers to the last error that occurred.
Check this variable to determine the relevant position of the message when a parsing error occurs.
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.
Set this variable to 1 if it is it is intended to parse an mbox message archive.
mbox archives may contain multiple messages. Each message starts with the header From. 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.
Specify whether the message headers should be decoded.
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.
Specify whether the message body parts should be decoded.
Set this variable to 1 if it is necessary to parse the message bodies and extract its part structure.
Specify whether the message headers that usually contain e-mail addresses should be parsed and the addresses should be extracted by the Decode function.
Set this variable to 1 if it is necessary to extract the e-mail addresses contained in certain message headers.
The headers to be parsed are defined by the address_headers variable.
Specify which headers contain addresses that should be parsed and extracted.
Change this variable if you need to extract e-mail addresses from a different list of message headers.
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.
By default the class addresses from the headers: 'from:', 'to:', 'cc:', 'bcc:', 'return-path:', 'reply-to:' and 'disposition-notification-to:'.
Maximum length of the chunks of message data that the class parse at one time.
Adjust this value according to the available memory.
Specify whether the class should ignore syntax errors in malformed messages.
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.
Currently the class only ignores some types of syntax errors. Other syntax errors may still cause the Decode to fail.
Return a list of positions of the original message that contain syntax errors.
Check this variable to retrieve eventual message syntax errors that were ignored when the ignore_syntax_errors is set to 1.
The indexes of this array are the positions of the errors. The array values are the corresponding syntax error messages.
Parse and decode message data and retrieve its structure.
Pass an array to the parameters 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 decoded returns the data structure of the parsed messages.
parameters - 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:
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 fopen() function.
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.
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 decoded 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'.
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 decoded 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.
decoded - Retrieve the structure of the parsed message headers and body data.
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.
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:
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.
Associative array that returns the list of all the encoded message headers when the decode_headers 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 Value that contains the decoded header part value, and another entry named Encoding that specifies the character set encoding of the value in upper case.
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.
String with the decoded data contained in the message body. If the SaveBody or SkipBody parameters are defined, the Body entry is not set.
Name of the file to which the message body data was saved when the SaveBody parameter is defined.
Length of the current decoded body part.
Number of the current message body part.
Name of the file for body parts composed from files.
Character set encoding for file parts with names that may include non-ASCII characters.
Language of file parts with names that may include non-ASCII characters.
Disposition of parts that files. It may be either 'inline' for file parts to be displayed with the message, or 'attachment' otherwise.
This function returns 1 if the specified message data is parsed successfully. Otherwise, check the variables error and error_position to determine what error occurred and the relevant message position.
Analyze a parsed message to describe its contents.
Pass an array to the message parameter with the decoded message array structure returned by the Decode function. The results returns details about the type of message that was analyzed and its contents.
results - 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.
Type of message that was analyzed. Currently it supports the types: binary, text, html, video, image, audio, zip, pdf, postscript, ms-word, ms-excel, ms-powerpoint, ms-tnef, odf-writer, signature, report-type, delivery-status and message.
Name of the variant of the message type format.
Human readable description in English of the message type.
From message headers:
Character set encoding of the message part.
The message subject.
Character set encoding of the message subject.
The message date.
Array of e-mail addresses found in the From, To, Cc, Bcc.
Each of the entries consists of an associative array with an entry named address with the e-mail address and optionally another named name with the associated name.
For content message parts:
String of data of the message part.
File with data of the message part.
Length of the data of the message part.
For message with embedded files:
Original name of the file.
Content identifier of the file to be used in references from other message parts.
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.
Information of whether the embedded file should be displayed inline when the message is presented, or it is an attachment file.
For composite message:
List of files attached to the message.
List of alternative message parts that can be displayed if the main message type is not supported by the program displaying the message.
List of message parts related with the main message type.
It may list for instance embedded images or CSS files related with an HTML message type.
For bounced messages or other types of delivery status report messages:
List of recipients of the original message.
Each entry contains an associative array that may have the entries: Recipient with the original recipient address, Action with the name action that triggered the delivery status report, Status with the code of the status of the message delivery.
Human readable response sent by the server the originated the report.