Home » Mail » Mail_mimeDecode » Manual
The Mail_mimeDecode class provides an API to decode mail/MIME messages. This class will parse a raw mime email and return the structure. Returned structure is similar to that returned by imap_fetchstructure().
Mail_mimeDecode::Mail_mimeDecode()
Mail_mimeDecode::Mail_mimeDecode() – constructor
Synopsis
require_once 'Mail/mimeDecode.php';
void Mail_mimeDecode (
string
$input
)
Description
Create a new Mail_mimeDecode object.
Parameter
-
string $input- the input to decode
Note
This function can be called statically.
Mail_mimeDecode::decode()
Mail_mimeDecode::decode() – perform decoding
Synopsis
require_once 'Mail/mimeDecode.php';
object decode (
array $args
= null
)
Description
This function performs the decoding and returns a structure containing the message data.
Parameter
-
array $args- an array with the function arguments-
boolean $args['include_bodies']- whether to include the bodies in the returned structure. -
boolean $args['decode_bodies']- whether to decode the returned bodies. -
boolean $args['decode_headers']- whether to decode the headers (RFC2047). -
string $args['input']- if and only if called statically, this should be used to specify the input to be decoded. -
string $args['crlf']- if and only if called statically, this should be used to specify the line ending type.
-
Return value
object -
-
array $return->headers- an associative array of the headers. The keys of the array are the header names (lowercased) whilst the values are the header values (original case). If there are multiple headers with the same name (eg.Received:) then the value is a numerically indexed array of each of the header values. If the parameterdecode_headersis specified as TRUE, the headers will be decoded according to RFC 2047. -
string $return->ctype_primary- the first part of the content type (ie. before the forward slash). Eg. if the content type ismultipart/mixed, ctype_primary would be"multipart". -
string $return->ctype_secondary- the second part of the content type. Eg. If the content type ismultipart/mixed, ctype_secondary would be"mixed". -
array $return->ctype_parameters- if the content type header has any parameters (eg.boundary="=_hudfhdsalfhds8fy8329hfj") then they will be in this associative array. Keys are the parameter name (eg.boundary) whilst the values are the parameter values (eg. =_hudfhdsalfhds8fy8329hfj). -
string $return->disposition- if the Content-Disposition header is present, its value will be given here. This is usually either"inline"or"attachment". -
array $return->d_parameters- if any parameters are given with the Content-Disposition header, they will be given here in an associative array, keys being the parameter names and values being the parameter values."name"and"filename"are two common examples here. -
array $return->body- if theinclude_bodiesparameter is given when instanciating the class, (either statically or via a concrete instance), then this will be present if the part in question has a body. MIME parts with content typemultipart/*generally do not not have bodies, instead consisting of subparts. If the parameterdecode_bodiesis specified as TRUE then the body will be decoded. -
array $return->parts- if a MIME part consists of subparts, then this array will be present consisting of objects with the same properties as described here.
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | " Called statically and no input given " |
You called the function statically and forgot
to fill $args['input']
|
Fill $args['input'] with the
content to decode or do not call the function
statically.
|
| NULL | every other | See the error message. | The input or parts of the input does not complies to the MIME standard. |
Note
This function can be called statically.
Mail_mimeDecode::uudecode()
Mail_mimeDecode::uudecode() – decode of UU-coded data
Synopsis
require_once 'Mail/mimeDecode.php';
array &uudecode (
string
$input
)
Description
Decodes UU-coded data. 'Unix-to-Unix'-Encoding is used to send binary files (eg. programs, graphics) over 7bit-ASCII-only media, like email.
Parameter
-
string $input- data to decode
Return value
array -
the decoded data
-
string $return[]['filename']- the name of the UUencoded file. -
string $return[]['fileperm']- the file permissions of the UUencoded file, if given. The format is unix-styled, ie."0666"or"666". -
string $return[]['filedata']- the decoded content of the UUencoded file.
Note
This function can be called statically.
Mail_mimeDecode::getXML()
Mail_mimeDecode::getXML() – create XML representation of MIME parts
Synopsis
require_once 'Mail/mimeDecode.php';
string getXML (
array $decoded
)
Description
getXML() converts the returned array from decode () into a valid XML document.
Parameter
-
array $decode- the array from decode().
Return value
string -
the XML document
Note
This function can be called statically.
Example
Create a XML representation
<?php
...
$output = $obj->decode();
$xml = Mail_mimeDecode::getXML($output);
...
?>
Mail_mimeDecode - Example
Mail_mimeDecode - Example – decode an email
Example
Decode an email
<?php
require_once 'Mail/mimeDecode.php';
...
$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$decoder = new Mail_mimeDecode($input);
$structure = $decoder->decode($params);
?>
This example calls the decode function statically (ie no object, straight function call) and then passes the structure to the getXML() function.
<?php
...
$params['include_bodies'] = true;
$params['decode_bodies'] = false;
$params['decode_headers'] = true;
$params['input'] = $input;
$params['crlf'] = "\r\n";
$structure = Mail_mimeDecode::decode($params);
$xml = Mail_mimeDecode::getXML($structure);
?>