Message
Message
Class Message.
Represents a standard MIME (Multipurpose Internet Mail Extensions) message.
This class provides a robust structure for creating and manipulating email messages including header management, multipart content handling (such as HTML and Text alternatives), and automatic boundary generation.
It supports both encoding (object to string) for sending emails and decoding (raw string to object) for parsing received emails. Additionally, it implements JsonSerializable for easy conversion to JSON format.
Properties
crlf
protected string $crlf = '"\r\n"'parts
private array $partsheaders
private array $headersmsgid
private string $msgidboundary
private string $boundaryMethods
__construct
Message constructor.
public __construct(array $parts, array $headers): voidInitializes a new MIME message with optional parts and headers. Sets a unique message ID and defines a multipart boundary.
Parameters
| Parameter | Type | Description |
|---|---|---|
$parts | array | an array of message parts to add immediately |
$headers | array | initial headers to set for the message |
__toString
Convert the message to a string.
public __toString(): voidThis method is a wrapper for the encode() method and returns the encoded message as a string.
addPart
Adds a part to the MIME message.
public addPart(mixed $part): voidThis method accepts either a Part object or an array describing the part content. If an array is provided, it attempts to construct a Part or Html object based on the 'type' key.
Parameters
| Parameter | Type | Description |
|---|---|---|
$part | mixed | The part to add. Can be an instance of Part, or an array containing 'content' and optionally 'type' (e.g., 'html') and 'headers'. |
addHeaders
Add headers to the message.
public addHeaders(string $headers): voidThis method appends new headers to the existing message headers. If a string is provided, it is converted into an array before merging.
Parameters
| Parameter | Type | Description |
|---|---|---|
$headers | string | The header(s) to add. Can be a single header string or an array of header strings. |
getHeaders
Get the headers of the MIME message.
public getHeaders(): voidgetHeader
Retrieves the value of a specific header field from the message.
public getHeader(string $name): stringThis method searches the message headers for the specified name. If the header exists, its value is returned. If the header does not exist, an empty string is returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the header field to retrieve (e.g., 'Subject', 'From'). |
getParts
Returns the parts of the message.
public getParts(): voidfindPart
Find a specific MIME part within the message by its content type.
public findPart(string $contentType): PartThis method searches through the immediate parts of the message for a part that matches one of the provided content types.
Parameters
| Parameter | Type | Description |
|---|---|---|
$contentType | string | The content type(s) to search for. Can be a single string (e.g., 'text/html') or an array of strings. |
encode
Encodes the MIME message into a string.
public encode(): stringThis method constructs a multipart MIME message string starting with a standard preamble. It iterates through all message parts, appending them with the appropriate boundary separators, and concludes with a final closing boundary.
decode
Decodes a raw MIME message string into a Message object.
public decode(string $data): MessageThis method parses the input string, separating the headers from the body. If the content type is 'multipart', it further parses the body using the specified boundary into an array of Part objects.
Parameters
| Parameter | Type | Description |
|---|---|---|
$data | string | the raw MIME message string to decode |
parseMessageHeaders
Parses the headers of a MIME message.
public parseMessageHeaders(string $content): voidThis method takes a string containing the raw headers of a MIME message and parses them into an associative array. It handles multi-line headers by unfolding them.
Parameters
| Parameter | Type | Description |
|---|---|---|
$content | string | the raw content of the message headers |
jsonSerialize
Serializes the message object to a value that can be serialized natively by json_encode().
public jsonSerialize(): mixedGenerated by Hazaar API Doc Generator on Wed, 07 Jan 2026 11:29:59 +0000