Class Message
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
Constants summary
-
stringEMAIL_PATTERN'/^((?:[\\p{L}0-9.!#$%&\'*+\\/=?^_`{|}~-]+)*@[\\p{L}0-9-._]+)$/ui' -
intLINE_LENGTH_MUST998 -
intLINE_LENGTH_SHOULD78 -
stringMESSAGE_BOTH'both' -
stringMESSAGE_HTML'html' -
stringMESSAGE_TEXT'text'
Properties summary
- $appCharset protected
string|nullThe application wide charset, used to encode headers and body
- $attachments protected
arrayList of files that should be attached to the email.
- $bcc protected
arrayBlind Carbon Copy
- $boundary protected
string|nullIf set, boundary to use for multipart mime messages
- $cc protected
arrayCarbon Copy
- $charset protected
stringCharset the email body is sent in
- $charset8bit protected
array8Bit character sets
- $contentTypeCharset protected
arrayDefine Content-Type charset name
- $domain protected
stringDomain for messageId generation.
- $emailFormat protected
stringWhat format should the email be sent in
- $emailFormatAvailable protected
arrayAvailable formats to be sent.
- $emailPattern protected
string|nullRegex for email validation
- $from protected
arrayThe mail which the email is sent from
- $headerCharset protected
string|nullCharset the email header is sent in If null, the $charset property will be used as default
- $headers protected
arrayAssociative array of a user defined headers Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
- $htmlMessage protected
stringHtml message
- $message protected
arrayFinal message to send
- $messageId protected
bool|stringMessage ID
- $priority protected
int|nullContains the optional priority of the email.
- $readReceipt protected
arrayThe read receipt email
- $replyTo protected
arrayList of email(s) that the recipient will reply to
- $returnPath protected
arrayThe mail that will be used in case of any errors like
- Remote mailserver down
- Remote user has exceeded his quota
- Unknown user
- $sender protected
arrayThe sender email
- $subject protected
stringThe subject of the email
- $textMessage protected
stringText message
- $to protected
arrayRecipient of the email
- $transferEncoding protected
string|nullThe email transfer encoding used.
- $transferEncodingAvailable protected
arrayAvailable encoding to be set for transfer.
Method Summary
- encodeString() protected
Translates a string for one charset to another if the App.encoding value differs and the mb_convert_encoding function exists
- getContentTransferEncoding() public
Return the Content-Transfer Encoding value based on the set transferEncoding or set charset.
- jsonSerialize() public
Serializes the email object to a value that can be natively serialized and re-used to clone this email instance.
Method Detail
__construct() public
__construct(?array $config)
Constructor
Parameters
-
array|null$config optional Array of configs, or string to load configs from app.php
addAttachments() public
addAttachments(array $attachments)
Add attachments
Parameters
-
array$attachments Array of filenames.
Returns
$thisThrows
InvalidArgumentExceptionSee Also
addBcc() public
addBcc(mixed $email, ?string $name)
Add "bcc" address.
Parameters
-
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
Returns
$thisaddCc() public
addCc(mixed $email, ?string $name)
Add "cc" address.
Parameters
-
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
Returns
$thisaddEmail() protected
addEmail(string $varName, mixed $email, ?string $name)
Add email
Parameters
-
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
Returns
$thisThrows
InvalidArgumentExceptionaddHeaders() public
addHeaders(array $headers)
Add header for the message
Parameters
-
array$headers Headers to set.
Returns
$thisaddTo() public
addTo(mixed $email, ?string $name)
Add "To" address.
Parameters
-
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
Returns
$thisattachFiles() protected
attachFiles(?string $boundary)
Attach non-embedded files by adding file contents inside boundaries.
Parameters
-
string|null$boundary optional Boundary to use. If null, will default to $this->boundary
Returns
string[]An array of lines to add to the message
attachInlineFiles() protected
attachInlineFiles(?string $boundary)
Attach inline/embedded files to the message.
Parameters
-
string|null$boundary optional Boundary to use. If null, will default to $this->boundary
Returns
string[]An array of lines to add to the message
createBoundary() protected
createBoundary()
Create unique boundary identifier
createFromArray() public
createFromArray(array $config)
Configures an email instance object from serialized config.
Parameters
-
array$config Email configuration array.
Returns
$thisConfigured email instance.
decodeForHeader() protected
decodeForHeader(string $text)
Decode the specified string
Parameters
-
string$text String to decode
Returns
stringDecoded string
encodeForHeader() protected
encodeForHeader(string $text)
Encode the specified string using the current charset
Parameters
-
string$text String to encode
Returns
stringEncoded string
encodeString() protected
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
Parameters
-
string$text The text to be converted
-
string$charset the target encoding
Returns
stringformatAddress() protected
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.
Parameters
-
array$address Addresses to format.
Returns
arraygenerateMessage() protected
generateMessage()
Generate full message.
Returns
string[]getAttachments() public
getAttachments()
Gets attachments to the email message.
Returns
arrayArray of attachments.
getBcc() public
getBcc()
Gets "bcc" address.
Returns
arraygetBody() public
getBody()
Get generated message body as array.
Returns
arraygetBodyHtml() public
getBodyHtml()
Get HTML body of message.
Returns
stringgetBodyString() public
getBodyString(string $eol)
Get generated body as string.
Parameters
-
string$eol optional End of line string for imploding.
Returns
stringSee Also
getBodyText() public
getBodyText()
Get text body of message.
Returns
stringgetBodyTypes() public
getBodyTypes()
Gets the body types that are in this email message
Returns
arrayArray of types. Valid types are Email::MESSAGE_TEXT and Email::MESSAGE_HTML
getCc() public
getCc()
Gets "cc" address.
Returns
arraygetCharset() public
getCharset()
Charset getter.
Returns
stringCharset
getContentTransferEncoding() public
getContentTransferEncoding()
Return the Content-Transfer Encoding value based on the set transferEncoding or set charset.
Returns
stringgetContentTypeCharset() public
getContentTypeCharset()
Return charset value for Content-Type.
Checks fallback/compatibility types which include workarounds for legacy japanese character sets.
Returns
stringgetDomain() public
getDomain()
Gets domain.
Returns
stringgetEmailFormat() public
getEmailFormat()
Gets email format.
Returns
stringgetEmailPattern() public
getEmailPattern()
EmailPattern setter/getter
Returns
string|nullgetFrom() public
getFrom()
Gets "from" address.
Returns
arraygetHeaderCharset() public
getHeaderCharset()
HeaderCharset getter.
Returns
stringCharset
getHeaders() public
getHeaders(array $include)
Get list of headers
Includes:
fromreplyToreadReceiptreturnPathtoccbccsubject
Parameters
-
string[]$include optional List of headers.
Returns
string[]getHeadersString() public
getHeadersString(array $include, string $eol, ?\Closure $callback)
Get headers as string.
Parameters
-
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.
Returns
stringSee Also
getMessageId() public
getMessageId()
Gets message ID.
Returns
bool|stringgetOriginalSubject() public
getOriginalSubject()
Get original subject without encoding
Returns
stringOriginal subject
getPriority() public
getPriority()
Gets priority.
Returns
int|nullgetReadReceipt() public
getReadReceipt()
Gets Read Receipt (Disposition-Notification-To header).
Returns
arraygetReplyTo() public
getReplyTo()
Gets "Reply-To" address.
Returns
arraygetReturnPath() public
getReturnPath()
Gets return path.
Returns
arraygetSender() public
getSender()
Gets the "sender" address. See RFC link below for full explanation.
Returns
arrayLinks
getSubject() public
getSubject()
Gets subject.
Returns
stringgetTo() public
getTo()
Gets "to" address
Returns
arraygetTransferEncoding() public
getTransferEncoding()
TransferEncoding getter.
Returns
string|nullEncoding
jsonSerialize() public
jsonSerialize()
Serializes the email object to a value that can be natively serialized and re-used to clone this email instance.
Returns
arraySerializable array of configuration properties.
Throws
ExceptionWhen a view var object can not be properly serialized.
readFile() protected
readFile(mixed $file)
Read the file contents and return a base64 version of the file contents.
Parameters
-
string|\Psr\Http\Message\UploadedFileInterface$file The absolute path to the file to read or UploadedFileInterface instance.
Returns
stringFile contents in base64 encoding
reset() public
reset()
Reset all the internal variables to be able to send out a new email.
Returns
$thisserialize() public
serialize()
Serializes the Email object.
Returns
stringsetAttachments() public
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.
Parameters
-
array$attachments Array of filenames.
Returns
$thisThrows
InvalidArgumentExceptionsetBcc() public
setBcc(mixed $email, ?string $name)
Sets "bcc" address.
Parameters
-
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
Returns
$thissetBody() public
setBody(array $content)
Set message body.
Parameters
-
array$content Content array with keys "text" and/or "html" with content string of respective type.
Returns
$thissetBodyHtml() public
setBodyHtml(string $content)
Set HTML body for message.
Parameters
-
string$content Content string
Returns
$thissetBodyText() public
setBodyText(string $content)
Set text body for message.
Parameters
-
string$content Content string
Returns
$thissetCc() public
setCc(mixed $email, ?string $name)
Sets "cc" address.
Parameters
-
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
Returns
$thissetCharset() public
setCharset(string $charset)
Charset setter.
Parameters
-
string$charset Character set.
Returns
$thissetConfig() public
setConfig(array $config)
Sets the configuration for this instance.
Parameters
-
array$config Config array.
Returns
$thissetDomain() public
setDomain(string $domain)
Sets domain.
Domain as top level (the part after @).
Parameters
-
string$domain Manually set the domain for CLI mailing.
Returns
$thissetEmail() protected
setEmail(string $varName, mixed $email, ?string $name)
Set email
Parameters
-
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
Returns
$thisThrows
InvalidArgumentExceptionsetEmailFormat() public
setEmailFormat(string $format)
Sets email format.
Parameters
-
string$format Formatting string.
Returns
$thisThrows
InvalidArgumentExceptionsetEmailPattern() public
setEmailPattern(?string $regex)
EmailPattern setter/getter
Parameters
-
string|null$regex The pattern to use for email address validation, null to unset the pattern and make use of filter_var() instead.
Returns
$thissetEmailSingle() protected
setEmailSingle(string $varName, mixed $email, ?string $name, string $throwMessage)
Set only 1 email
Parameters
-
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
Returns
$thisThrows
InvalidArgumentExceptionsetFrom() public
setFrom(mixed $email, ?string $name)
Sets "from" address.
Parameters
-
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
Returns
$thisThrows
InvalidArgumentExceptionsetHeaderCharset() public
setHeaderCharset(?string $charset)
HeaderCharset setter.
Parameters
-
string|null$charset Character set.
Returns
$thissetHeaders() public
setHeaders(array $headers)
Sets headers for the message
Parameters
-
array$headers Associative array containing headers to be set.
Returns
$thissetMessageId() public
setMessageId(mixed $message)
Sets message ID.
Parameters
-
bool|string$message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID.
Returns
$thisThrows
InvalidArgumentExceptionsetPriority() public
setPriority(?int $priority)
Sets priority.
Parameters
-
int|null$priority 1 (highest) to 5 (lowest)
Returns
$thissetReadReceipt() public
setReadReceipt(mixed $email, ?string $name)
Sets Read Receipt (Disposition-Notification-To header).
Parameters
-
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
Returns
$thisThrows
InvalidArgumentExceptionsetReplyTo() public
setReplyTo(mixed $email, ?string $name)
Sets "Reply-To" address.
Parameters
-
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
Returns
$thisThrows
InvalidArgumentExceptionsetReturnPath() public
setReturnPath(mixed $email, ?string $name)
Sets return path.
Parameters
-
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
Returns
$thisThrows
InvalidArgumentExceptionsetSender() public
setSender(mixed $email, ?string $name)
Sets the "sender" address. See RFC link below for full explanation.
Parameters
-
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
Returns
$thisThrows
InvalidArgumentExceptionLinks
setSubject() public
setSubject(string $subject)
Sets subject.
Parameters
-
string$subject Subject string.
Returns
$thissetTo() public
setTo(mixed $email, ?string $name)
Sets "to" address.
Parameters
-
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
Returns
$thissetTransferEncoding() public
setTransferEncoding(?string $encoding)
TransferEncoding setter.
Parameters
-
string|null$encoding Encoding set.
Returns
$thisThrows
InvalidArgumentExceptionunserialize() public
unserialize(mixed $data)
Unserializes the Email object.
Parameters
-
string$data Serialized string.
validateEmail() protected
validateEmail(string $email, string $context)
Validate email address
Parameters
-
string$email Email address to validate
-
string$context Which property was set
Throws
InvalidArgumentExceptionIf email address does not validate
wrap() protected
wrap(?string $message, int $wrapLength)
Wrap the message to follow the RFC 2822 - 2.1.1
Parameters
-
string|null$message optional Message to wrap
-
int$wrapLength optional The line length
Returns
arrayWrapped message
Property Detail
$appCharset protected
The application wide charset, used to encode headers and body
Type
string|null$attachments protected
List of files that should be attached to the email.
Only absolute paths
Type
array$bcc protected
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
Type
array$boundary protected
If set, boundary to use for multipart mime messages
Type
string|null$cc protected
Carbon Copy
List of email's that should receive a copy of the email. The Recipient WILL be able to see this list
Type
array$charset protected
Charset the email body is sent in
Type
string$charset8bit protected
8Bit character sets
Type
array$contentTypeCharset protected
Define Content-Type charset name
Type
array$domain protected
Domain for messageId generation.
Needs to be manually set for CLI mailing as env('HTTP_HOST') is empty
Type
string$emailFormat protected
What format should the email be sent in
Type
string$emailFormatAvailable protected
Available formats to be sent.
Type
array$emailPattern protected
Regex for email validation
If null, filter_var() will be used. Use the emailPattern() method to set a custom pattern.'
Type
string|null$from protected
The mail which the email is sent from
Type
array$headerCharset protected
Charset the email header is sent in If null, the $charset property will be used as default
Type
string|null$headers protected
Associative array of a user defined headers Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
Type
array$htmlMessage protected
Html message
Type
string$message protected
Final message to send
Type
array$messageId protected
Message ID
Type
bool|string$priority protected
Contains the optional priority of the email.
Type
int|null$readReceipt protected
The read receipt email
Type
array$replyTo protected
List of email(s) that the recipient will reply to
Type
array$returnPath protected
The mail that will be used in case of any errors like
- Remote mailserver down
- Remote user has exceeded his quota
- Unknown user
Type
array$sender protected
The sender email
Type
array$subject protected
The subject of the email
Type
string$textMessage protected
Text message
Type
string$to protected
Recipient of the email
Type
array$transferEncoding protected
The email transfer encoding used.
If null, the $charset property is used for determined the transfer encoding.
Type
string|null$transferEncodingAvailable protected
Available encoding to be set for transfer.
Type
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