MailMimeParser 2.2

MultiPart extends MessagePart
in package
implements IMultiPart

A message part that contains children.

Tags
author

Zaahid Bateson

Interfaces, Classes and Traits

IMultiPart
An interface representing a message part that contains children.

Table of Contents

$charsetOverride  : string
$ignoreTransferEncoding  : bool
$observers  : SplObjectStorage
$parent  : IMimePart
$partChildrenContainer  : PartChildrenContainer
$partStreamContainer  : PartStreamContainer
__construct()  : mixed
__toString()  : string
Returns the message/part as a string, containing its headers, content and children.
addChild()  : mixed
Registers the passed part as a child of the current part.
attach()  : mixed
attachContentStream()  : mixed
Attaches the stream or resource handle for the part's content. The stream is closed when another stream is attached, or the MimePart is destroyed.
detach()  : mixed
detachContentStream()  : mixed
Detaches the content stream.
getAllParts()  : array<string|int, IMessagePart>
Returns the current part, all child parts, and child parts of all children optionally filtering them with the provided PartFilter.
getAllPartsByMimeType()  : array<string|int, IMessagePart>
Returns an array of all parts that have a content type matching the passed mime type.
getBinaryContentResourceHandle()  : resource|null
Returns a resource handle for the content's raw data stream, or null if the part doesn't have a content stream.
getBinaryContentStream()  : StreamInterface|null
Returns the raw data stream for the current part, if it exists, or null if there's no content associated with the stream.
getChild()  : IMessagePart|null
Returns the direct child at the given 0-based index and optional filter, or null if none exist or do not match.
getChildCount()  : int
Returns the number of direct children under this part (optionally counting only filtered items if a callable filter is passed).
getChildIterator()  : RecursiveIterator
Returns a \RecursiveIterator of child parts.
getChildParts()  : array<string|int, IMessagePart>
Returns an array of all direct child parts, optionally filtering them with a passed callable.
getContent()  : string|null
Shortcut to reading stream content and assigning it to a string. Returns null if the part doesn't have a content stream.
getContentStream()  : StreamInterface|null
Returns the StreamInterface for the part's content or null if the part doesn't have a content section.
getCountOfPartsByMimeType()  : int
Returns the number of parts that have content types matching the passed mime type.
getFilename()  : string|null
Returns a filename for the part if one is defined, or null otherwise.
getParent()  : IMimePart
Returns this part's parent.
getPart()  : IMessagePart|null
Returns the part at the given 0-based index for this part (part 0) and all parts under it, or null if not found with the passed filter function.
getPartByContentId()  : IMessagePart|null
Returns a part that has the given Content ID, or null if not found.
getPartByMimeType()  : IMessagePart|null
Returns the part that has a content type matching the passed mime type at the given index, or null if there are no matching parts.
getPartCount()  : int
Returns the total number of parts in this and all children.
getResourceHandle()  : resource
Returns a resource handle for the string representation of this part, containing its headers, content and children. For an IMessage, this would be the entire RFC822 (or greater) email.
getStream()  : StreamInterface
Returns a Psr7 StreamInterface for the string representation of this part, containing its headers, content and children.
hasContent()  : bool
Returns true if the part contains a 'body' (content).
notify()  : mixed
removeAllParts()  : int
Removes all parts below the current part. If a callable filter is passed, removes only those matching the passed filter. The number of removed parts is returned.
removePart()  : int|null
Removes the child part from this part and returns its previous position or null if it wasn't found.
save()  : mixed
Writes a string representation of this part, including its headers, content and children to the passed file, resource, or stream.
saveContent()  : mixed
Saves the binary content of the stream to the passed file, resource or stream.
setCharsetOverride()  : mixed
Overrides the default character set used for reading content from content streams in cases where a user knows the source charset is not what is specified.
setContent()  : mixed
Sets the content of the part to the passed string, resource, or stream.
getAllPartsIterator()  : mixed
iteratorFindAt()  : mixed

Properties

$charsetOverride

protected string $charsetOverride

can be used to set an override for content's charset in cases where a user knows the charset on the content is not what it claims to be.

$ignoreTransferEncoding

protected bool $ignoreTransferEncoding

set to true when a user attaches a stream manually, it's assumed to already be decoded or to have relevant transfer encoding decorators attached already.

$observers

protected SplObjectStorage $observers

attached observers that need to be notified of modifications to this part.

Methods

__toString()

Returns the message/part as a string, containing its headers, content and children.

public __toString() : string

Convenience method for calling getContents() on IMessagePart::getStream().

Return values
string

addChild()

Registers the passed part as a child of the current part.

public addChild(IMessagePart $part[, mixed $position = null ]) : mixed

If the $position parameter is non-null, adds the part at the passed position index, otherwise adds it as the last child.

Parameters
$part : IMessagePart

The part to add.

$position : mixed = null

Optional insertion position 0-based index.

Return values
mixed

attach()

public attach(SplObserver $observer) : mixed
Parameters
$observer : SplObserver
Return values
mixed

attachContentStream()

Attaches the stream or resource handle for the part's content. The stream is closed when another stream is attached, or the MimePart is destroyed.

public attachContentStream(StreamInterface $stream[, mixed $streamCharset = MailMimeParser::DEFAULT_CHARSET ]) : mixed
Parameters
$stream : StreamInterface

the content

$streamCharset : mixed = MailMimeParser::DEFAULT_CHARSET

the charset of $stream

Return values
mixed

detach()

public detach(SplObserver $observer) : mixed
Parameters
$observer : SplObserver
Return values
mixed

detachContentStream()

Detaches the content stream.

public detachContentStream() : mixed
Return values
mixed

getAllParts()

Returns the current part, all child parts, and child parts of all children optionally filtering them with the provided PartFilter.

public getAllParts([mixed $fnFilter = null ]) : array<string|int, IMessagePart>

Note that the first part returned is the current part itself. This is often desirable for queries with a passed filter, e.g. looking for an IMessagePart with a specific Content-Type that may be satisfied by the current part.

The passed callable must accept an IMessagePart as an argument, and return true if it should be accepted, or false to filter the part out. Some default filters are provided by static functions returning callables in PartFilter.

Parameters
$fnFilter : mixed = null

Optional function accepting an IMessagePart and returning true if the part should be included.

Return values
array<string|int, IMessagePart>

An array of matching parts.

getAllPartsByMimeType()

Returns an array of all parts that have a content type matching the passed mime type.

public getAllPartsByMimeType(mixed $mimeType) : array<string|int, IMessagePart>

Creates a filter that looks at the return value of IMessagePart::getContentType() for all parts (including the current part), returning an array of matching parts.

Parameters
$mimeType : mixed

The mime type to find.

Return values
array<string|int, IMessagePart>

An array of matching parts.

getBinaryContentResourceHandle()

Returns a resource handle for the content's raw data stream, or null if the part doesn't have a content stream.

public getBinaryContentResourceHandle() : resource|null

The method wraps a call to IMessagePart::getBinaryContentStream() and returns a resource handle for the returned Stream.

Return values
resource|null

the resource

getBinaryContentStream()

Returns the raw data stream for the current part, if it exists, or null if there's no content associated with the stream.

public getBinaryContentStream() : StreamInterface|null

This is basically the same as calling IMessagePart::getContentStream(), except no automatic charset conversion is done. Note that for non-text streams, this doesn't have an effect, as charset conversion is not performed in that case, and is useful only when:

  • The charset defined is not correct, and the conversion produces errors; or
  • You'd like to read the raw contents without conversion, for instance to save it to file or allow a user to download it as-is (in a download link for example).
Return values
StreamInterface|null

the stream

getChild()

Returns the direct child at the given 0-based index and optional filter, or null if none exist or do not match.

public getChild(mixed $index[, mixed $fnFilter = null ]) : IMessagePart|null

The passed callable must accept an IMessagePart as an argument, and return true if it should be accepted, or false to filter the part out. Some default filters are provided by static functions returning callables in PartFilter.

Parameters
$index : mixed

0-based index

$fnFilter : mixed = null

Optional function accepting an IMessagePart and returning true if the part should be included.

Return values
IMessagePart|null

The matching direct child part or null if not found.

getChildCount()

Returns the number of direct children under this part (optionally counting only filtered items if a callable filter is passed).

public getChildCount([mixed $fnFilter = null ]) : int

The passed callable must accept an IMessagePart as an argument, and return true if it should be accepted, or false to filter the part out. Some default filters are provided by static functions returning callables in PartFilter.

Parameters
$fnFilter : mixed = null

Optional function accepting an IMessagePart and returning true if the part should be included.

Return values
int

The number of children, or number of children matching the the passed filtering callable.

getChildParts()

Returns an array of all direct child parts, optionally filtering them with a passed callable.

public getChildParts([mixed $fnFilter = null ]) : array<string|int, IMessagePart>

The passed callable must accept an IMessagePart as an argument, and return true if it should be accepted, or false to filter the part out. Some default filters are provided by static functions returning callables in PartFilter.

Parameters
$fnFilter : mixed = null

Optional function accepting an IMessagePart and returning true if the part should be included.

Return values
array<string|int, IMessagePart>

An array of matching child parts.

getContent()

Shortcut to reading stream content and assigning it to a string. Returns null if the part doesn't have a content stream.

public getContent([mixed $charset = MailMimeParser::DEFAULT_CHARSET ]) : string|null

The returned string is encoded to the passed $charset character encoding.

Parameters
$charset : mixed = MailMimeParser::DEFAULT_CHARSET

the target charset for the returned string

Return values
string|null

the content

getContentStream()

Returns the StreamInterface for the part's content or null if the part doesn't have a content section.

public getContentStream([mixed $charset = MailMimeParser::DEFAULT_CHARSET ]) : StreamInterface|null

To get a stream without charset conversion if you know the part's content contains a binary stream, call self::getBinaryContentStream() instead.

The library automatically handles decoding and charset conversion (to the target passed $charset) based on the part's transfer encoding as returned by IMessagePart::getContentTransferEncoding() and the part's charset as returned by IMessagePart::getCharset(). The returned stream is ready to be read from directly.

Note that the returned Stream is a shared object. If called multiple times with the same $charset, and the value of the part's Content-Transfer-Encoding header has not changed, the stream will be rewound. This would affect other existing variables referencing the stream, for example:

// assuming $part is a part containing the following
// string for its content: '12345678'
$stream = $part->getContentStream();
$someChars = $part->read(4);

$stream2 = $part->getContentStream();
$moreChars = $part->read(4);
echo ($someChars === $moreChars);    //1

In this case the Stream was rewound, and $stream's second call to read 4 bytes reads the same first 4.

Parameters
$charset : mixed = MailMimeParser::DEFAULT_CHARSET

Optional charset for the returned stream.

Return values
StreamInterface|null

the stream

getCountOfPartsByMimeType()

Returns the number of parts that have content types matching the passed mime type.

public getCountOfPartsByMimeType(mixed $mimeType) : int
Parameters
$mimeType : mixed

The mime type to find.

Return values
int

The number of matching parts.

getFilename()

Returns a filename for the part if one is defined, or null otherwise.

public getFilename() : string|null
Return values
string|null

the file name

getPart()

Returns the part at the given 0-based index for this part (part 0) and all parts under it, or null if not found with the passed filter function.

public getPart(mixed $index[, mixed $fnFilter = null ]) : IMessagePart|null

Note that the first part returned is the current part itself. This is usually desirable for queries with a passed filter, e.g. looking for an part with a specific Content-Type that may be satisfied by the current part.

The passed callable must accept an IMessagePart as an argument, and return true if it should be accepted, or false to filter the part out. Some default filters are provided by static functions returning callables in PartFilter.

Parameters
$index : mixed

The 0-based index (0 being this part if $fnFilter is null or this part is satisfied by the filter).

$fnFilter : mixed = null

Optional function accepting an IMessagePart and returning true if the part should be included.

Return values
IMessagePart|null

A matching part, or null if not found.

getPartByContentId()

Returns a part that has the given Content ID, or null if not found.

public getPartByContentId(mixed $contentId) : IMessagePart|null

Calls IMessagePart::getContentId() to find a matching part.

Parameters
$contentId : mixed

The content ID to find a part for.

Return values
IMessagePart|null

The matching part.

getPartByMimeType()

Returns the part that has a content type matching the passed mime type at the given index, or null if there are no matching parts.

public getPartByMimeType(mixed $mimeType, mixed $index) : IMessagePart|null

Creates a filter that looks at the return value of IMessagePart::getContentType() for all parts (including the current part) and returns a matching one at the given 0-based index.

Parameters
$mimeType : mixed

The mime type to find.

$index : mixed

Optional 0-based index (defaulting to '0').

Return values
IMessagePart|null

The part.

getPartCount()

Returns the total number of parts in this and all children.

public getPartCount([mixed $fnFilter = null ]) : int

Note that the current part is considered, so the minimum getPartCount is 1 without a filter.

The passed callable must accept an IMessagePart as an argument, and return true if it should be accepted, or false to filter the part out. Some default filters are provided by static functions returning callables in PartFilter.

Parameters
$fnFilter : mixed = null

Optional function accepting an IMessagePart and returning true if the part should be included.

Return values
int

The number of matching parts.

getResourceHandle()

Returns a resource handle for the string representation of this part, containing its headers, content and children. For an IMessage, this would be the entire RFC822 (or greater) email.

public getResourceHandle() : resource

If the part has not been modified and represents a parsed part, the original stream should be returned. Otherwise a stream representation of the part including its modifications should be returned. This insures that an unmodified, signed message could be passed on that way even after parsing and reading.

The returned stream is not guaranteed to be RFC822 (or greater) compliant for the following reasons:

  • The original email or part, if not modified, is returned as-is and may not be compliant.
  • Although certain parts may have been modified, an original unmodified header from the original email or part may not be compliant.
  • A user may set headers in a non-compliant format.
Return values
resource

the resource handle containing the part.

getStream()

Returns a Psr7 StreamInterface for the string representation of this part, containing its headers, content and children.

public getStream() : StreamInterface

If the part has not been modified and represents a parsed part, the original stream should be returned. Otherwise a stream representation of the part including its modifications should be returned. This insures that an unmodified, signed message could be passed on that way even after parsing and reading.

The returned stream is not guaranteed to be RFC822 (or greater) compliant for the following reasons:

  • The original email or part, if not modified, is returned as-is and may not be compliant.
  • Although certain parts may have been modified, an original unmodified header from the original email or part may not be compliant.
  • A user may set headers in a non-compliant format.
Return values
StreamInterface

the stream containing the part.

hasContent()

Returns true if the part contains a 'body' (content).

public hasContent() : bool
Return values
bool

notify()

public notify() : mixed
Return values
mixed

removeAllParts()

Removes all parts below the current part. If a callable filter is passed, removes only those matching the passed filter. The number of removed parts is returned.

public removeAllParts([mixed $fnFilter = null ]) : int

Note: the current part will not be removed. Although the function naming matches getAllParts, which returns the current part, it also doesn't only remove direct children like getChildParts. Internally this function uses getAllParts but the current part is filtered out if returned.

Parameters
$fnFilter : mixed = null

Optional function accepting an IMessagePart and returning true if the part should be included.

Return values
int

The number of removed parts.

removePart()

Removes the child part from this part and returns its previous position or null if it wasn't found.

public removePart(IMessagePart $part) : int|null

Note that if the part is not a direct child of this part, the returned position is its index within its parent (calls removePart on its direct parent).

This also means that parts from unrelated parts/messages could be removed by a call to removePart -- it will always remove the part from its parent if it has one, essentially calling php $part->getParent()->removePart(); .

Parameters
$part : IMessagePart

The part to remove

Return values
int|null

The previous index position of the part within its old parent.

save()

Writes a string representation of this part, including its headers, content and children to the passed file, resource, or stream.

public save(mixed $filenameResourceOrStream[, mixed $filemode = 'w+' ]) : mixed

If the part has not been modified and represents a parsed part, the original stream should be written to the file. Otherwise a stream representation of the part including its modifications should be written. This insures that an unmodified, signed message could be passed on this way even after parsing and reading.

The written stream is not guaranteed to be RFC822 (or greater) compliant for the following reasons:

  • The original email or part, if not modified, is returned as-is and may not be compliant.
  • Although certain parts may have been modified, an original unmodified header from the original email or part may not be compliant.
  • A user may set headers in a non-compliant format.

If the passed $filenameResourceOrStream is a string, it's assumed to be a filename to write to.

When passing a resource or Psr7 Stream, the resource is not closed, nor rewound after being written to.

Parameters
$filenameResourceOrStream : mixed

the file, resource, or stream to write to.

$filemode : mixed = 'w+'

Optional filemode to open a file in (if $filenameResourceOrStream is a string)

Return values
mixed

saveContent()

Saves the binary content of the stream to the passed file, resource or stream.

public saveContent(mixed $filenameResourceOrStream) : mixed

Note that charset conversion is not performed in this case, and the contents of the part are saved in their binary format as transmitted (but after any content-transfer decoding is performed). IMessagePart::getBinaryContentStream() for a more detailed description of the stream.

If the passed parameter is a string, it's assumed to be a filename to write to. The file is opened in 'w+' mode, and closed before returning.

When passing a resource or Psr7 Stream, the resource is not closed, nor rewound.

Parameters
$filenameResourceOrStream : mixed
Return values
mixed

setCharsetOverride()

Overrides the default character set used for reading content from content streams in cases where a user knows the source charset is not what is specified.

public setCharsetOverride(mixed $charsetOverride[, mixed $onlyIfNoCharset = false ]) : mixed

If set, the returned value from IMessagePart::getCharset() must be ignored during subsequent read operations and streams created out of this part's content.

Note that setting an override on an IMessage and calling getTextStream, getTextContent, getHtmlStream or getHtmlContent will not be applied to those sub-parts, unless the text/html part is the IMessage itself. Instead, IMessage::getTextPart() should be called, and setCharsetOverride called on the returned IMessagePart.

Parameters
$charsetOverride : mixed

the actual charset of the content.

$onlyIfNoCharset : mixed = false

if true, $charsetOverride is used only if getCharset returns null.

Return values
mixed

setContent()

Sets the content of the part to the passed string, resource, or stream.

public setContent(mixed $resource[, mixed $charset = MailMimeParser::DEFAULT_CHARSET ]) : mixed
Parameters
$resource : mixed

the content.

$charset : mixed = MailMimeParser::DEFAULT_CHARSET
Return values
mixed

getAllPartsIterator()

private getAllPartsIterator() : mixed
Return values
mixed

iteratorFindAt()

private iteratorFindAt(Iterator $iter, mixed $index[, mixed $fnFilter = null ]) : mixed
Parameters
$iter : Iterator
$index : mixed
$fnFilter : mixed = null
Return values
mixed

Search results