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'
int
998
int
78
string
'both'
string
'html'
string
'text'
string|null
The application wide charset, used to encode headers and body
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
8Bit character sets
array
Define Content-Type charset name
string
Domain for messageId generation.
string
What format should the email be sent in
array
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
bool|string
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.
array
Available encoding to be set for transfer.
Translates a string for one charset to another if the App.encoding value differs and the mb_convert_encoding function exists
Return the Content-Transfer Encoding value based on the set transferEncoding or set charset.
Serializes the email object to a value that can be natively serialized and re-used to clone this email instance.
__construct(?array $config)
Constructor
array|null
$config optional Array of configs, or string to load configs from app.php
addAttachments(array $attachments)
Add attachments
array
$attachments Array of filenames.
$this
InvalidArgumentException
addBcc(mixed $email, ?string $name)
Add "bcc" address.
string|array
$email Null to get, String with email, Array with email as key, name as value or email as value (without name)
string|null
$name optional Name
$this
addCc(mixed $email, ?string $name)
Add "cc" address.
string|array
$email Null to get, 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, mixed $email, ?string $name)
Add email
string
$varName Property name
string|array
$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)
Add header for the message
array
$headers Headers to set.
$this
addTo(mixed $email, ?string $name)
Add "To" address.
string|array
$email Null to get, 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 $boundary)
Attach non-embedded files by adding file contents inside boundaries.
string|null
$boundary optional Boundary to use. If null, will default to $this->boundary
string[]
An array of lines to add to the message
attachInlineFiles(?string $boundary)
Attach inline/embedded files to the message.
string|null
$boundary optional Boundary to use. If null, will default to $this->boundary
string[]
An array of lines to add to the message
createBoundary()
Create unique boundary identifier
createFromArray(array $config)
Configures an email instance object from serialized config.
array
$config Email configuration array.
$this
Configured email instance.
decodeForHeader(string $text)
Decode the specified string
string
$text String to decode
string
Decoded string
encodeForHeader(string $text)
Encode the specified string using the current charset
string
$text String to encode
string
Encoded string
encodeString(string $text, string $charset)
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)
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()
Generate full message.
string[]
getAttachments()
Gets attachments to the email message.
array
Array of attachments.
getBcc()
Gets "bcc" address.
array
getBody()
Get generated message body as array.
array
getBodyHtml()
Get HTML body of message.
string
getBodyString(string $eol)
Get generated body as string.
string
$eol optional End of line string for imploding.
string
getBodyText()
Get text body of message.
string
getBodyTypes()
Gets the body types that are in this email message
array
Array of types. Valid types are Email::MESSAGE_TEXT and Email::MESSAGE_HTML
getCc()
Gets "cc" address.
array
getCharset()
Charset getter.
string
Charset
getContentTransferEncoding()
Return the Content-Transfer Encoding value based on the set transferEncoding or set charset.
string
getContentTypeCharset()
Return charset value for Content-Type.
Checks fallback/compatibility types which include workarounds for legacy japanese character sets.
string
getDomain()
Gets domain.
string
getEmailFormat()
Gets email format.
string
getEmailPattern()
EmailPattern setter/getter
string|null
getFrom()
Gets "from" address.
array
getHeaderCharset()
HeaderCharset getter.
string
Charset
getHeaders(array $include)
Get list of headers
from
replyTo
readReceipt
returnPath
to
cc
bcc
subject
string[]
$include optional List of headers.
string[]
getHeadersString(array $include, string $eol, ?\Closure $callback)
Get headers as string.
string[]
$include optional List of headers.
string
$eol optional End of line string for concatenating headers.
\Closure
$callback optional Callback to run each header value through before stringifying.
string
getMessageId()
Gets message ID.
bool|string
getOriginalSubject()
Get original subject without encoding
string
Original subject
getPriority()
Gets priority.
int|null
getReadReceipt()
Gets Read Receipt (Disposition-Notification-To header).
array
getReplyTo()
Gets "Reply-To" address.
array
getReturnPath()
Gets return path.
array
getSender()
Gets the "sender" address. See RFC link below for full explanation.
array
getSubject()
Gets subject.
string
getTo()
Gets "to" address
array
getTransferEncoding()
TransferEncoding getter.
string|null
Encoding
jsonSerialize()
Serializes the email object to a value that can be natively serialized and re-used to clone this email instance.
array
Serializable array of configuration properties.
Exception
readFile(mixed $file)
Read the file contents and return a base64 version of the file contents.
string|\Psr\Http\Message\UploadedFileInterface
$file The absolute path to the file to read or UploadedFileInterface instance.
string
File contents in base64 encoding
reset()
Reset all the internal variables to be able to send out a new email.
$this
serialize()
Serializes the Email object.
string
setAttachments(array $attachments)
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(mixed $email, ?string $name)
Sets "bcc" address.
string|array
$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 $content)
Set message body.
array
$content Content array with keys "text" and/or "html" with content string of respective type.
$this
setBodyHtml(string $content)
Set HTML body for message.
string
$content Content string
$this
setBodyText(string $content)
Set text body for message.
string
$content Content string
$this
setCc(mixed $email, ?string $name)
Sets "cc" address.
string|array
$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)
Charset setter.
string
$charset Character set.
$this
setConfig(array $config)
Sets the configuration for this instance.
array
$config Config array.
$this
setDomain(string $domain)
Sets domain.
Domain as top level (the part after @).
string
$domain Manually set the domain for CLI mailing.
$this
setEmail(string $varName, mixed $email, ?string $name)
Set email
string
$varName Property name
string|array
$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)
Sets email format.
string
$format Formatting string.
$this
InvalidArgumentException
setEmailPattern(?string $regex)
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, mixed $email, ?string $name, string $throwMessage)
Set only 1 email
string
$varName Property name
string|array
$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(mixed $email, ?string $name)
Sets "from" address.
string|array
$email Null to get, 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 $charset)
HeaderCharset setter.
string|null
$charset Character set.
$this
setHeaders(array $headers)
Sets headers for the message
array
$headers Associative array containing headers to be set.
$this
setMessageId(mixed $message)
Sets message ID.
bool|string
$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 $priority)
Sets priority.
int|null
$priority 1 (highest) to 5 (lowest)
$this
setReadReceipt(mixed $email, ?string $name)
Sets Read Receipt (Disposition-Notification-To header).
string|array
$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(mixed $email, ?string $name)
Sets "Reply-To" address.
string|array
$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(mixed $email, ?string $name)
Sets return path.
string|array
$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(mixed $email, ?string $name)
Sets the "sender" address. See RFC link below for full explanation.
string|array
$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)
Sets subject.
string
$subject Subject string.
$this
setTo(mixed $email, ?string $name)
Sets "to" address.
string|array
$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 $encoding)
TransferEncoding setter.
string|null
$encoding Encoding set.
$this
InvalidArgumentException
unserialize(mixed $data)
Unserializes the Email object.
string
$data Serialized string.
validateEmail(string $email, string $context)
Validate email address
string
$email Email address to validate
string
$context Which property was set
InvalidArgumentException
wrap(?string $message, int $wrapLength)
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
Wrapped message
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
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
Define Content-Type charset name
array
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
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
bool|string
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
© 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.1/class-Cake.Mailer.Message.html