Email message class.
This class is used for sending Internet Message Format based on the standard outlined in https://www.rfc-editor.org/rfc/rfc2822.txt
string '/^((?:[\\p{L}0-9.!#$%&\'*+\\/=?^_`{|}~-]+)*@[\\p{L}0-9-._]+)$/ui' Holds the regex pattern for email validation
int 998
Line length - no must more - RFC 2822 - 2.1.1
int 78
Line length - no should more - RFC 2822 - 2.1.1
string 'both'
Type of message - BOTH
string 'html'
Type of message - HTML
string 'text'
Type of message - TEXT
string|nullThe application wide charset, used to encode headers and body
array<string, array>List of files that should be attached to the email.
arrayBlind Carbon Copy
string|nullIf set, boundary to use for multipart mime messages
arrayCarbon Copy
stringCharset the email body is sent in
array<string>8Bit character sets
array<string, string>Define Content-Type charset name
stringDomain for messageId generation. Needs to be manually set for CLI mailing as env('HTTP_HOST') is empty
stringWhat format should the email be sent in
array<string>Available formats to be sent.
string|nullRegex for email validation
arrayThe mail which the email is sent from
string|nullCharset the email header is sent in If null, the $charset property will be used as default
arrayAssociative array of a user defined headers Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
stringHtml message
arrayFinal message to send
string|boolMessage ID
int|nullContains the optional priority of the email.
arrayThe read receipt email
arrayList of email(s) that the recipient will reply to
arrayThe mail that will be used in case of any errors like
arrayThe sender email
stringThe subject of the email
stringText message
arrayRecipient of the email
string|nullThe email transfer encoding used. If null, the $charset property is used for determined the transfer encoding.
array<string>Available encoding to be set for transfer.
Constructor
Magic method used for serializing the Message object.
Magic method used to rebuild the Message object.
Add attachments
Add "bcc" address.
Add "cc" address.
Add email
Add header for the message
Add "Reply-To" address.
Add "To" address.
Attach non-embedded files by adding file contents inside boundaries.
Attach inline/embedded files to the message.
Create unique boundary identifier
Configures an email instance object from serialized config.
Decode the specified string
Encode the specified string using the current charset
Translates a string for one charset to another if the App.encoding value differs and the mb_convert_encoding function exists
Format addresses
Generate full message.
Gets attachments to the email message.
Gets "bcc" address.
Get generated message body as array.
Get HTML body of message.
Get generated body as string.
Get text body of message.
Gets the body types that are in this email message
Gets "cc" address.
Charset getter.
Return the Content-Transfer Encoding value based on the set transferEncoding or set charset.
Return charset value for Content-Type.
Gets domain.
Gets email format.
EmailPattern setter/getter
Gets "from" address.
HeaderCharset getter.
Get list of headers
Get headers as string.
Gets message ID.
Get original subject without encoding
Gets priority.
Gets Read Receipt (Disposition-Notification-To header).
Gets "Reply-To" address.
Gets return path.
Gets the "sender" address. See RFC link below for full explanation.
Gets subject.
Gets "to" address
TransferEncoding getter.
Serializes the email object to a value that can be natively serialized and re-used to clone this email instance.
Read the file contents and return a base64 version of the file contents.
Reset all the internal variables to be able to send out a new email.
Serializes the Email object.
Add attachments to the email message
Sets "bcc" address.
Set message body.
Set HTML body for message.
Set text body for message.
Sets "cc" address.
Charset setter.
Sets the configuration for this instance.
Sets domain.
Set email
Sets email format.
EmailPattern setter/getter
Set only 1 email
Sets "from" address.
HeaderCharset setter.
Sets headers for the message
Sets message ID.
Sets priority.
Sets Read Receipt (Disposition-Notification-To header).
Sets "Reply-To" address.
Sets return path.
Sets the "sender" address. See RFC link below for full explanation.
Sets subject.
Sets "to" address.
TransferEncoding setter.
Unserializes the Message object.
Validate email address
Wrap the message to follow the RFC 2822 - 2.1.1
__construct(array<string, mixed>|null $config = null)
Constructor
array<string, mixed>|null $config optional Array of configs, or string to load configs from app.php
__serialize(): array
Magic method used for serializing the Message object.
array__unserialize(array $data): void
Magic method used to rebuild the Message object.
array $data Data array.
voidaddAttachments(array $attachments): $this
Add attachments
array $attachments Array of filenames.
$thisInvalidArgumentExceptionaddBcc(array|string $email, string|null $name = null): $this
Add "bcc" address.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thisaddCc(array|string $email, string|null $name = null): $this
Add "cc" address.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thisaddEmail(string $varName, array|string $email, string|null $name): $this
Add email
string $varName Property name
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name Name
$thisInvalidArgumentExceptionaddHeaders(array $headers): $this
Add header for the message
array $headers Headers to set.
$thisaddReplyTo(array|string $email, string|null $name = null): $this
Add "Reply-To" address.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thisaddTo(array|string $email, string|null $name = null): $this
Add "To" address.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thisattachFiles(string|null $boundary = null): array<string>
Attach non-embedded files by adding file contents inside boundaries.
string|null $boundary optional Boundary to use. If null, will default to $this->boundary
array<string>attachInlineFiles(string|null $boundary = null): array<string>
Attach inline/embedded files to the message.
string|null $boundary optional Boundary to use. If null, will default to $this->boundary
array<string>createBoundary(): void
Create unique boundary identifier
voidcreateFromArray(array<string, mixed> $config): $this
Configures an email instance object from serialized config.
array<string, mixed> $config Email configuration array.
$thisdecodeForHeader(string $text): string
Decode the specified string
string $text String to decode
stringencodeForHeader(string $text): string
Encode the specified string using the current charset
string $text String to encode
stringencodeString(string $text, string $charset): string
Translates a string for one charset to another if the App.encoding value differs and the mb_convert_encoding function exists
string $text The text to be converted
string $charset the target encoding
stringformatAddress(array $address): array
Format addresses
If the address contains non alphanumeric/whitespace characters, it will be quoted as characters like : and , are known to cause issues in address header fields.
array $address Addresses to format.
arraygenerateMessage(): array<string>
Generate full message.
array<string>getAttachments(): array<string, array>
Gets attachments to the email message.
array<string, array>getBcc(): array
Gets "bcc" address.
arraygetBody(): array
Get generated message body as array.
arraygetBodyHtml(): string
Get HTML body of message.
stringgetBodyString(string $eol = "\r\n"): string
Get generated body as string.
string $eol optional End of line string for imploding.
stringgetBodyText(): string
Get text body of message.
stringgetBodyTypes(): array
Gets the body types that are in this email message
arraygetCc(): array
Gets "cc" address.
arraygetCharset(): string
Charset getter.
stringgetContentTransferEncoding(): string
Return the Content-Transfer Encoding value based on the set transferEncoding or set charset.
stringgetContentTypeCharset(): string
Return charset value for Content-Type.
Checks fallback/compatibility types which include workarounds for legacy japanese character sets.
stringgetDomain(): string
Gets domain.
stringgetEmailFormat(): string
Gets email format.
stringgetEmailPattern(): string|null
EmailPattern setter/getter
string|nullgetFrom(): array
Gets "from" address.
arraygetHeaderCharset(): string
HeaderCharset getter.
stringgetHeaders(array<string> $include = []): array<string>
Get list of headers
fromreplyToreadReceiptreturnPathtoccbccsubjectarray<string> $include optional List of headers.
array<string>getHeadersString(array<string> $include = [], string $eol = "\r\n", Closure|null $callback = null): string
Get headers as string.
array<string> $include optional List of headers.
string $eol optional End of line string for concatenating headers.
Closure|null $callback optional Callback to run each header value through before stringifying.
stringgetMessageId(): string|bool
Gets message ID.
string|boolgetOriginalSubject(): string
Get original subject without encoding
stringgetPriority(): int|null
Gets priority.
int|nullgetReadReceipt(): array
Gets Read Receipt (Disposition-Notification-To header).
arraygetReplyTo(): array
Gets "Reply-To" address.
arraygetReturnPath(): array
Gets return path.
arraygetSender(): array
Gets the "sender" address. See RFC link below for full explanation.
arraygetSubject(): string
Gets subject.
stringgetTo(): array
Gets "to" address
arraygetTransferEncoding(): string|null
TransferEncoding getter.
string|nulljsonSerialize(): array
Serializes the email object to a value that can be natively serialized and re-used to clone this email instance.
arrayExceptionreadFile(Psr\Http\Message\UploadedFileInterface|string $file): string
Read the file contents and return a base64 version of the file contents.
Psr\Http\Message\UploadedFileInterface|string $file The absolute path to the file to read or UploadedFileInterface instance.
stringreset(): $this
Reset all the internal variables to be able to send out a new email.
$thisserialize(): string
Serializes the Email object.
stringsetAttachments(array $attachments): $this
Add attachments to the email message
Attachments can be defined in a few forms depending on how much control you need:
Attach a single file:
$this->setAttachments('path/to/file'); Attach a file with a different filename:
$this->setAttachments(['custom_name.txt' => 'path/to/file.txt']);
Attach a file and specify additional properties:
$this->setAttachments(['custom_name.png' => [
'file' => 'path/to/file',
'mimetype' => 'image/png',
'contentId' => 'abc123',
'contentDisposition' => false
]
]); Attach a file from string and specify additional properties:
$this->setAttachments(['custom_name.png' => [
'data' => file_get_contents('path/to/file'),
'mimetype' => 'image/png'
]
]); The contentId key allows you to specify an inline attachment. In your email text, you can use <img src="cid:abc123"/> to display the image inline.
The contentDisposition key allows you to disable the Content-Disposition header, this can improve attachment compatibility with outlook email clients.
array $attachments Array of filenames.
$thisInvalidArgumentExceptionsetBcc(array|string $email, string|null $name = null): $this
Sets "bcc" address.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thissetBody(array<string, string> $content): $this
Set message body.
array<string, string> $content Content array with keys "text" and/or "html" with content string of respective type.
$thissetBodyHtml(string $content): $this
Set HTML body for message.
string $content Content string
$thissetBodyText(string $content): $this
Set text body for message.
string $content Content string
$thissetCc(array|string $email, string|null $name = null): $this
Sets "cc" address.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thissetCharset(string $charset): $this
Charset setter.
string $charset Character set.
$thissetConfig(array<string, mixed> $config): $this
Sets the configuration for this instance.
array<string, mixed> $config Config array.
$thissetDomain(string $domain): $this
Sets domain.
Domain as top level (the part after @).
string $domain Manually set the domain for CLI mailing.
$thissetEmail(string $varName, array|string $email, string|null $name): $this
Set email
string $varName Property name
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name Name
$thisInvalidArgumentExceptionsetEmailFormat(string $format): $this
Sets email format.
string $format Formatting string.
$thisInvalidArgumentExceptionsetEmailPattern(string|null $regex): $this
EmailPattern setter/getter
string|null $regex The pattern to use for email address validation, null to unset the pattern and make use of filter_var() instead.
$thissetEmailSingle(string $varName, array|string $email, string|null $name, string $throwMessage): $this
Set only 1 email
string $varName Property name
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name Name
string $throwMessage Exception message
$thisInvalidArgumentExceptionsetFrom(array|string $email, string|null $name = null): $this
Sets "from" address.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thisInvalidArgumentExceptionsetHeaderCharset(string|null $charset): $this
HeaderCharset setter.
string|null $charset Character set.
$thissetHeaders(array $headers): $this
Sets headers for the message
array $headers Associative array containing headers to be set.
$thissetMessageId(string|bool $message): $this
Sets message ID.
string|bool $message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID.
$thisInvalidArgumentExceptionsetPriority(int|null $priority): $this
Sets priority.
int|null $priority 1 (highest) to 5 (lowest)
$thissetReadReceipt(array|string $email, string|null $name = null): $this
Sets Read Receipt (Disposition-Notification-To header).
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thisInvalidArgumentExceptionsetReplyTo(array|string $email, string|null $name = null): $this
Sets "Reply-To" address.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thisInvalidArgumentExceptionsetReturnPath(array|string $email, string|null $name = null): $this
Sets return path.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thisInvalidArgumentExceptionsetSender(array|string $email, string|null $name = null): $this
Sets the "sender" address. See RFC link below for full explanation.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thisInvalidArgumentExceptionsetSubject(string $subject): $this
Sets subject.
string $subject Subject string.
$thissetTo(array|string $email, string|null $name = null): $this
Sets "to" address.
array|string $email String with email, Array with email as key, name as value or email as value (without name)
string|null $name optional Name
$thissetTransferEncoding(string|null $encoding): $this
TransferEncoding setter.
string|null $encoding Encoding set.
$thisInvalidArgumentExceptionunserialize(string $data): void
Unserializes the Message object.
string $data Serialized string.
voidvalidateEmail(string $email, string $context): void
Validate email address
string $email Email address to validate
string $context Which property was set
voidInvalidArgumentExceptionwrap(string|null $message = null, int $wrapLength = self::LINE_LENGTH_MUST): array<string>
Wrap the message to follow the RFC 2822 - 2.1.1
string|null $message optional Message to wrap
int $wrapLength optional The line length
array<string>The application wide charset, used to encode headers and body
string|nullList of files that should be attached to the email.
Only absolute paths
array<string, array>Blind Carbon Copy
List of email's that should receive a copy of the email. The Recipient WILL NOT be able to see this list
arrayIf set, boundary to use for multipart mime messages
string|nullCarbon Copy
List of email's that should receive a copy of the email. The Recipient WILL be able to see this list
arrayCharset the email body is sent in
string8Bit character sets
array<string>Define Content-Type charset name
array<string, string>Domain for messageId generation. Needs to be manually set for CLI mailing as env('HTTP_HOST') is empty
stringWhat format should the email be sent in
stringAvailable formats to be sent.
array<string>Regex for email validation
If null, filter_var() will be used. Use the emailPattern() method to set a custom pattern.'
string|nullThe mail which the email is sent from
arrayCharset the email header is sent in If null, the $charset property will be used as default
string|nullAssociative array of a user defined headers Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
arrayHtml message
stringFinal message to send
arrayMessage ID
string|boolContains the optional priority of the email.
int|nullThe read receipt email
arrayList of email(s) that the recipient will reply to
arrayThe mail that will be used in case of any errors like
arrayThe sender email
arrayThe subject of the email
stringText message
stringRecipient of the email
arrayThe email transfer encoding used. If null, the $charset property is used for determined the transfer encoding.
string|nullAvailable encoding to be set for transfer.
array<string>
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.Mailer.Message.html