\Horde_Mime_Part

Object-oriented representation of a MIME part (RFC 2045-2049).

Summary

Methods
Properties
Constants
__construct()
__clone()
setDisposition()
getDisposition()
setDispositionParameter()
getDispositionParameter()
getAllDispositionParameters()
setName()
getName()
setContents()
appendContents()
clearContents()
getContents()
setType()
getType()
getDefaultType()
getPrimaryType()
getSubType()
setCharset()
getCharset()
setHeaderCharset()
getHeaderCharset()
setLanguage()
getLanguage()
setDuration()
getDuration()
setDescription()
getDescription()
setTransferEncoding()
getParts()
setContentTypeParameter()
getContentTypeParameter()
getAllContentTypeParameters()
setEOL()
getEOL()
addMimeHeaders()
toString()
replaceEOL()
getBytes()
setBytes()
getSize()
setContentId()
getContentId()
setMimeId()
getMimeId()
buildMimeIds()
isBasePart()
isAttachment()
setMetadata()
getMetadata()
send()
findBody()
partIterator()
getPartByIndex()
parseMessage()
getRawPartText()
offsetExists()
offsetGet()
offsetSet()
offsetUnset()
count()
current()
key()
next()
rewind()
valid()
hasChildren()
getChildren()
serialize()
unserialize()
clearContentTypeParameter()
contentTypeMap()
addPart()
getPart()
alterPart()
removePart()
$defaultCharset
$memoryLimit
$parent
$encodingTypes
$mimeTypes
VERSION
EOL
RFC_EOL
DEFAULT_ENCODING
ENCODE_7BIT
ENCODE_8BIT
ENCODE_BINARY
NESTING_LIMIT
STATUS_REINDEX
STATUS_BASEPART
UNKNOWN
_transferDecode()
_transferEncode()
_getTransferEncoding()
_reindex()
_writeStream()
_readStream()
_scanStream()
_getStructure()
_findHeader()
_findBoundary()
_sanityCheckRfc822Attachments()
_reEncodeMessageAttachment()
$_bytes
$_contents
$_eol
$_headers
$_hdrCharset
$_metadata
$_mimeid
$_parts
$_status
$_temp
$_transferEncoding
$_failed
N/A
No private methods found
No private properties found
N/A

Constants

VERSION

VERSION

EOL

EOL

RFC_EOL

RFC_EOL

DEFAULT_ENCODING

DEFAULT_ENCODING

ENCODE_7BIT

ENCODE_7BIT

ENCODE_8BIT

ENCODE_8BIT

ENCODE_BINARY

ENCODE_BINARY

NESTING_LIMIT

NESTING_LIMIT

STATUS_REINDEX

STATUS_REINDEX

STATUS_BASEPART

STATUS_BASEPART

UNKNOWN

UNKNOWN

Properties

$defaultCharset

$defaultCharset : string

The default charset to use when parsing text parts with no charset information.

Type

string

$memoryLimit

$memoryLimit : integer

The memory limit for use with the PHP temp stream.

Type

integer

$parent

$parent : \Horde_Mime_Part

Parent object. Value only accurate when iterating.

Type

\Horde_Mime_Part

$encodingTypes

$encodingTypes : 

Type

$mimeTypes

$mimeTypes : 

Type

$_bytes

$_bytes : integer

Default value for this Part's size.

Type

integer

$_contents

$_contents : resource

The body of the part. Always stored in binary format.

Type

resource

$_eol

$_eol : string

The sequence to use as EOL for this part.

The default is currently to output the EOL sequence internally as just "\n" instead of the canonical "\r\n" required in RFC 822 & 2045. To be RFC complaint, the full EOL combination should be used when sending a message.

Type

string

$_headers

$_headers : \Horde_Mime_Headers

The MIME headers for this part.

Type

\Horde_Mime_Headers

$_hdrCharset

$_hdrCharset : string

The charset to output the headers in.

Type

string

$_metadata

$_metadata : array

Metadata.

Type

array

$_mimeid

$_mimeid : string

The MIME ID of this part.

Type

string

$_parts

$_parts : array

The subparts of this part.

Type

array

$_status

$_status : integer

Status mask for this part.

Type

integer

$_temp

$_temp : array

Temporary array.

Type

array

$_transferEncoding

$_transferEncoding : string

The desired transfer encoding of this part.

Type

string

$_failed

$_failed : boolean

Flag to detect if a message failed to send at least once.

Type

boolean

Methods

__construct()

__construct() 

Constructor.

__clone()

__clone() 

Function to run on clone.

setDisposition()

setDisposition(string  $disposition = null) 

Set the content-disposition of this part.

Parameters

string $disposition

The content-disposition to set ('inline', 'attachment', or an empty value).

getDisposition()

getDisposition() : string

Get the content-disposition of this part.

Returns

string —

The part's content-disposition. An empty string means no desired disposition has been set for this part.

setDispositionParameter()

setDispositionParameter(string  $label, string  $data) 

Add a disposition parameter to this part.

Parameters

string $label

The disposition parameter label.

string $data

The disposition parameter data. If null, removes the parameter (@since 2.8.0).

getDispositionParameter()

getDispositionParameter(string  $label) : string

Get a disposition parameter from this part.

Parameters

string $label

The disposition parameter label.

Returns

string —

The data requested. Returns null if $label is not set.

getAllDispositionParameters()

getAllDispositionParameters() : array

Get all parameters from the Content-Disposition header.

Returns

array —

An array of all the parameters Returns the empty array if no parameters set.

setName()

setName(string  $name) 

Set the name of this part.

Parameters

string $name

The name to set.

getName()

getName(boolean  $default = false) : string

Get the name of this part.

Parameters

boolean $default

If the name parameter doesn't exist, should we use the default name from the description parameter?

Returns

string —

The name of the part.

setContents()

setContents(mixed  $contents, array  $options = array()) 

Set the body contents of this part.

Parameters

mixed $contents

The part body. Either a string or a stream resource, or an array containing both.

array $options

Additional options:

  • encoding: (string) The encoding of $contents. DEFAULT: Current transfer encoding value.
  • usestream: (boolean) If $contents is a stream, should we directly use that stream? DEFAULT: $contents copied to a new stream.

appendContents()

appendContents(mixed  $contents,   $options = array()) 

Add to the body contents of this part.

Parameters

mixed $contents

The part body. Either a string or a stream resource, or an array containing both.

  • encoding: (string) The encoding of $contents. DEFAULT: Current transfer encoding value.
  • usestream: (boolean) If $contents is a stream, should we directly use that stream? DEFAULT: $contents copied to a new stream.
$options

clearContents()

clearContents() 

Clears the body contents of this part.

getContents()

getContents(array  $options = array()) : mixed

Return the body of the part.

Parameters

array $options

Additional options:

  • canonical: (boolean) Returns the contents in strict RFC 822 & 2045 output - namely, all newlines end with the canonical sequence. DEFAULT: No
  • stream: (boolean) Return the body as a stream resource. DEFAULT: No

Returns

mixed —

The body text (string) of the part, null if there is no contents, and a stream resource if 'stream' is true.

setType()

setType(string  $type) 

Set the MIME type of this part.

Parameters

string $type

The MIME type to set (ex.: text/plain).

getType()

getType(boolean  $charset = false) : string

Get the full MIME Content-Type of this part.

Parameters

boolean $charset

Append character set information to the end of the content type if this is a text/* part? `

Returns

string —

The MIME type of this part.

getDefaultType()

getDefaultType() : string

If the subtype of a MIME part is unrecognized by an application, the default type should be used instead (See RFC 2046). This method returns the default subtype for a particular primary MIME type.

Returns

string —

The default MIME type of this part (ex.: text/plain).

getPrimaryType()

getPrimaryType() : string

Get the primary type of this part.

Returns

string —

The primary MIME type of this part.

getSubType()

getSubType() : string

Get the subtype of this part.

Returns

string —

The MIME subtype of this part.

setCharset()

setCharset(string  $charset) 

Set the character set of this part.

Parameters

string $charset

The character set of this part.

getCharset()

getCharset() : string

Get the character set to use for this part.

Returns

string —

The character set of this part (lowercase). Returns null if there is no character set.

setHeaderCharset()

setHeaderCharset(string  $charset) 

Set the character set to use when outputting MIME headers.

Parameters

string $charset

The character set.

getHeaderCharset()

getHeaderCharset() : string

Get the character set to use when outputting MIME headers.

Returns

string —

The character set. If no preferred character set has been set, returns null.

setLanguage()

setLanguage(mixed  $lang) 

Set the language(s) of this part.

Parameters

mixed $lang

A language string, or an array of language strings.

getLanguage()

getLanguage() 

Get the language(s) of this part.

setDuration()

setDuration(integer  $duration) 

Set the content duration of the data contained in this part (see RFC 3803).

Parameters

integer $duration

The duration of the data, in seconds. If null, clears the duration information.

getDuration()

getDuration() : integer

Get the content duration of the data contained in this part (see RFC 3803).

Returns

integer —

The duration of the data, in seconds. Returns null if there is no duration information.

setDescription()

setDescription(string  $description) 

Set the description of this part.

Parameters

string $description

The description of this part. If null, deletes the description (@since 2.8.0).

getDescription()

getDescription(boolean  $default = false) : string

Get the description of this part.

Parameters

boolean $default

If the description parameter doesn't exist, should we use the name of the part?

Returns

string —

The description of this part.

setTransferEncoding()

setTransferEncoding(string  $encoding, array  $options = array()) 

Set the transfer encoding to use for this part.

Only needed in the following circumstances: 1.) Indicate what the transfer encoding is if the data has not yet been set in the object (can only be set if there presently are not any contents). 2.) Force the encoding to a certain type on a toString() call (if 'send' is true).

Parameters

string $encoding

The transfer encoding to use.

array $options

Additional options:

  • send: (boolean) If true, use $encoding as the sending encoding. DEFAULT: $encoding is used to change the base encoding.

getParts()

getParts() : array

Get a list of all MIME subparts.

Returns

array —

An array of the Horde_Mime_Part subparts.

setContentTypeParameter()

setContentTypeParameter(string  $label, string  $data) 

Add/remove a content type parameter to this part.

Parameters

string $label

The content-type parameter label.

string $data

The content-type parameter data. If null, removes the parameter (@since 2.8.0).

getContentTypeParameter()

getContentTypeParameter(string  $label) : string

Get a content type parameter from this part.

Parameters

string $label

The content type parameter label.

Returns

string —

The data requested. Returns null if $label is not set.

getAllContentTypeParameters()

getAllContentTypeParameters() : array

Get all parameters from the Content-Type header.

Returns

array —

An array of all the parameters Returns the empty array if no parameters set.

setEOL()

setEOL(string  $eol) 

Sets a new string to use for EOLs.

Parameters

string $eol

The string to use for EOLs.

getEOL()

getEOL() : string

Get the string to use for EOLs.

Returns

string —

The string to use for EOLs.

addMimeHeaders()

addMimeHeaders(array  $options = array()) : \Horde_Mime_Headers

Returns a Horde_Mime_Header object containing all MIME headers needed for the part.

Parameters

array $options

Additional options:

  • encode: (integer) A mask of allowable encodings. DEFAULT: Auto-determined
  • headers: (Horde_Mime_Headers) The object to add the MIME headers to. DEFAULT: Add headers to a new object

Returns

\Horde_Mime_Headers

A Horde_Mime_Headers object.

toString()

toString(array  $options = array()) : mixed

Return the entire part in MIME format.

Parameters

array $options

Additional options:

  • canonical: (boolean) Returns the encoded part in strict RFC 822 & 2045 output - namely, all newlines end with the canonical sequence. DEFAULT: false
  • defserver: (string) The default server to use when creating the header string. DEFAULT: none
  • encode: (integer) A mask of allowable encodings. DEFAULT: self::ENCODE_7BIT
  • headers: (mixed) Include the MIME headers? If true, create a new headers object. If a Horde_Mime_Headers object, add MIME headers to this object. If a string, use the string verbatim. DEFAULT: true
  • id: (string) Return only this MIME ID part. DEFAULT: Returns the base part.
  • stream: (boolean) Return a stream resource. DEFAULT: false

Returns

mixed —

The MIME string (returned as a resource if $stream is true).

replaceEOL()

replaceEOL(mixed  $text, string  $eol = null, boolean  $stream = false) : string

Replace newlines in this part's contents with those specified by either the given newline sequence or the part's current EOL setting.

Parameters

mixed $text

The text to replace. Either a string or a stream resource. If a stream, and returning a string, will close the stream when done.

string $eol

The EOL sequence to use. If not present, uses the part's current EOL setting.

boolean $stream

If true, returns a stream resource.

Returns

string —

The text with the newlines replaced by the desired newline sequence (returned as a stream resource if $stream is true).

getBytes()

getBytes(boolean  $approx = false) : integer

Determine the size of this MIME part and its child members.

Parameters

boolean $approx

If true, determines an approximate size for parts consisting of base64 encoded data.

Returns

integer —

Size of the part, in bytes.

setBytes()

setBytes(integer  $bytes) 

Explicitly set the size (in bytes) of this part. This value will only be returned (via getBytes()) if there are no contents currently set.

This function is useful for setting the size of the part when the contents of the part are not fully loaded (i.e. creating a Horde_Mime_Part object from IMAP header information without loading the data of the part).

Parameters

integer $bytes

The size of this part in bytes.

getSize()

getSize(boolean  $approx = false) : string

Output the size of this MIME part in KB.

Parameters

boolean $approx

If true, determines an approximate size for parts consisting of base64 encoded data.

Returns

string —

Size of the part in KB.

setContentId()

setContentId(string  $cid = null) : string

Sets the Content-ID header for this part.

Parameters

string $cid

Use this CID (if not already set). Else, generate a random CID.

Returns

string —

The Content-ID for this part.

getContentId()

getContentId() : string

Returns the Content-ID for this part.

Returns

string —

The Content-ID for this part (null if not set).

setMimeId()

setMimeId(string  $mimeid) 

Alter the MIME ID of this part.

Parameters

string $mimeid

The MIME ID.

getMimeId()

getMimeId() : string

Returns the MIME ID of this part.

Returns

string —

The MIME ID.

buildMimeIds()

buildMimeIds(string  $id = null, boolean  $rfc822 = false) 

Build the MIME IDs for this part and all subparts.

Parameters

string $id

The ID of this part.

boolean $rfc822

Is this a message/rfc822 part?

isBasePart()

isBasePart(boolean  $base) 

Is this the base MIME part?

Parameters

boolean $base

True if this is the base MIME part.

isAttachment()

isAttachment() : boolean

Determines if this MIME part is an attachment for display purposes.

Returns

boolean —

True if this part should be considered an attachment.

setMetadata()

setMetadata(string  $key, mixed  $data = null) 

Set a piece of metadata on this object.

Parameters

string $key

The metadata key.

mixed $data

The metadata. If null, clears the key.

getMetadata()

getMetadata(string  $key) : mixed

Retrieves metadata from this object.

Parameters

string $key

The metadata key.

Returns

mixed —

The metadata, or null if it doesn't exist.

send()

send(string  $email, \Horde_Mime_Headers  $headers, \Horde_Mail_Transport  $mailer, array  $opts = array()) 

Sends this message.

Parameters

string $email

The address list to send to.

\Horde_Mime_Headers $headers

The Horde_Mime_Headers object holding this message's headers.

\Horde_Mail_Transport $mailer

A Horde_Mail_Transport object.

array $opts

Additional options:

  - broken_rfc2231: (boolean) Attempt to work around non-RFC
                    2231-compliant MUAs by generating both a RFC
                    2047-like parameter name and also the correct RFC
                    2231 parameter (@since 2.5.0).
                    DEFAULT: false
  - encode: (integer) The encoding to use. A mask of self::ENCODE_*
            values.
            DEFAULT: Auto-determined based on transport driver.

Throws

\Horde_Mime_Exception
\InvalidArgumentException

findBody()

findBody(string  $subtype = null) : mixed

Finds the main "body" text part (if any) in a message.

"Body" data is the first text part under this part.

Parameters

string $subtype

Specifically search for this subtype.

Returns

mixed —

The MIME ID of the main body part, or null if a body part is not found.

partIterator()

partIterator(boolean  $current = true) : \Iterator

Returns the recursive iterator needed to iterate through this part.

Parameters

boolean $current

Include the current part as the base?

Returns

\Iterator —

Recursive iterator.

getPartByIndex()

getPartByIndex(  $index) : \Horde_Mime_Part

Returns a subpart by index.

Parameters

$index

Returns

\Horde_Mime_Part

Part, or null if not found.

parseMessage()

parseMessage(string  $text, array  $opts = array()) : \Horde_Mime_Part

Attempts to build a Horde_Mime_Part object from message text.

Parameters

string $text

The text of the MIME message.

array $opts

Additional options:

  • forcemime: (boolean) If true, the message data is assumed to be MIME data. If not, a MIME-Version header must exist (RFC 2045 [4]) to be parsed as a MIME message. DEFAULT: false
  • level: (integer) Current nesting level of the MIME data. DEFAULT: 0
  • no_body: (boolean) If true, don't set body contents of parts (since 2.2.0). DEFAULT: false

Throws

\Horde_Mime_Exception

Returns

\Horde_Mime_Part

A MIME Part object.

getRawPartText()

getRawPartText(mixed  $text, string  $type, string  $id) : string

Attempts to obtain the raw text of a MIME part.

Parameters

mixed $text

The full text of the MIME message. The text is assumed to be MIME data (no MIME-Version checking is performed). It can be either a stream or a string.

string $type

Either 'header' or 'body'.

string $id

The MIME ID.

Throws

\Horde_Mime_Exception

Returns

string —

The raw text.

offsetExists()

offsetExists(  $offset) 

Parameters

$offset

offsetGet()

offsetGet(  $offset) 

Parameters

$offset

offsetSet()

offsetSet(  $offset,   $value) 

Parameters

$offset
$value

offsetUnset()

offsetUnset(  $offset) 

Parameters

$offset

count()

count() : integer

Returns the number of child message parts (doesn't include grandchildren or more remote ancestors).

Returns

integer —

Number of message parts.

current()

current() 

key()

key() 

next()

next() 

rewind()

rewind() 

valid()

valid() 

hasChildren()

hasChildren() 

getChildren()

getChildren() 

serialize()

serialize() : string

Serialization.

Returns

string —

Serialized data.

unserialize()

unserialize(string  $data) 

Unserialization.

Parameters

string $data

Serialized data.

Throws

\Exception

clearContentTypeParameter()

clearContentTypeParameter(  $label) 

Parameters

$label

contentTypeMap()

contentTypeMap(  $sort = true) 

Parameters

$sort

addPart()

addPart(  $mime_part) 

Parameters

$mime_part

getPart()

getPart(  $id) 

Parameters

$id

alterPart()

alterPart(  $id,   $mime_part) 

Parameters

$id
$mime_part

removePart()

removePart(  $id) 

Parameters

$id

_transferDecode()

_transferDecode(resource  $fp, string  $encoding) : resource

Decodes the contents of the part to binary encoding.

Parameters

resource $fp

A stream containing the data to decode.

string $encoding

The original file encoding.

Returns

resource —

A new file resource with the decoded data.

_transferEncode()

_transferEncode(resource  $fp, string  $encoding) : resource

Encodes the contents of the part as necessary for transport.

Parameters

resource $fp

A stream containing the data to encode.

string $encoding

The encoding to use.

Returns

resource —

A new file resource with the encoded data.

_getTransferEncoding()

_getTransferEncoding(integer  $encode = self::ENCODE_7BIT) : string

Get the transfer encoding for the part based on the user requested transfer encoding and the current contents of the part.

Parameters

integer $encode

A mask of allowable encodings.

Returns

string —

The transfer-encoding of this part.

_reindex()

_reindex(boolean  $force = false) 

Reindexes the MIME IDs, if necessary.

Parameters

boolean $force

Reindex if the current part doesn't have an ID.

_writeStream()

_writeStream(array  $data, array  $options = array()) : resource

Write data to a stream.

Parameters

array $data

The data to write. Either a stream resource or a string.

array $options

Additional options:

  • error: (boolean) Catch errors when writing to the stream. Throw an ErrorException if an error is found. DEFAULT: false
  • filter: (array) Filter(s) to apply to the string. Keys are the filter names, values are filter params.
  • fp: (resource) Use this stream instead of creating a new one.

Throws

\ErrorException

Returns

resource —

The stream resource.

_readStream()

_readStream(resource  $fp, boolean  $close = false) : string

Read data from a stream.

Parameters

resource $fp

An active stream.

boolean $close

Close the stream when done reading?

Returns

string —

The data from the stream.

_scanStream()

_scanStream(resource  $fp) : mixed

Scans a stream for content type.

Parameters

resource $fp

A stream resource.

Returns

mixed —

Either 'binary', '8bit', or false.

_getStructure()

_getStructure(string  $header, string  $body, array  $opts = array()) : \Horde_Mime_Part

Creates a MIME object from the text of one part of a MIME message.

Parameters

string $header

The header text.

string $body

The body text.

array $opts

Additional options:

  - ctype: (string) The default content-type.
  - forcemime: (boolean) If true, the message data is assumed to be
               MIME data. If not, a MIME-Version header must exist to
               be parsed as a MIME message.
  - level: (integer) Current nesting level.
  - no_body: (boolean) If true, don't set body contents of parts.

Returns

\Horde_Mime_Part

The MIME part object.

_findHeader()

_findHeader(string  $text, string  $eol) : integer

Find the location of the end of the header text.

Parameters

string $text

The text to search.

string $eol

The EOL string.

Returns

integer —

Header position.

_findBoundary()

_findBoundary(string  $text, integer  $pos, string  $boundary, integer  $end = null) : array

Find the location of the next boundary string.

Parameters

string $text

The text to search.

integer $pos

The current position in $text.

string $boundary

The boundary string.

integer $end

If set, return after matching this many boundaries.

Returns

array —

Keys are the boundary number, values are an array with two elements: 'start' and 'length'.

_sanityCheckRfc822Attachments()

_sanityCheckRfc822Attachments() 

Re-enocdes message/rfc822 parts in case there was e.g., some broken line length in the headers of the message in the part. Since we shouldn't alter the original message in any way, we simply reset cause the part to be encoded as base64 and sent as a application/octet part.

_reEncodeMessageAttachment()

_reEncodeMessageAttachment(\Horde_Mime_Part  $part) 

Rebuilds $part and forces it to be a base64 encoded application/octet-stream part.

Parameters

\Horde_Mime_Part $part

The MIME part.