MailMimeParser 2.3

Message extends MimePart
in package
implements IMessage

An email message.

The message could represent a simple text email, a multipart message with children, or a non-mime message containing UUEncoded parts.

Tags
author

Zaahid Bateson

Interfaces, Classes, Traits and Enums

IMessage
An interface representing an email message.

Table of Contents

$charsetOverride  : string|null
$headerContainer  : PartHeaderContainer
$ignoreTransferEncoding  : bool
$observers  : SplObjectStorage
$parent  : IMimePart|null
$partChildrenContainer  : PartChildrenContainer
$partStreamContainer  : PartStreamContainer
$multipartHelper  : MultipartHelper
$privacyHelper  : PrivacyHelper
__construct()  : mixed
__toString()  : string
Returns the message/part as a string, containing its headers, content and children.
addAttachmentPart()  : static
Adds an attachment part for the passed raw data string, handle, or stream and given parameters.
addAttachmentPartFromFile()  : static
Adds an attachment part using the passed file.
addChild()  : mixed
Registers the passed part as a child of the current part.
addRawHeader()  : static
Adds a header with the given $name and $value.
attach()  : void
attachContentStream()  : static
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()  : void
detachContentStream()  : static
Detaches the content stream.
from()  : IMessage
Convenience method to parse a handle or string into an IMessage without requiring including MailMimeParser, instantiating it, and calling parse.
getAllAttachmentParts()  : array<string|int, IMessagePart>
Returns all attachment parts.
getAllHeaders()  : array<string|int, IHeader>
Returns an array of all headers in this part.
getAllHeadersByName()  : array<string|int, IHeader>
Returns an array of headers that match the passed name.
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.
getAttachmentCount()  : int
Returns the number of attachments available.
getAttachmentPart()  : IMessagePart|null
Returns the attachment part at the given 0-based index, or null if none is set.
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.
getCharset()  : string|null
Returns the charset of the content, or null if not applicable/defined.
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.
getContentDisposition()  : string
Returns the content's disposition, or returns the value of $default if not defined.
getContentId()  : string|null
Returns the Content ID of the part, or null if not defined.
getContentStream()  : StreamInterface|null
Returns the StreamInterface for the part's content or null if the part doesn't have a content section.
getContentTransferEncoding()  : string
Returns the content transfer encoding used to encode the content on this part, or the value of $default if not defined.
getContentType()  : string
Returns the mime type of the content, or $default if one is not set.
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.
getHeader()  : IHeader|null
Returns the IHeader object for the header with the given $name.
getHeaderParameter()  : string|null
Returns the value of the parameter named $param on a header with the passed $header name, or null if the parameter doesn't exist and a $defaultValue isn't passed.
getHeaderValue()  : string|null
Returns the string value for the header with the given $name, or null if the header doesn't exist and no alternative $defaultValue is passed.
getHtmlContent()  : string|null
Returns the content of the inline text/html part as a string.
getHtmlPart()  : IMessagePart|null
Returns the inline text/html IMessagePart for a message.
getHtmlPartCount()  : int
Returns the number of inline text/html parts this message contains.
getHtmlStream()  : StreamInterface|null
Returns a Psr7 Stream for the 'inline' text/html content.
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.
getRawHeaderIterator()  : Iterator
Returns an iterator to all headers in this part. Each returned element is an array with its first element set to the header's name, and the second to its raw value:
getRawHeaders()  : array<string|int, array<string|int, string>>
Returns a two dimensional string array of all headers for the mime part with the first element holding the name, and the second its raw string value:
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.
getSignaturePart()  : IMimePart
Returns the signature part of a multipart/signed message or null.
getSignedMessageAsString()  : string
Returns a string containing the entire body of a signed message for verification or calculating a signature.
getSignedMessageStream()  : StreamInterface
Returns a stream that can be used to read the content part of a signed message, which can be used to sign an email or verify a signature.
getStream()  : StreamInterface
Returns a Psr7 StreamInterface for the string representation of this part, containing its headers, content and children.
getTextContent()  : string|null
Returns the content of the inline text/plain part as a string.
getTextPart()  : IMessagePart|null
Returns the inline text/plain IMessagePart for a message.
getTextPartCount()  : int
Returns the number of inline text/plain parts this message contains.
getTextStream()  : StreamInterface|null
Returns a Psr7 Stream for the 'inline' text/plain content.
hasContent()  : bool
Returns true if the part contains a 'body' (content).
isMime()  : bool
Returns true if the current part is a mime part.
isMultiPart()  : bool
Returns true if this part's content type matches multipart/*
isSignaturePart()  : bool
Returns true if this part's parent is an IMessage, and is the same part returned by {@see IMessage::getSignaturePart()}.
isTextPart()  : bool
Returns true if this part has a defined 'charset' on its Content-Type header.
notify()  : void
removeAllHtmlParts()  : bool
Removes all text/html inline parts in this message.
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.
removeAllTextParts()  : bool
Removes all text/plain inline parts in this message.
removeAttachmentPart()  : self
Removes the attachment at the given index.
removeHeader()  : static
Removes all headers from this part with the passed name.
removeHtmlPart()  : bool
Removes the text/html part of the message at the passed index if one exists (defaults to first part if an index isn't passed).
removePart()  : int|null
Removes the child part from this part and returns its previous position or null if it wasn't found.
removeSingleHeader()  : static
Removes a single header with the passed name (in cases where more than one may exist, and others should be preserved).
removeTextPart()  : bool
Removes the text/plain part of the message at the passed index if one exists (defaults to first part if an index isn't passed).
save()  : static
Writes a string representation of this part, including its headers, content and children to the passed file, resource, or stream.
saveContent()  : self
Saves the binary content of the stream to the passed file, resource or stream.
setAsMultipartSigned()  : static
Turns the message into a multipart/signed message, moving the actual message into a child part, sets the content-type of the main message to multipart/signed and adds an empty signature part as well.
setCharsetOverride()  : static
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()  : static
Sets the content of the part to the passed string, resource, or stream.
setHtmlPart()  : self
Sets the text/html part of the message to the passed $resource, either creating a new part if one doesn't exist for text/html, or assigning the value of $resource to an existing text/html part.
setRawHeader()  : static
Adds a header with the given $name and $value. An optional $offset may be passed, which will overwrite a header if one exists with the given name and offset only. Otherwise a new header is added. The passed $offset may be ignored in that case if it doesn't represent the next insert position for the header with the passed name... instead it would be 'pushed' on at the next position.
setSignature()  : self
Sets the signature body of the message to the passed $body for a multipart/signed message.
setTextPart()  : static
Sets the text/plain part of the message to the passed $resource, either creating a new part if one doesn't exist for text/plain, or assigning the value of $resource to an existing text/plain part.
getAllPartsIterator()  : AppendIterator
iteratorFindAt()  : mixed

Properties

$charsetOverride

protected string|null $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.

$privacyHelper

private PrivacyHelper $privacyHelper

service providing functions for multipart/signed messages.

Methods

__construct()

public __construct([PartStreamContainer|null $streamContainer = null ][, PartHeaderContainer|null $headerContainer = null ][, PartChildrenContainer|null $partChildrenContainer = null ][, MultipartHelper|null $multipartHelper = null ][, PrivacyHelper|null $privacyHelper = null ]) : mixed
Parameters
$streamContainer : PartStreamContainer|null = null
$headerContainer : PartHeaderContainer|null = null
$partChildrenContainer : PartChildrenContainer|null = null
$multipartHelper : MultipartHelper|null = null
$privacyHelper : PrivacyHelper|null = null
Return values
mixed

__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

addAttachmentPart()

Adds an attachment part for the passed raw data string, handle, or stream and given parameters.

public addAttachmentPart(mixed $resource, string $mimeType[, string|null $filename = null ][, string $disposition = 'attachment' ][, string $encoding = 'base64' ]) : static
Parameters
$resource : mixed

the part's content

$mimeType : string

the mime-type of the attachment

$filename : string|null = null

Optional filename (to set relevant header params)

$disposition : string = 'attachment'

Optional Content-Disposition value.

$encoding : string = 'base64'

defaults to 'base64', only applied for a mime email

Return values
static

addAttachmentPartFromFile()

Adds an attachment part using the passed file.

public addAttachmentPartFromFile(mixed $filePath, string $mimeType[, string|null $filename = null ][, string $disposition = 'attachment' ][, string $encoding = 'base64' ]) : static
Parameters
$filePath : mixed

file to attach

$mimeType : string

the mime-type of the attachment

$filename : string|null = null

Optional filename (to set relevant header params)

$disposition : string = 'attachment'

Optional Content-Disposition value.

$encoding : string = 'base64'

defaults to 'base64', only applied for a mime email

Return values
static

addChild()

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

public addChild(IMessagePart $part[, int|null $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 : int|null = null

Optional insertion position 0-based index.

Return values
mixed

addRawHeader()

Adds a header with the given $name and $value.

public addRawHeader(string $name, string $value) : static
Parameters
$name : string

The name of the header

$value : string

The raw value of the header.

Return values
static

attach()

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

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[, string $streamCharset = MailMimeParser::DEFAULT_CHARSET ]) : static
Parameters
$stream : StreamInterface

the content

$streamCharset : string = MailMimeParser::DEFAULT_CHARSET

the charset of $stream

Return values
static

detach()

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

detachContentStream()

Detaches the content stream.

public detachContentStream() : static
Return values
static

from()

Convenience method to parse a handle or string into an IMessage without requiring including MailMimeParser, instantiating it, and calling parse.

public static from(resource|StreamInterface|string $resource, bool $attached) : IMessage

If the passed $resource is a resource handle or StreamInterface, the resource must remain open while the returned IMessage object exists. Pass true as the second argument to have the resource attached to the IMessage and closed for you when it's destroyed, or pass false to manually close it if it should remain open after the IMessage object is destroyed.

Parameters
$resource : resource|StreamInterface|string

The resource handle to the input stream of the mime message, or a string containing a mime message.

$attached : bool

pass true to have it attached to the returned IMessage and destroyed with it.

Return values
IMessage

getAllAttachmentParts()

Returns all attachment parts.

public getAllAttachmentParts() : array<string|int, IMessagePart>

The method returns all parts other than the main content part for a non-mime message, and all parts under a mime message except:

  • text/plain and text/html parts with a Content-Disposition not set to 'attachment'
  • all multipart/* parts
  • any signature part
Return values
array<string|int, IMessagePart>

getAllHeaders()

Returns an array of all headers in this part.

public getAllHeaders() : array<string|int, IHeader>
Return values
array<string|int, IHeader>

an array of header objects

getAllHeadersByName()

Returns an array of headers that match the passed name.

public getAllHeadersByName(mixed $name) : array<string|int, IHeader>
Parameters
$name : mixed
Return values
array<string|int, IHeader>

an array of header objects

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.

getAttachmentCount()

Returns the number of attachments available.

public getAttachmentCount() : int
Return values
int

getAttachmentPart()

Returns the attachment part at the given 0-based index, or null if none is set.

public getAttachmentPart(int $index) : IMessagePart|null

The method returns all parts other than the main content part for a non-mime message, and all parts under a mime message except:

  • text/plain and text/html parts with a Content-Disposition not set to 'attachment'
  • all multipart/* parts
  • any signature part
Parameters
$index : int

the 0-based index of the attachment part to return.

Return values
IMessagePart|null

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

getCharset()

Returns the charset of the content, or null if not applicable/defined.

public getCharset() : string|null

Looks for a 'charset' parameter under the 'Content-Type' header of this part and returns it if set, defaulting to 'ISO-8859-1' if the Content-Type header exists and is of type text/plain or text/html.

Note that the returned value is also converted to upper case.

Return values
string|null

the charset

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([string $charset = MailMimeParser::DEFAULT_CHARSET ]) : string|null

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

Parameters
$charset : string = MailMimeParser::DEFAULT_CHARSET

the target charset for the returned string

Return values
string|null

the content

getContentDisposition()

Returns the content's disposition, or returns the value of $default if not defined.

public getContentDisposition([string $default = 'inline' ]) : string

Looks at the 'Content-Disposition' header, which should only contain either 'inline' or 'attachment'. If the header is not one of those values, $default is returned, which defaults to 'inline' unless passed something else.

Parameters
$default : string = 'inline'

Optional default value if not set or does not match 'inline' or 'attachment'.

Return values
string

the content disposition

getContentId()

Returns the Content ID of the part, or null if not defined.

public getContentId() : string|null

Looks up and returns the value of the 'Content-ID' header.

Return values
string|null

the content ID or null if not defined.

getContentStream()

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

public getContentStream([string $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 : string = MailMimeParser::DEFAULT_CHARSET

Optional charset for the returned stream.

Return values
StreamInterface|null

the stream

getContentTransferEncoding()

Returns the content transfer encoding used to encode the content on this part, or the value of $default if not defined.

public getContentTransferEncoding([string $default = '7bit' ]) : string

Looks up and returns the value of the 'Content-Transfer-Encoding' header if set, defaulting to '7bit' if an alternate $default param is not passed.

The returned value is always lowercase, and header values of 'x-uue', 'uue' and 'uuencode' will return 'x-uuencode' instead.

Parameters
$default : string = '7bit'

Optional default value to return if the header isn't set.

Return values
string

the content transfer encoding.

getContentType()

Returns the mime type of the content, or $default if one is not set.

public getContentType([string $default = 'text/plain' ]) : string

Looks at the part's Content-Type header and returns its value if set, or defaults to 'text/plain'.

Note that the returned value is converted to lower case, and may not be identical to calling MimePart::getHeaderValue('Content-Type') in some cases.

Parameters
$default : string = 'text/plain'

Optional default value to specify a default other than text/plain if needed.

Return values
string

the mime type

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

getHeader()

Returns the IHeader object for the header with the given $name.

public getHeader(mixed $name, mixed $offset) : IHeader|null

If the optional $offset is passed, and multiple headers exist with the same name, the one at the passed offset is returned.

Note that mime headers aren't case sensitive, and the '-' character is

If a header with the given $name and $offset doesn't exist, null is returned.

Parameters
$name : mixed

The name of the header to retrieve.

$offset : mixed

Optional offset if there are multiple headers with the given name.

Return values
IHeader|null

the header object

getHeaderParameter()

Returns the value of the parameter named $param on a header with the passed $header name, or null if the parameter doesn't exist and a $defaultValue isn't passed.

public getHeaderParameter(mixed $header, mixed $param[, mixed $defaultValue = null ]) : string|null

Only headers of type ParameterHeader have parameters. Content-Type and Content-Disposition are examples of headers with parameters. "Charset" is a common parameter of Content-Type.

Parameters
$header : mixed

The name of the header.

$param : mixed

The name of the parameter.

$defaultValue : mixed = null

Optional default value to return if the parameter doesn't exist.

Return values
string|null

The value of the parameter.

getHeaderValue()

Returns the string value for the header with the given $name, or null if the header doesn't exist and no alternative $defaultValue is passed.

public getHeaderValue(mixed $name[, mixed $defaultValue = null ]) : string|null

Note that mime headers aren't case sensitive.

Parameters
$name : mixed

The name of the header

$defaultValue : mixed = null

Optional default value to return if the header doesn't exist on this part.

Return values
string|null

the value of the header

getHtmlContent()

Returns the content of the inline text/html part as a string.

public getHtmlContent(mixed $index[, mixed $charset = MailMimeParser::DEFAULT_CHARSET ]) : string|null

If the message contains more than one text/html 'inline' part, the default behavior is to return the first part. The content for additional parts can be returned by passing a 0-based index.

If a part at the passed index doesn't exist, null is returned.

Parameters
$index : mixed

Optional 0-based index of inline html part content.

$charset : mixed = MailMimeParser::DEFAULT_CHARSET

Optional charset for the returned string to be encoded in.

Return values
string|null

getHtmlPart()

Returns the inline text/html IMessagePart for a message.

public getHtmlPart(mixed $index) : IMessagePart|null

If the message contains more than one text/html 'inline' part, the default behavior is to return the first part. Additional parts can be returned by passing a 0-based index.

If there are no inline text/plain parts in this message, null is returned.

Parameters
$index : mixed

Optional index of part to return.

Return values
IMessagePart|null

getHtmlPartCount()

Returns the number of inline text/html parts this message contains.

public getHtmlPartCount() : int
Return values
int

getHtmlStream()

Returns a Psr7 Stream for the 'inline' text/html content.

public getHtmlStream(mixed $index[, mixed $charset = MailMimeParser::DEFAULT_CHARSET ]) : StreamInterface|null

If the message contains more than one text/html 'inline' part, the default behavior is to return the first part. The streams for additional parts can be returned by passing a 0-based index.

If a part at the passed index doesn't exist, null is returned.

Parameters
$index : mixed

Optional 0-based index of inline html part stream.

$charset : mixed = MailMimeParser::DEFAULT_CHARSET

Optional charset to encode the stream with.

Return values
StreamInterface|null

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.

getRawHeaderIterator()

Returns an iterator to all headers in this part. Each returned element is an array with its first element set to the header's name, and the second to its raw value:

public getRawHeaderIterator() : Iterator

[ 'Header-Name', 'Header Value' ]

Return values
Iterator

an iterator for raw headers

getRawHeaders()

Returns a two dimensional string array of all headers for the mime part with the first element holding the name, and the second its raw string value:

public getRawHeaders() : array<string|int, array<string|int, string>>

[ [ '1st-Header-Name', 'Header Value' ], [ '2nd-Header-Name', 'Header Value' ] ]

Return values
array<string|int, array<string|int, string>>

an array of raw headers

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.

getSignaturePart()

Returns the signature part of a multipart/signed message or null.

public getSignaturePart() : IMimePart

The signature part is determined to always be the 2nd child of a multipart/signed message, the first being the 'body'.

Using the 'protocol' parameter of the Content-Type header is unreliable in some instances (for instance a difference of x-pgp-signature versus pgp-signature).

Return values
IMimePart

getSignedMessageAsString()

Returns a string containing the entire body of a signed message for verification or calculating a signature.

public getSignedMessageAsString() : string

Non-CRLF new lines are replaced to always be CRLF.

Return values
string

or null if the message doesn't have any children

getSignedMessageStream()

Returns a stream that can be used to read the content part of a signed message, which can be used to sign an email or verify a signature.

public getSignedMessageStream() : StreamInterface

The method simply returns the stream for the first child. No verification of whether the message is in fact a signed message is performed.

Note that unlike getSignedMessageAsString, getSignedMessageStream doesn't replace new lines, and before calculating a signature, LFs not preceded by CR should be replaced with CRLFs.

Return values
StreamInterface

or null if the message doesn't have any children

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.

getTextContent()

Returns the content of the inline text/plain part as a string.

public getTextContent(mixed $index[, mixed $charset = MailMimeParser::DEFAULT_CHARSET ]) : string|null

If the message contains more than one text/plain 'inline' part, the default behavior is to return the first part. The content for additional parts can be returned by passing a 0-based index.

If a part at the passed index doesn't exist, null is returned.

Parameters
$index : mixed

Optional 0-based index of inline text part content.

$charset : mixed = MailMimeParser::DEFAULT_CHARSET

Optional charset for the returned string to be encoded in.

Return values
string|null

getTextPart()

Returns the inline text/plain IMessagePart for a message.

public getTextPart(mixed $index) : IMessagePart|null

If the message contains more than one text/plain 'inline' part, the default behavior is to return the first part. Additional parts can be returned by passing a 0-based index.

If there are no inline text/plain parts in this message, null is returned.

Parameters
$index : mixed

Optional index of part to return.

Return values
IMessagePart|null

getTextPartCount()

Returns the number of inline text/plain parts this message contains.

public getTextPartCount() : int
Return values
int

getTextStream()

Returns a Psr7 Stream for the 'inline' text/plain content.

public getTextStream(mixed $index[, mixed $charset = MailMimeParser::DEFAULT_CHARSET ]) : StreamInterface|null

If the message contains more than one text/plain 'inline' part, the default behavior is to return the first part. The streams for additional parts can be returned by passing a 0-based index.

If a part at the passed index doesn't exist, null is returned.

Parameters
$index : mixed

Optional 0-based index of inline text part stream.

$charset : mixed = MailMimeParser::DEFAULT_CHARSET

Optional charset to encode the stream with.

Return values
StreamInterface|null

hasContent()

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

public hasContent() : bool
Return values
bool

isMime()

Returns true if the current part is a mime part.

public isMime() : bool

The message is considered 'mime' if it has either a Content-Type or MIME-Version header defined.

Return values
bool

isMultiPart()

Returns true if this part's content type matches multipart/*

public isMultiPart() : bool
Return values
bool

isSignaturePart()

Returns true if this part's parent is an IMessage, and is the same part returned by {@see IMessage::getSignaturePart()}.

public isSignaturePart() : bool
Return values
bool

isTextPart()

Returns true if this part has a defined 'charset' on its Content-Type header.

public isTextPart() : bool

This may result in some false positives if charset is set on a part that is not plain text which has been seen. If a part is known to be binary, it's better to use IMessagePart::getBinaryContentStream() to avoid issues, or to call IMessagePart::saveContent() directly if saving a part's content.

Return values
bool

notify()

public notify() : void
Return values
void

removeAllHtmlParts()

Removes all text/html inline parts in this message.

public removeAllHtmlParts([bool $moveRelatedPartsBelowMessage = true ]) : bool

If the message contains a multipart/alternative part, the html parts are removed from below the alternative part only. If there is only one remaining part after that, it is moved up, replacing the multipart/alternative part.

If the multipart/alternative part further contains a multipart/related (or mixed) part which holds an inline html part, only parts from that child multipart are removed, and if the passed $moveRelatedPartsBelowMessage is true, any non-html parts are moved to be below the message directly (changing the message into a multipart/mixed message if need be).

For more control, call IMessagePart::removePart() with parts you wish to remove.

Parameters
$moveRelatedPartsBelowMessage : bool = true

Optionally pass false to remove related parts.

Return values
bool

true on success

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.

removeAllTextParts()

Removes all text/plain inline parts in this message.

public removeAllTextParts([bool $moveRelatedPartsBelowMessage = true ]) : bool

If the message contains a multipart/alternative part, the text parts are removed from below the alternative part only. If there is only one remaining part after that, it is moved up, replacing the multipart/alternative part.

If the multipart/alternative part further contains a multipart/related (or mixed) part which holds an inline text part, only parts from that child multipart are removed, and if the passed $moveRelatedPartsBelowMessage is true, any non-text parts are moved to be below the message directly (changing the message into a multipart/mixed message if need be).

For more control, call IMessagePart::removePart() with parts you wish to remove.

Parameters
$moveRelatedPartsBelowMessage : bool = true

Optionally pass false to remove related parts.

Return values
bool

true on success

removeAttachmentPart()

Removes the attachment at the given index.

public removeAttachmentPart(int $index) : self

Attachments are considered to be all parts other than the main content part for a non-mime message, and all parts under a mime message except:

  • text/plain and text/html parts with a Content-Disposition not set to 'attachment'
  • all multipart/* parts
  • any signature part
Parameters
$index : int
Return values
self

removeHeader()

Removes all headers from this part with the passed name.

public removeHeader(string $name) : static
Parameters
$name : string

The name of the header(s) to remove.

Return values
static

removeHtmlPart()

Removes the text/html part of the message at the passed index if one exists (defaults to first part if an index isn't passed).

public removeHtmlPart(int $index) : bool

Returns true if a part exists at the passed index and has been removed.

Parameters
$index : int

Optional 0-based index of inline html part to remove.

Return values
bool

true on success

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.

removeSingleHeader()

Removes a single header with the passed name (in cases where more than one may exist, and others should be preserved).

public removeSingleHeader(string $name, int $offset) : static
Parameters
$name : string

The name of the header to remove

$offset : int

Optional offset of the header to remove (defaults to 0 -- the first header).

Return values
static

removeTextPart()

Removes the text/plain part of the message at the passed index if one exists (defaults to first part if an index isn't passed).

public removeTextPart(int $index) : bool

Returns true if a part exists at the passed index and has been removed.

Parameters
$index : int

Optional 0-based index of inline text part to remove.

Return values
bool

true on success

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[, string $filemode = 'w+' ]) : static
Parameters
$filenameResourceOrStream : mixed

the file, resource, or stream to write to.

$filemode : string = 'w+'

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

Return values
static

saveContent()

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

public saveContent(mixed $filenameResourceOrStream) : self

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
self

setAsMultipartSigned()

Turns the message into a multipart/signed message, moving the actual message into a child part, sets the content-type of the main message to multipart/signed and adds an empty signature part as well.

public setAsMultipartSigned(string $micalg, string $protocol) : static
Parameters
$micalg : string

The Message Integrity Check algorithm being used

$protocol : string

The mime-type of the signature body

Return values
static

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(string $charsetOverride[, bool $onlyIfNoCharset = false ]) : static
Parameters
$charsetOverride : string

the actual charset of the content.

$onlyIfNoCharset : bool = false

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

Return values
static

setContent()

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

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

the content.

$charset : string = MailMimeParser::DEFAULT_CHARSET
Return values
static

setHtmlPart()

Sets the text/html part of the message to the passed $resource, either creating a new part if one doesn't exist for text/html, or assigning the value of $resource to an existing text/html part.

public setHtmlPart(mixed $resource[, string $charset = 'UTF-8' ]) : self

The optional $contentTypeCharset parameter is the charset for the text/html part's Content-Type, not the charset of the passed $resource. $resource must be encoded in UTF-8 regardless of the target charset.

Parameters
$resource : mixed

UTF-8 encoded content.

$charset : string = 'UTF-8'
Return values
self

setRawHeader()

Adds a header with the given $name and $value. An optional $offset may be passed, which will overwrite a header if one exists with the given name and offset only. Otherwise a new header is added. The passed $offset may be ignored in that case if it doesn't represent the next insert position for the header with the passed name... instead it would be 'pushed' on at the next position.

public setRawHeader(string $name, string|null $value, int $offset) : static
Parameters
$name : string

The name of the new header, e.g. 'Content-Type'.

$value : string|null

The raw value of the new header.

$offset : int

An optional offset, defaulting to '0' and therefore overriding the first header of the given $name if one exists.

Return values
static

setSignature()

Sets the signature body of the message to the passed $body for a multipart/signed message.

public setSignature(string $body) : self
Parameters
$body : string

the message's hash

Return values
self

setTextPart()

Sets the text/plain part of the message to the passed $resource, either creating a new part if one doesn't exist for text/plain, or assigning the value of $resource to an existing text/plain part.

public setTextPart(mixed $resource[, string $charset = 'UTF-8' ]) : static
Parameters
$resource : mixed

UTF-8 encoded content.

$charset : string = 'UTF-8'
Return values
static

getAllPartsIterator()

private getAllPartsIterator() : AppendIterator
Return values
AppendIterator

iteratorFindAt()

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

Search results