Message
extends Message
in package
uses
CharsetAwareTrait
Message class
Tags
Table of Contents
Constants
- CRLF = "\r\n"
- Message newline constant
Properties
- $addresses : array<string|int, mixed>
- Message addresses
- $body : Body|null
- Body
- $boundary : string|null
- Boundary
- $charSet : string|null
- Character set
- $headers : array<string|int, mixed>
- Headers
- $parts : array<string|int, mixed>
- Nested parts
- $subType : string|null
- Subtype
Methods
- __clone() : void
- Perform a "deep" clone of a message object
- __construct() : mixed
- Constructor
- __toString() : string
- Render the part
- addFile() : Part
- Add file as body
- addHeader() : Part
- Add a header
- addHeaders() : Part
- Add headers
- addHtml() : Message
- Add HTML message part
- addPart() : Message
- Add message part
- addParts() : Part
- Add nested parts
- addText() : Message
- Add text message part
- attachFile() : Message
- Attach file message part
- attachFileFromStream() : Message
- Attach file message part from stream
- attachment() : static
- Create an attachment part from a file on disk
- attachmentFromContent() : static
- Create an attachment part from in-memory content
- createForm() : Message
- Create multipart form object
- decodeText() : string
- Decode text
- generateBoundary() : string
- Generate boundary
- generateId() : string
- Generate a Message-ID and set it as a side effect
- getAttachments() : array<string|int, mixed>
- Get attachments
- getBcc() : array<string|int, mixed>
- Get BCC
- getBody() : Body|null
- Get body
- getBodyContent() : string|null
- Get the rendered body content
- getBoundary() : string
- Get message MIME boundary (auto-generating and setting MIME-Version if needed)
- getCc() : array<string|int, mixed>
- Get CC
- getCharSet() : string|null
- Get character set
- getContents() : mixed
- Get contents (decoded)
- getContentType() : string|null
- Get content-type
- getFilename() : string|null
- Get attachment filename
- getFrom() : array<string|int, mixed>
- Get From
- getHeader() : Header|null
- Get headers
- getHeaderAsString() : string|null
- Get a single header rendered as a full "Name: Value" string
- getHeaders() : array<string|int, mixed>
- Get headers (flat name => string map)
- getHeadersAsArray() : array<string|int, mixed>
- Get headers as array
- getHeadersAsString() : string
- Get all headers rendered as a string, one per line
- getHeaderValue() : string|null
- Get header value as a string
- getPart() : Part|null
- Get message part
- getParts() : array<string|int, mixed>
- Get message parts
- getReplyTo() : array<string|int, mixed>
- Get Reply-To
- getReturnPath() : array<string|int, mixed>
- Get Return-Path
- getSender() : array<string|int, mixed>
- Get Sender
- getSubject() : string|null
- Get subject
- getSubType() : string|null
- Get subtype
- getTo() : array<string|int, mixed>
- Get To
- hasAttachment() : bool
- Has attachment (check via a header)
- hasAttachments() : bool
- Does message have attachments (check via parts)
- hasBcc() : bool
- Has BCC
- hasBody() : bool
- Has body
- hasBoundary() : bool
- Has boundary
- hasCc() : bool
- Has CC
- hasFrom() : bool
- Has From
- hasHeader() : bool
- Has header
- hasHeaders() : bool
- Has headers
- hasParts() : bool
- Has nested parts
- hasReplyTo() : bool
- Has Reply-To
- hasReturnPath() : bool
- Has Return-Path
- hasSender() : bool
- Has Sender
- hasSubType() : bool
- Has subtype
- hasTo() : bool
- Has To
- html() : static
- Create a text/html part
- inferSubType() : Part
- Infer and set the subtype (alternative/mixed) from the current nested parts
- load() : Message
- Load a message from a string source or file on disk
- parse() : Message
- Parse message from string
- parseBody() : array<string|int, mixed>
- Parse message body string
- parseForm() : array<string|int, mixed>
- Parse form data
- parseFromFile() : Message
- Parse message from file
- parseHeaders() : array<string|int, mixed>
- Parse message header string
- parseMessage() : Message
- Parse message
- parsePart() : Part|array<string|int, mixed>
- Parse message part string
- removeHeader() : Message
- Remove header
- render() : string
- Render the message
- renderAsLines() : array<string|int, mixed>
- Render as an array of lines
- renderBody() : string
- Render the part body
- renderHeaders() : string
- Render the part headers
- renderParts() : string
- Render the parts
- renderRaw() : string
- Render the part raw (no headers or preamble)
- save() : void
- Save message to file on disk
- setBcc() : Message
- Set BCC
- setBody() : Message
- Set body
- setBoundary() : Part
- Set boundary
- setCc() : Message
- Set CC
- setCharSet() : static
- Set character set
- setContentId() : Part
- Set the Content-ID header
- setContentType() : static
- Set Content-Type header
- setFrom() : Message
- Set From
- setMessageId() : Message
- Set the Message-ID header
- setReplyTo() : Message
- Set Reply-To
- setReturnPath() : Message
- Set Return-Path
- setSender() : Message
- Set Sender
- setSubject() : Message
- Set Subject
- setSubType() : Part
- Set subtype
- setTo() : Message
- Set To
- text() : static
- Create a text/plain part
- toByteStream() : void
- Write this entire entity to a buffer
- detectContentType() : string
- Detect a content type from a filename's extension
- addressListToArray() : array<string|int, mixed>
- Convert an AddressList into an email => ?name array
- arrayToAddressString() : string
- Convert an array of addresses into a comma-joined address string
Constants
CRLF
Message newline constant
public
string
CRLF
= "\r\n"
Properties
$addresses
Message addresses
protected
array<string|int, mixed>
$addresses
= ['To' => [], 'CC' => [], 'BCC' => [], 'From' => [], 'Reply-To' => [], 'Sender' => [], 'Return-Path' => []]
$body
Body
protected
Body|null
$body
= null
$boundary
Boundary
protected
string|null
$boundary
= null
$charSet
Character set
protected
string|null
$charSet
= null
$headers
Headers
protected
array<string|int, mixed>
$headers
= []
$parts
Nested parts
protected
array<string|int, mixed>
$parts
= []
$subType
Subtype
protected
string|null
$subType
= null
Methods
__clone()
Perform a "deep" clone of a message object
public
__clone() : void
__construct()
Constructor
public
__construct([string|null $subject = null ]) : mixed
Instantiate the message object
Parameters
- $subject : string|null = null
__toString()
Render the part
public
__toString() : string
Return values
stringaddFile()
Add file as body
public
addFile(string $file[, string $disposition = 'attachment' ][, Encoding $encoding = PartBodyEncoding::BASE64 ][, int|bool $split = true ]) : Part
Parameters
- $file : string
- $disposition : string = 'attachment'
- $encoding : Encoding = PartBodyEncoding::BASE64
- $split : int|bool = true
Tags
Return values
PartaddHeader()
Add a header
public
addHeader(Header|string $header[, string|null $value = null ]) : Part
Parameters
- $header : Header|string
- $value : string|null = null
Return values
PartaddHeaders()
Add headers
public
addHeaders(array<string|int, mixed> $headers) : Part
Parameters
- $headers : array<string|int, mixed>
Return values
PartaddHtml()
Add HTML message part
public
addHtml(mixed $html) : Message
Parameters
- $html : mixed
Return values
MessageaddPart()
Add message part
public
addPart(Part $part) : Message
Auto-infers the multipart subtype (alternative/mixed) from the nested parts via Part::inferSubType(); when parts exist but no subtype could be inferred (e.g. a single text-only part), defaults to 'mixed' per the "Single-part rendering" spec resolution.
Parameters
- $part : Part
Return values
MessageaddParts()
Add nested parts
public
addParts(array<string|int, mixed> $parts) : Part
Parameters
- $parts : array<string|int, mixed>
Return values
PartaddText()
Add text message part
public
addText(mixed $text) : Message
Parameters
- $text : mixed
Return values
MessageattachFile()
Attach file message part
public
attachFile(string $file[, Encoding $encoding = PartBodyEncoding::BASE64 ]) : Message
Parameters
- $file : string
- $encoding : Encoding = PartBodyEncoding::BASE64
Return values
MessageattachFileFromStream()
Attach file message part from stream
public
attachFileFromStream(string $stream[, string $basename = 'file.tmp' ][, Encoding $encoding = PartBodyEncoding::BASE64 ]) : Message
Parameters
- $stream : string
- $basename : string = 'file.tmp'
- $encoding : Encoding = PartBodyEncoding::BASE64
Return values
Messageattachment()
Create an attachment part from a file on disk
public
static attachment(string $file[, string|null $contentType = null ][, string $disposition = 'attachment' ][, Encoding $encoding = PartBodyEncoding::BASE64 ][, int|bool $split = true ]) : static
Parameters
- $file : string
- $contentType : string|null = null
- $disposition : string = 'attachment'
- $encoding : Encoding = PartBodyEncoding::BASE64
- $split : int|bool = true
Return values
staticattachmentFromContent()
Create an attachment part from in-memory content
public
static attachmentFromContent(string $content, string $filename[, string|null $contentType = null ][, string $disposition = 'attachment' ][, Encoding $encoding = PartBodyEncoding::BASE64 ][, int|bool $split = true ]) : static
Parameters
- $content : string
- $filename : string
- $contentType : string|null = null
- $disposition : string = 'attachment'
- $encoding : Encoding = PartBodyEncoding::BASE64
- $split : int|bool = true
Return values
staticcreateForm()
Create multipart form object
public
static createForm([array<string|int, mixed> $fields = [] ]) : Message
Parameters
- $fields : array<string|int, mixed> = []
Return values
MessagedecodeText()
Decode text
public
static decodeText(string $text) : string
Parameters
- $text : string
Return values
stringgenerateBoundary()
Generate boundary
public
generateBoundary() : string
Return values
stringgenerateId()
Generate a Message-ID and set it as a side effect
public
generateId([string|null $domain = null ]) : string
This overrides Part::generateId(?string $domain = null): string (same
signature, legal). Note: MimeMessage::setMessageId() internally does
$id ?? $this->generateId($domain) when no $id is given - calling
setMessageId(null, $domain) from inside this override would resolve
$this->generateId() polymorphically back to THIS method, recursing
forever. Generating the id via parent::generateId() first and passing
it explicitly avoids that.
Parameters
- $domain : string|null = null
Return values
stringgetAttachments()
Get attachments
public
getAttachments() : array<string|int, mixed>
Return values
array<string|int, mixed>getBcc()
Get BCC
public
getBcc() : array<string|int, mixed>
Return values
array<string|int, mixed>getBody()
Get body
public
getBody() : Body|null
Return values
Body|nullgetBodyContent()
Get the rendered body content
public
getBodyContent() : string|null
Return values
string|nullgetBoundary()
Get message MIME boundary (auto-generating and setting MIME-Version if needed)
public
getBoundary() : string
Return values
stringgetCc()
Get CC
public
getCc() : array<string|int, mixed>
Return values
array<string|int, mixed>getCharSet()
Get character set
public
getCharSet() : string|null
Return values
string|nullgetContents()
Get contents (decoded)
public
getContents() : mixed
getContentType()
Get content-type
public
getContentType() : string|null
Return values
string|nullgetFilename()
Get attachment filename
public
getFilename() : string|null
Return values
string|nullgetFrom()
Get From
public
getFrom() : array<string|int, mixed>
Return values
array<string|int, mixed>getHeader()
Get headers
public
getHeader(string $name) : Header|null
Parameters
- $name : string
Return values
Header|nullgetHeaderAsString()
Get a single header rendered as a full "Name: Value" string
public
getHeaderAsString(string $name) : string|null
Parameters
- $name : string
Return values
string|nullgetHeaders()
Get headers (flat name => string map)
public
getHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed>getHeadersAsArray()
Get headers as array
public
getHeadersAsArray() : array<string|int, mixed>
Return values
array<string|int, mixed>getHeadersAsString()
Get all headers rendered as a string, one per line
public
getHeadersAsString([array<string|int, mixed> $omitHeaders = [] ]) : string
Parameters
- $omitHeaders : array<string|int, mixed> = []
Return values
stringgetHeaderValue()
Get header value as a string
public
getHeaderValue(string $name) : string|null
Named getHeaderValue() rather than getHeader() because Pop\Mime\Part already
declares a public getHeader(string $name): ?Header. A same-named override
returning ?string here is not legal (return-type covariance requires a
subtype of Header|null, and string is unrelated) - PHP fatals at class-load
time for every new Message(). getHeader() is therefore left un-overridden
and inherited as-is (returns the raw Part\Header object, consistent with how
Text/Html/Attachment already expose it); this method is the flat-string
convenience accessor instead.
Parameters
- $name : string
Return values
string|nullgetPart()
Get message part
public
getPart(int $i) : Part|null
Parameters
- $i : int
Return values
Part|nullgetParts()
Get message parts
public
getParts() : array<string|int, mixed>
Return values
array<string|int, mixed>getReplyTo()
Get Reply-To
public
getReplyTo() : array<string|int, mixed>
Return values
array<string|int, mixed>getReturnPath()
Get Return-Path
public
getReturnPath() : array<string|int, mixed>
Return values
array<string|int, mixed>getSender()
Get Sender
public
getSender() : array<string|int, mixed>
Return values
array<string|int, mixed>getSubject()
Get subject
public
getSubject() : string|null
Return values
string|nullgetSubType()
Get subtype
public
getSubType() : string|null
Return values
string|nullgetTo()
Get To
public
getTo() : array<string|int, mixed>
Return values
array<string|int, mixed>hasAttachment()
Has attachment (check via a header)
public
hasAttachment() : bool
Return values
boolhasAttachments()
Does message have attachments (check via parts)
public
hasAttachments() : bool
Return values
boolhasBcc()
Has BCC
public
hasBcc() : bool
Return values
boolhasBody()
Has body
public
hasBody() : bool
Return values
boolhasBoundary()
Has boundary
public
hasBoundary() : bool
Return values
boolhasCc()
Has CC
public
hasCc() : bool
Return values
boolhasFrom()
Has From
public
hasFrom() : bool
Return values
boolhasHeader()
Has header
public
hasHeader(string $name) : bool
Parameters
- $name : string
Return values
boolhasHeaders()
Has headers
public
hasHeaders() : bool
Return values
boolhasParts()
Has nested parts
public
hasParts() : bool
Return values
boolhasReplyTo()
Has Reply-To
public
hasReplyTo() : bool
Return values
boolhasReturnPath()
Has Return-Path
public
hasReturnPath() : bool
Return values
boolhasSender()
Has Sender
public
hasSender() : bool
Return values
boolhasSubType()
Has subtype
public
hasSubType() : bool
Return values
boolhasTo()
Has To
public
hasTo() : bool
Return values
boolhtml()
Create a text/html part
public
static html(string $content) : static
Parameters
- $content : string
Return values
staticinferSubType()
Infer and set the subtype (alternative/mixed) from the current nested parts
public
inferSubType() : Part
Return values
Partload()
Load a message from a string source or file on disk
public
static load(string $message) : Message
Parameters
- $message : string
Tags
Return values
Messageparse()
Parse message from string
public
static parse(string $stream) : Message
Parameters
- $stream : string
Tags
Return values
MessageparseBody()
Parse message body string
public
static parseBody(string $bodyString[, string|null $boundary = null ]) : array<string|int, mixed>
Parameters
- $bodyString : string
- $boundary : string|null = null
Return values
array<string|int, mixed>parseForm()
Parse form data
public
static parseForm(string $formString) : array<string|int, mixed>
Parameters
- $formString : string
Return values
array<string|int, mixed>parseFromFile()
Parse message from file
public
static parseFromFile(string $file) : Message
Parameters
- $file : string
Tags
Return values
MessageparseHeaders()
Parse message header string
public
static parseHeaders(string $headerString) : array<string|int, mixed>
Parameters
- $headerString : string
Return values
array<string|int, mixed>parseMessage()
Parse message
public
static parseMessage(string $messageString) : Message
Parameters
- $messageString : string
Return values
MessageparsePart()
Parse message part string
public
static parsePart(string $partString) : Part|array<string|int, mixed>
Parameters
- $partString : string
Return values
Part|array<string|int, mixed>removeHeader()
Remove header
public
removeHeader(string $header) : Message
Parameters
- $header : string
Return values
Messagerender()
Render the message
public
render([bool $preamble = true ][, string|null $body = null ]) : string
Overrides Part::render() to explicitly call getBoundary() first (when parts are present), which is what actually generates the MIME-Version header as a side effect. Part::renderParts() (called internally by the inherited render()) reads $this->boundary/generateBoundary() directly and never routes through this class's getBoundary() override, so without this, MIME-Version was never emitted on real rendered output. Routing through getHeadersAsString() also picks up the charset-append logic that the inherited renderHeaders() doesn't have.
Body must be rendered BEFORE headers are stringified: renderParts() (invoked by getBodyContent()) is what lazily synthesizes the Content-Type: multipart/...; boundary=... header as a side effect when one isn't already present. Calling getHeadersAsString() first would stringify the header set before that side effect fires, silently dropping Content-Type from the rendered output.
$body may be passed in to reuse an already-rendered body (e.g. when only the headers differ across repeated renders of the same message, such as looping Bcc transactions) instead of re-running renderParts() (and re-encoding any attachments) on every call.
Parameters
- $preamble : bool = true
- $body : string|null = null
Return values
stringrenderAsLines()
Render as an array of lines
public
renderAsLines([string|null $body = null ]) : array<string|int, mixed>
Parameters
- $body : string|null = null
-
pre-rendered body to reuse (see render())
Return values
array<string|int, mixed>renderBody()
Render the part body
public
renderBody() : string
Return values
stringrenderHeaders()
Render the part headers
public
renderHeaders() : string
Return values
stringrenderParts()
Render the parts
public
renderParts([bool $preamble = true ][, bool $headers = true ]) : string
Parameters
- $preamble : bool = true
- $headers : bool = true
Return values
stringrenderRaw()
Render the part raw (no headers or preamble)
public
renderRaw() : string
Return values
stringsave()
Save message to file on disk
public
save(string $to) : void
Parameters
- $to : string
setBcc()
Set BCC
public
setBcc(mixed $bcc) : Message
Parameters
- $bcc : mixed
Return values
MessagesetBody()
Set body
public
setBody(mixed $body) : Message
Parameters
- $body : mixed
Return values
MessagesetBoundary()
Set boundary
public
setBoundary(string $boundary) : Part
Parameters
- $boundary : string
Return values
PartsetCc()
Set CC
public
setCc(mixed $cc) : Message
Parameters
- $cc : mixed
Return values
MessagesetCharSet()
Set character set
public
setCharSet([string|null $charSet = null ]) : static
Parameters
- $charSet : string|null = null
Return values
staticsetContentId()
Set the Content-ID header
public
setContentId([string|null $id = null ][, string|null $domain = null ]) : Part
Parameters
- $id : string|null = null
- $domain : string|null = null
Return values
PartsetContentType()
Set Content-Type header
public
setContentType(string $contentType) : static
Parameters
- $contentType : string
Return values
staticsetFrom()
Set From
public
setFrom(mixed $from) : Message
Parameters
- $from : mixed
Return values
MessagesetMessageId()
Set the Message-ID header
public
setMessageId([string|null $id = null ][, string|null $domain = null ]) : Message
Parameters
- $id : string|null = null
- $domain : string|null = null
Return values
MessagesetReplyTo()
Set Reply-To
public
setReplyTo(mixed $replyTo) : Message
Parameters
- $replyTo : mixed
Return values
MessagesetReturnPath()
Set Return-Path
public
setReturnPath(mixed $returnPath) : Message
Parameters
- $returnPath : mixed
Return values
MessagesetSender()
Set Sender
public
setSender(mixed $sender) : Message
Parameters
- $sender : mixed
Return values
MessagesetSubject()
Set Subject
public
setSubject(string $subject) : Message
Parameters
- $subject : string
Return values
MessagesetSubType()
Set subtype
public
setSubType(string $subType) : Part
Parameters
- $subType : string
Return values
PartsetTo()
Set To
public
setTo(mixed $to) : Message
Parameters
- $to : mixed
Return values
Messagetext()
Create a text/plain part
public
static text(string $content) : static
Parameters
- $content : string
Return values
statictoByteStream()
Write this entire entity to a buffer
public
toByteStream(BufferInterface $is[, string|null $body = null ]) : void
Parameters
- $is : BufferInterface
- $body : string|null = null
-
pre-rendered body to reuse (see render())
detectContentType()
Detect a content type from a filename's extension
protected
static detectContentType(string $filename) : string
Parameters
- $filename : string
Return values
stringaddressListToArray()
Convert an AddressList into an email => ?name array
private
static addressListToArray(AddressList $list) : array<string|int, mixed>
Parameters
- $list : AddressList
Return values
array<string|int, mixed>arrayToAddressString()
Convert an array of addresses into a comma-joined address string
private
static arrayToAddressString(array<string|int, mixed> $addresses) : string
Supports:
- ['email@example.com' => 'Name', ...] (key is email)
- ['Name' => 'email@example.com', ...] (value is email)
- [0 => 'email@example.com', ...] (plain list)
- [0 => stdClass{mailbox, host}, ...]
Parameters
- $addresses : array<string|int, mixed>