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|null
The application wide charset, used to encode headers and body
array<string, array>
List of files that should be attached to the email.
array
Blind Carbon Copy
string|null
If set, boundary to use for multipart mime messages
array
Carbon Copy
string
Charset the email body is sent in
array<string>
8Bit character sets
array<string, string>
Define Content-Type charset name
string
Domain for messageId generation. Needs to be manually set for CLI mailing as env('HTTP_HOST') is empty
string
What format should the email be sent in
array<string>
Available formats to be sent.
string|null
Regex for email validation
array
The mail which the email is sent from
string|null
Charset the email header is sent in If null, the $charset property will be used as default
array
Associative array of a user defined headers Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
string
Html message
array
Final message to send
string|bool
Message ID
int|null
Contains the optional priority of the email.
array
The read receipt email
array
List of email(s) that the recipient will reply to
array
The mail that will be used in case of any errors like
array
The sender email
string
The subject of the email
string
Text message
array
Recipient of the email
string|null
The 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.
void
addAttachments(array $attachments): $this
Add attachments
array
$attachments Array of filenames.
$this
InvalidArgumentException
addBcc(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
$this
addCc(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
$this
addEmail(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
$this
InvalidArgumentException
addHeaders(array $headers): $this
Add header for the message
array
$headers Headers to set.
$this
addReplyTo(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
$this
addTo(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
$this
attachFiles(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
void
createFromArray(array<string, mixed> $config): $this
Configures an email instance object from serialized config.
array<string, mixed>
$config Email configuration array.
$this
decodeForHeader(string $text): string
Decode the specified string
string
$text String to decode
string
encodeForHeader(string $text): string
Encode the specified string using the current charset
string
$text String to encode
string
encodeString(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
string
formatAddress(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.
array
generateMessage(): 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.
array
getBody(): array
Get generated message body as array.
array
getBodyHtml(): string
Get HTML body of message.
string
getBodyString(string $eol = "\r\n"): string
Get generated body as string.
string
$eol optional End of line string for imploding.
string
getBodyText(): string
Get text body of message.
string
getBodyTypes(): array
Gets the body types that are in this email message
array
getCc(): array
Gets "cc" address.
array
getCharset(): string
Charset getter.
string
getContentTransferEncoding(): string
Return the Content-Transfer Encoding value based on the set transferEncoding or set charset.
string
getContentTypeCharset(): string
Return charset value for Content-Type.
Checks fallback/compatibility types which include workarounds for legacy japanese character sets.
string
getDomain(): string
Gets domain.
string
getEmailFormat(): string
Gets email format.
string
getEmailPattern(): string|null
EmailPattern setter/getter
string|null
getFrom(): array
Gets "from" address.
array
getHeaderCharset(): string
HeaderCharset getter.
string
getHeaders(array<string> $include = []): array<string>
Get list of headers
from
replyTo
readReceipt
returnPath
to
cc
bcc
subject
array<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.
string
getMessageId(): string|bool
Gets message ID.
string|bool
getOriginalSubject(): string
Get original subject without encoding
string
getPriority(): int|null
Gets priority.
int|null
getReadReceipt(): array
Gets Read Receipt (Disposition-Notification-To header).
array
getReplyTo(): array
Gets "Reply-To" address.
array
getReturnPath(): array
Gets return path.
array
getSender(): array
Gets the "sender" address. See RFC link below for full explanation.
array
getSubject(): string
Gets subject.
string
getTo(): array
Gets "to" address
array
getTransferEncoding(): string|null
TransferEncoding getter.
string|null
jsonSerialize(): array
Serializes the email object to a value that can be natively serialized and re-used to clone this email instance.
array
Exception
readFile(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.
string
reset(): $this
Reset all the internal variables to be able to send out a new email.
$this
serialize(): string
Serializes the Email object.
string
setAttachments(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.
$this
InvalidArgumentException
setBcc(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
$this
setBody(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.
$this
setBodyHtml(string $content): $this
Set HTML body for message.
string
$content Content string
$this
setBodyText(string $content): $this
Set text body for message.
string
$content Content string
$this
setCc(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
$this
setCharset(string $charset): $this
Charset setter.
string
$charset Character set.
$this
setConfig(array<string, mixed> $config): $this
Sets the configuration for this instance.
array<string, mixed>
$config Config array.
$this
setDomain(string $domain): $this
Sets domain.
Domain as top level (the part after @).
string
$domain Manually set the domain for CLI mailing.
$this
setEmail(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
$this
InvalidArgumentException
setEmailFormat(string $format): $this
Sets email format.
string
$format Formatting string.
$this
InvalidArgumentException
setEmailPattern(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.
$this
setEmailSingle(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
$this
InvalidArgumentException
setFrom(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
$this
InvalidArgumentException
setHeaderCharset(string|null $charset): $this
HeaderCharset setter.
string|null
$charset Character set.
$this
setHeaders(array $headers): $this
Sets headers for the message
array
$headers Associative array containing headers to be set.
$this
setMessageId(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.
$this
InvalidArgumentException
setPriority(int|null $priority): $this
Sets priority.
int|null
$priority 1 (highest) to 5 (lowest)
$this
setReadReceipt(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
$this
InvalidArgumentException
setReplyTo(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
$this
InvalidArgumentException
setReturnPath(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
$this
InvalidArgumentException
setSender(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
$this
InvalidArgumentException
setSubject(string $subject): $this
Sets subject.
string
$subject Subject string.
$this
setTo(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
$this
setTransferEncoding(string|null $encoding): $this
TransferEncoding setter.
string|null
$encoding Encoding set.
$this
InvalidArgumentException
unserialize(string $data): void
Unserializes the Message object.
string
$data Serialized string.
void
validateEmail(string $email, string $context): void
Validate email address
string
$email Email address to validate
string
$context Which property was set
void
InvalidArgumentException
wrap(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|null
List 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
array
If set, boundary to use for multipart mime messages
string|null
Carbon Copy
List of email's that should receive a copy of the email. The Recipient WILL be able to see this list
array
Charset the email body is sent in
string
8Bit 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
string
What format should the email be sent in
string
Available 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|null
The mail which the email is sent from
array
Charset the email header is sent in If null, the $charset property will be used as default
string|null
Associative array of a user defined headers Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
array
Html message
string
Final message to send
array
Message ID
string|bool
Contains the optional priority of the email.
int|null
The read receipt email
array
List of email(s) that the recipient will reply to
array
The mail that will be used in case of any errors like
array
The sender email
array
The subject of the email
string
Text message
string
Recipient of the email
array
The email transfer encoding used. If null, the $charset property is used for determined the transfer encoding.
string|null
Available 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