\Cake\MailerEmail

CakePHP Email class.

This class is used for sending Internet Message Format based on the standard outlined in https://www.rfc-editor.org/rfc/rfc2822.txt

Configuration

Configuration for Email is managed by Email::config() and Email::configTransport(). Email::config() can be used to add or read a configuration profile for Email instances. Once made configuration profiles can be used to re-use across various email messages your application sends.

Summary

Methods
Properties
Constants
setConfig()
getConfig()
config()
drop()
configured()
parseDsn()
setDsnClassMap()
getDsnClassMap()
dsnClassMap()
viewBuilder()
createView()
set()
viewOptions()
__construct()
__clone()
setFrom()
getFrom()
from()
setSender()
getSender()
sender()
setReplyTo()
getReplyTo()
replyTo()
setReadReceipt()
getReadReceipt()
readReceipt()
setReturnPath()
getReturnPath()
returnPath()
setTo()
getTo()
to()
addTo()
setCc()
getCc()
cc()
addCc()
setBcc()
getBcc()
bcc()
addBcc()
setCharset()
getCharset()
charset()
setHeaderCharset()
getHeaderCharset()
headerCharset()
setTransferEncoding()
getTransferEncoding()
setEmailPattern()
getEmailPattern()
emailPattern()
setSubject()
getSubject()
subject()
getOriginalSubject()
setHeaders()
addHeaders()
getHeaders()
setTemplate()
getTemplate()
setLayout()
getLayout()
template()
setViewRenderer()
getViewRenderer()
viewRender()
setViewVars()
getViewVars()
viewVars()
setTheme()
getTheme()
theme()
setHelpers()
getHelpers()
helpers()
setEmailFormat()
getEmailFormat()
emailFormat()
setTransport()
getTransport()
transport()
setMessageId()
getMessageId()
messageId()
setDomain()
getDomain()
domain()
setAttachments()
getAttachments()
attachments()
addAttachments()
message()
setPriority()
getPriority()
setConfigTransport()
getConfigTransport()
configTransport()
configuredTransport()
dropTransport()
setProfile()
getProfile()
profile()
send()
deliver()
reset()
jsonSerialize()
createFromArray()
serialize()
unserialize()
$viewClass
$viewVars
$charset
$headerCharset
LINE_LENGTH_SHOULD
LINE_LENGTH_MUST
MESSAGE_HTML
MESSAGE_TEXT
EMAIL_PATTERN
_setEmail()
_validateEmail()
_setEmailSingle()
_addEmail()
_formatAddress()
_logDelivery()
flatten()
_applyConfig()
_encode()
_decode()
_encodeString()
_wrap()
_createBoundary()
_attachFiles()
_readFile()
_attachInlineFiles()
_render()
_getTypes()
_renderTemplates()
_getContentTransferEncoding()
_getContentTypeCharset()
_checkViewVars()
$_config
$_viewBuilder
$_to
$_from
$_sender
$_replyTo
$_readReceipt
$_returnPath
$_cc
$_bcc
$_messageId
$_domain
$_subject
$_headers
$_textMessage
$_htmlMessage
$_message
$_emailFormatAvailable
$_emailFormat
$_transport
$transferEncoding
$_transferEncodingAvailable
$_appCharset
$_attachments
$_boundary
$_priority
$_dsnClassMap
$_profile
$_charset8bit
$_contentTypeCharset
$_emailPattern
N/A
No private methods found
No private properties found
N/A

Constants

LINE_LENGTH_SHOULD

LINE_LENGTH_SHOULD = 78 : integer

Line length - no should more - RFC 2822 - 2.1.1

LINE_LENGTH_MUST

LINE_LENGTH_MUST = 998 : integer

Line length - no must more - RFC 2822 - 2.1.1

MESSAGE_HTML

MESSAGE_HTML = 'html' : string

Type of message - HTML

MESSAGE_TEXT

MESSAGE_TEXT = 'text' : string

Type of message - TEXT

EMAIL_PATTERN

EMAIL_PATTERN = '/^((?:[\p{L}0-9.!#$%&\'*+\/=?^_`{|}~-]+)*@[\p{L}0-9-._]+)$/ui' : string

Holds the regex pattern for email validation

Properties

$viewClass

$viewClass : string|null

The name of default View class.

Type

string|null

$viewVars

$viewVars : array

Variables for the view

Type

array

$charset

$charset : string

Charset the email body is sent in

Type

string

$headerCharset

$headerCharset : string|null

Charset the email header is sent in If null, the $charset property will be used as default

Type

string|null

$_config

$_config : array

Configuration sets.

Type

array

$_viewBuilder

$_viewBuilder : \Cake\View\ViewBuilder

The view builder instance being used.

Type

\Cake\View\ViewBuilder

$_to

$_to : array

Recipient of the email

Type

array

$_from

$_from : array

The mail which the email is sent from

Type

array

$_sender

$_sender : array

The sender email

Type

array

$_replyTo

$_replyTo : array

The email the recipient will reply to

Type

array

$_readReceipt

$_readReceipt : array

The read receipt email

Type

array

$_returnPath

$_returnPath : array

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

$_cc

$_cc : array

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

$_bcc

$_bcc : 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

Type

array

$_messageId

$_messageId : boolean|string

Message ID

Type

boolean|string

$_domain

$_domain : string

Domain for messageId generation.

Needs to be manually set for CLI mailing as env('HTTP_HOST') is empty

Type

string

$_subject

$_subject : string

The subject of the email

Type

string

$_headers

$_headers : array

Associative array of a user defined headers Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5

Type

array

$_textMessage

$_textMessage : string

Text message

Type

string

$_htmlMessage

$_htmlMessage : string

Html message

Type

string

$_message

$_message : array

Final message to send

Type

array

$_emailFormatAvailable

$_emailFormatAvailable : array

Available formats to be sent.

Type

array

$_emailFormat

$_emailFormat : string

What format should the email be sent in

Type

string

$_transport

$_transport : \Cake\Mailer\AbstractTransport|null

The transport instance to use for sending mail.

Type

\Cake\Mailer\AbstractTransport|null

$transferEncoding

$transferEncoding : string|null

The email transfer encoding used.

If null, the $charset property is used for determined the transfer encoding.

Type

string|null

$_transferEncodingAvailable

$_transferEncodingAvailable : array

Available encoding to be set for transfer.

Type

array

$_appCharset

$_appCharset : string|null

The application wide charset, used to encode headers and body

Type

string|null

$_attachments

$_attachments : array

List of files that should be attached to the email.

Only absolute paths

Type

array

$_boundary

$_boundary : string|null

If set, boundary to use for multipart mime messages

Type

string|null

$_priority

$_priority : integer|null

Contains the optional priority of the email.

Type

integer|null

$_dsnClassMap

$_dsnClassMap : array

An array mapping url schemes to fully qualified Transport class names.

Unused.

Type

array

$_profile

$_profile : array

A copy of the configuration profile for this instance. This copy can be modified with Email::profile().

Type

array

$_charset8bit

$_charset8bit : array

8Bit character sets

Type

array

$_contentTypeCharset

$_contentTypeCharset : array

Define Content-Type charset name

Type

array

$_emailPattern

$_emailPattern : string

Regex for email validation

If null, filter_var() will be used. Use the emailPattern() method to set a custom pattern.'

Type

string

Methods

setConfig()

setConfig(string|array  $key, array  $config = null) : void

This method can be used to define configuration adapters for an application.

To change an adapter's configuration at runtime, first drop the adapter and then reconfigure it.

Adapters will not be constructed until the first operation is done.

Usage

Assuming that the class' name is Cache the following scenarios are supported:

Setting a cache engine up.

Cache::setConfig('default', $settings);

Injecting a constructed adapter in:

Cache::setConfig('default', $instance);

Configure multiple adapters at once:

Cache::setConfig($arrayOfConfig);

Parameters

string|array $key

The name of the configuration, or an array of multiple configs.

array $config

An array of name => configuration data for adapter.

Throws

\BadMethodCallException

When trying to modify an existing config.

\LogicException

When trying to store an invalid structured config array.

getConfig()

getConfig(string  $key) : array|null

Reads existing configuration.

Parameters

string $key

The name of the configuration.

Returns

array|null —

Array of configuration data.

config()

config(string|array  $key, array|null  $config = null) : array|null

This method can be used to define configuration adapters for an application or read existing configuration.

To change an adapter's configuration at runtime, first drop the adapter and then reconfigure it.

Adapters will not be constructed until the first operation is done.

Usage

Assuming that the class' name is Cache the following scenarios are supported:

Reading config data back:

Cache::config('default');

Setting a cache engine up.

Cache::config('default', $settings);

Injecting a constructed adapter in:

Cache::config('default', $instance);

Configure multiple adapters at once:

Cache::config($arrayOfConfig);

Parameters

string|array $key

The name of the configuration, or an array of multiple configs.

array|null $config

An array of name => configuration data for adapter.

Throws

\BadMethodCallException

When trying to modify an existing config.

Returns

array|null —

Null when adding configuration or an array of configuration data when reading.

drop()

drop(string  $config) : boolean

Drops a constructed adapter.

If you wish to modify an existing configuration, you should drop it, change configuration and then re-add it.

If the implementing objects supports a $_registry object the named configuration will also be unloaded from the registry.

Parameters

string $config

An existing configuration you wish to remove.

Returns

boolean —

Success of the removal, returns false when the config does not exist.

configured()

configured() : array<mixed,string>

Returns an array containing the named configurations

Returns

array<mixed,string> —

Array of configurations.

parseDsn()

parseDsn(string  $dsn) : array

Parses a DSN into a valid connection configuration

This method allows setting a DSN using formatting similar to that used by PEAR::DB. The following is an example of its usage:

$dsn = 'mysql://user:pass@localhost/database?';
$config = ConnectionManager::parseDsn($dsn);

$dsn = 'Cake\Log\Engine\FileLog://?types=notice,info,debug&file=debug&path=LOGS';
$config = Log::parseDsn($dsn);

$dsn = 'smtp://user:secret@localhost:25?timeout=30&client=null&tls=null';
$config = Email::parseDsn($dsn);

$dsn = 'file:///?className=\My\Cache\Engine\FileEngine';
$config = Cache::parseDsn($dsn);

$dsn = 'File://?prefix=myapp_cake_core_&serialize=true&duration=+2 minutes&path=/tmp/persistent/';
$config = Cache::parseDsn($dsn);

For all classes, the value of scheme is set as the value of both the className unless they have been otherwise specified.

Note that querystring arguments are also parsed and set as values in the returned configuration.

Parameters

string $dsn

The DSN string to convert to a configuration array

Throws

\InvalidArgumentException

If not passed a string, or passed an invalid string

Returns

array —

The configuration array to be stored after parsing the DSN

setDsnClassMap()

setDsnClassMap(array  $map) : void

Updates the DSN class map for this class.

Parameters

array $map

Additions/edits to the class map to apply.

getDsnClassMap()

getDsnClassMap() : array

Returns the DSN class map for this class.

Returns

array

dsnClassMap()

dsnClassMap(array|null  $map = null) : array

Returns or updates the DSN class map for this class.

Parameters

array|null $map

Additions/edits to the class map to apply.

Returns

array

viewBuilder()

viewBuilder() : \Cake\View\ViewBuilder

Get the view builder being used.

Returns

\Cake\View\ViewBuilder

createView()

createView(string|null  $viewClass = null) : \Cake\View\View

Constructs the view class instance based on the current configuration.

Parameters

string|null $viewClass

Optional namespaced class name of the View class to instantiate.

Throws

\Cake\View\Exception\MissingViewException

If view class was not found.

Returns

\Cake\View\View

set()

set(string|array  $name, mixed  $value = null) : $this

Saves a variable or an associative array of variables for use inside a template.

Parameters

string|array $name

A string or an array of data.

mixed $value

Value in case $name is a string (which then works as the key). Unused if $name is an associative array, otherwise serves as the values to $name's keys.

Returns

$this

viewOptions()

viewOptions(string|array|null  $options = null, boolean  $merge = true) : array

Get/Set valid view options in the object's _validViewOptions property. The property is created as an empty array if it is not set. If called without any parameters it will return the current list of valid view options. See `createView()`.

Parameters

string|array|null $options

string or array of string to be appended to _validViewOptions.

boolean $merge

Whether to merge with or override existing valid View options. Defaults to true.

Returns

array —

The updated view options as an array.

__construct()

__construct(array|string|null  $config = null) 

Constructor

Parameters

array|string|null $config

Array of configs, or string to load configs from app.php

__clone()

__clone() : void

Clone ViewBuilder instance when email object is cloned.

setFrom()

setFrom(string|array  $email, string|null  $name = null) : $this

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

Name

Throws

\InvalidArgumentException

Returns

$this

getFrom()

getFrom() : array

Gets "from" address.

Returns

array

from()

from(string|array|null  $email = null, string|null  $name = null) : array|$this

From

Parameters

string|array|null $email

Null to get, String with email, Array with email as key, name as value or email as value (without name)

string|null $name

Name

Throws

\InvalidArgumentException

Returns

array|$this

setSender()

setSender(string|array  $email, string|null  $name = null) : $this

Sets "sender" 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

Name

Throws

\InvalidArgumentException

Returns

$this

getSender()

getSender() : array

Gets "sender" address.

Returns

array

sender()

sender(string|array|null  $email = null, string|null  $name = null) : array|$this

Sender

Parameters

string|array|null $email

Null to get, String with email, Array with email as key, name as value or email as value (without name)

string|null $name

Name

Throws

\InvalidArgumentException

Returns

array|$this

setReplyTo()

setReplyTo(string|array  $email, string|null  $name = null) : $this

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

Name

Throws

\InvalidArgumentException

Returns

$this

getReplyTo()

getReplyTo() : array

Gets "Reply-To" address.

Returns

array

replyTo()

replyTo(string|array|null  $email = null, string|null  $name = null) : array|$this

Reply-To

Parameters

string|array|null $email

Null to get, String with email, Array with email as key, name as value or email as value (without name)

string|null $name

Name

Throws

\InvalidArgumentException

Returns

array|$this

setReadReceipt()

setReadReceipt(string|array  $email, string|null  $name = null) : $this

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

Name

Throws

\InvalidArgumentException

Returns

$this

getReadReceipt()

getReadReceipt() : array

Gets Read Receipt (Disposition-Notification-To header).

Returns

array

readReceipt()

readReceipt(string|array|null  $email = null, string|null  $name = null) : array|$this

Read Receipt (Disposition-Notification-To header)

Parameters

string|array|null $email

Null to get, String with email, Array with email as key, name as value or email as value (without name)

string|null $name

Name

Throws

\InvalidArgumentException

Returns

array|$this

setReturnPath()

setReturnPath(string|array  $email, string|null  $name = null) : $this

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

Name

Throws

\InvalidArgumentException

Returns

$this

getReturnPath()

getReturnPath() : array

Gets return path.

Returns

array

returnPath()

returnPath(string|array|null  $email = null, string|null  $name = null) : array|$this

Return Path

Parameters

string|array|null $email

Null to get, String with email, Array with email as key, name as value or email as value (without name)

string|null $name

Name

Throws

\InvalidArgumentException

Returns

array|$this

setTo()

setTo(string|array  $email, string|null  $name = null) : $this

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

Name

Returns

$this

getTo()

getTo() : array

Gets "to" address

Returns

array

to()

to(string|array|null  $email = null, string|null  $name = null) : array|$this

To

Parameters

string|array|null $email

Null to get, String with email, Array with email as key, name as value or email as value (without name)

string|null $name

Name

Returns

array|$this

addTo()

addTo(string|array  $email, string|null  $name = null) : $this

Add To

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

Name

Returns

$this

setCc()

setCc(string|array  $email, string|null  $name = null) : $this

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

Name

Returns

$this

getCc()

getCc() : array

Gets "cc" address.

Returns

array

cc()

cc(string|array|null  $email = null, string|null  $name = null) : array|$this

Cc

Parameters

string|array|null $email

Null to get, String with email, Array with email as key, name as value or email as value (without name)

string|null $name

Name

Returns

array|$this

addCc()

addCc(string|array  $email, string|null  $name = null) : $this

Add Cc

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

Name

Returns

$this

setBcc()

setBcc(string|array  $email, string|null  $name = null) : $this

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

Name

Returns

$this

getBcc()

getBcc() : array

Gets "bcc" address.

Returns

array

bcc()

bcc(string|array|null  $email = null, string|null  $name = null) : array|$this

Bcc

Parameters

string|array|null $email

Null to get, String with email, Array with email as key, name as value or email as value (without name)

string|null $name

Name

Returns

array|$this

addBcc()

addBcc(string|array  $email, string|null  $name = null) : $this

Add Bcc

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

Name

Returns

$this

setCharset()

setCharset(string|null  $charset) : $this

Charset setter.

Parameters

string|null $charset

Character set.

Returns

$this

getCharset()

getCharset() : string

Charset getter.

Returns

string —

Charset

charset()

charset(string|null  $charset = null) : string

Charset setter/getter

Parameters

string|null $charset

Character set.

Returns

string —

Charset

setHeaderCharset()

setHeaderCharset(string|null  $charset) : $this

HeaderCharset setter.

Parameters

string|null $charset

Character set.

Returns

$this

getHeaderCharset()

getHeaderCharset() : string

HeaderCharset getter.

Returns

string —

Charset

headerCharset()

headerCharset(string|null  $charset = null) : string

HeaderCharset setter/getter

Parameters

string|null $charset

Character set.

Returns

string —

Charset

setTransferEncoding()

setTransferEncoding(string|null  $encoding) : $this

TransferEncoding setter.

Parameters

string|null $encoding

Encoding set.

Returns

$this

getTransferEncoding()

getTransferEncoding() : string|null

TransferEncoding getter.

Returns

string|null —

Encoding

setEmailPattern()

setEmailPattern(string|null  $regex) : $this

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

$this

getEmailPattern()

getEmailPattern() : string

EmailPattern setter/getter

Returns

string

emailPattern()

emailPattern(string|boolean|null  $regex = false) : string|$this

EmailPattern setter/getter

Parameters

string|boolean|null $regex

The pattern to use for email address validation, null to unset the pattern and make use of filter_var() instead, false or nothing to return the current value

Returns

string|$this

setSubject()

setSubject(string  $subject) : $this

Sets subject.

Parameters

string $subject

Subject string.

Returns

$this

getSubject()

getSubject() : string

Gets subject.

Returns

string

subject()

subject(string|null  $subject = null) : string|$this

Get/Set Subject.

Parameters

string|null $subject

Subject string.

Returns

string|$this

getOriginalSubject()

getOriginalSubject() : string

Get original subject without encoding

Returns

string —

Original subject

setHeaders()

setHeaders(array  $headers) : $this

Sets headers for the message

Parameters

array $headers

Associative array containing headers to be set.

Returns

$this

addHeaders()

addHeaders(array  $headers) : $this

Add header for the message

Parameters

array $headers

Headers to set.

Returns

$this

getHeaders()

getHeaders(array  $include = array()) : array

Get list of headers

Includes:

  • from
  • replyTo
  • readReceipt
  • returnPath
  • to
  • cc
  • bcc
  • subject

Parameters

array $include

List of headers.

Returns

array

setTemplate()

setTemplate(string|null  $template) : $this

Sets template.

Parameters

string|null $template

Template name or null to not use.

Returns

$this

getTemplate()

getTemplate() : string

Gets template.

Returns

string

setLayout()

setLayout(string|null  $layout) : $this

Sets layout.

Parameters

string|null $layout

Layout name or null to not use

Returns

$this

getLayout()

getLayout() : string

Gets layout.

Returns

string

template()

template(boolean|string  $template = false, boolean|string  $layout = false) : array|$this

Template and layout

Parameters

boolean|string $template

Template name or null to not use

boolean|string $layout

Layout name or null to not use

Returns

array|$this

setViewRenderer()

setViewRenderer(string  $viewClass) : $this

Sets view class for render.

Parameters

string $viewClass

View class name.

Returns

$this

getViewRenderer()

getViewRenderer() : string

Gets view class for render.

Returns

string

viewRender()

viewRender(string|null  $viewClass = null) : string|$this

View class for render

Parameters

string|null $viewClass

View class name.

Returns

string|$this

setViewVars()

setViewVars(array  $viewVars) : $this

Sets variables to be set on render.

Parameters

array $viewVars

Variables to set for view.

Returns

$this

getViewVars()

getViewVars() : array

Gets variables to be set on render.

Returns

array

viewVars()

viewVars(array|null  $viewVars = null) : array|$this

Variables to be set on render

Parameters

array|null $viewVars

Variables to set for view.

Returns

array|$this

setTheme()

setTheme(string  $theme) : $this

Sets theme to use when rendering.

Parameters

string $theme

Theme name.

Returns

$this

getTheme()

getTheme() : string

Gets theme to use when rendering.

Returns

string

theme()

theme(string|null  $theme = null) : string|$this

Theme to use when rendering

Parameters

string|null $theme

Theme name.

Returns

string|$this

setHelpers()

setHelpers(array  $helpers) : $this

Sets helpers to be used when rendering.

Parameters

array $helpers

Helpers list.

Returns

$this

getHelpers()

getHelpers() : array

Gets helpers to be used when rendering.

Returns

array

helpers()

helpers(array|null  $helpers = null) : array|$this

Helpers to be used in render

Parameters

array|null $helpers

Helpers list.

Returns

array|$this

setEmailFormat()

setEmailFormat(string  $format) : $this

Sets email format.

Parameters

string $format

Formatting string.

Throws

\InvalidArgumentException

Returns

$this

getEmailFormat()

getEmailFormat() : string

Gets email format.

Returns

string

emailFormat()

emailFormat(string|null  $format = null) : string|$this

Email format

Parameters

string|null $format

Formatting string.

Throws

\InvalidArgumentException

Returns

string|$this

setTransport()

setTransport(string|\Cake\Mailer\AbstractTransport  $name) : $this

Sets the transport.

When setting the transport you can either use the name of a configured transport or supply a constructed transport.

Parameters

string|\Cake\Mailer\AbstractTransport $name

Either the name of a configured transport, or a transport instance.

Throws

\LogicException

When the chosen transport lacks a send method.

\InvalidArgumentException

When $name is neither a string nor an object.

Returns

$this

transport()

transport(string|\Cake\Mailer\AbstractTransport|null  $name = null) : \Cake\Mailer\AbstractTransport|$this

Get/set the transport.

When setting the transport you can either use the name of a configured transport or supply a constructed transport.

Parameters

string|\Cake\Mailer\AbstractTransport|null $name

Either the name of a configured transport, or a transport instance.

Throws

\LogicException

When the chosen transport lacks a send method.

\InvalidArgumentException

When $name is neither a string nor an object.

Returns

\Cake\Mailer\AbstractTransport|$this

setMessageId()

setMessageId(boolean|string  $message) : $this

Sets message ID.

Parameters

boolean|string $message

True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID.

Throws

\InvalidArgumentException

Returns

$this

getMessageId()

getMessageId() : boolean|string

Gets message ID.

Returns

boolean|string

messageId()

messageId(boolean|string|null  $message = null) : boolean|string|$this

Message-ID

Parameters

boolean|string|null $message

True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID

Throws

\InvalidArgumentException

Returns

boolean|string|$this

setDomain()

setDomain(string  $domain) : $this

Sets domain.

Domain as top level (the part after @).

Parameters

string $domain

Manually set the domain for CLI mailing.

Returns

$this

getDomain()

getDomain() : string

Gets domain.

Returns

string

domain()

domain(string|null  $domain = null) : string|$this

Domain as top level (the part after @)

Parameters

string|null $domain

Manually set the domain for CLI mailing

Returns

string|$this

setAttachments()

setAttachments(string|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:

$email->setAttachments('path/to/file');

Attach a file with a different filename:

$email->setAttachments(['custom_name.txt' => 'path/to/file.txt']);

Attach a file and specify additional properties:

$email->setAttachments(['custom_name.png' => [
     'file' => 'path/to/file',
     'mimetype' => 'image/png',
     'contentId' => 'abc123',
     'contentDisposition' => false
   ]
]);

Attach a file from string and specify additional properties:

$email->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

string|array $attachments

String with the filename or array with filenames

Throws

\InvalidArgumentException

Returns

$this

getAttachments()

getAttachments() : array

Gets attachments to the email message.

Returns

array —

Array of attachments.

attachments()

attachments(string|array|null  $attachments = null) : array|$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:

$email->setAttachments('path/to/file');

Attach a file with a different filename:

$email->setAttachments(['custom_name.txt' => 'path/to/file.txt']);

Attach a file and specify additional properties:

$email->setAttachments(['custom_name.png' => [
     'file' => 'path/to/file',
     'mimetype' => 'image/png',
     'contentId' => 'abc123',
     'contentDisposition' => false
   ]
]);

Attach a file from string and specify additional properties:

$email->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

string|array|null $attachments

String with the filename or array with filenames

Throws

\InvalidArgumentException

Returns

array|$this —

Either the array of attachments when getting or $this when setting.

addAttachments()

addAttachments(string|array  $attachments) : $this

Add attachments

Parameters

string|array $attachments

String with the filename or array with filenames

Throws

\InvalidArgumentException

Returns

$this

message()

message(string|null  $type = null) : string|array

Get generated message (used by transport classes)

Parameters

string|null $type

Use MESSAGE_* constants or null to return the full message as array

Returns

string|array —

String if type is given, array if type is null

setPriority()

setPriority(integer|null  $priority) : $this

Sets priority.

Parameters

integer|null $priority

1 (highest) to 5 (lowest)

Returns

$this

getPriority()

getPriority() : integer

Gets priority.

Returns

integer

setConfigTransport()

setConfigTransport(string|array  $key, array|\Cake\Mailer\AbstractTransport|null  $config = null) : void

Sets transport configuration.

Use this method to define transports to use in delivery profiles. Once defined you cannot edit the configurations, and must use Email::dropTransport() to flush the configuration first.

When using an array of configuration data a new transport will be constructed for each message sent. When using a Closure, the closure will be evaluated for each message.

The className is used to define the class to use for a transport. It can either be a short name, or a fully qualified class name

Parameters

string|array $key

The configuration name to write. Or an array of multiple transports to set.

array|\Cake\Mailer\AbstractTransport|null $config

Either an array of configuration data, or a transport instance. Null when using key as array.

getConfigTransport()

getConfigTransport(string  $key) : array|null

Gets current transport configuration.

Parameters

string $key

The configuration name to read.

Returns

array|null —

Transport config.

configTransport()

configTransport(string|array  $key, array|\Cake\Mailer\AbstractTransport|null  $config = null) : array|null

Add or read transport configuration.

Use this method to define transports to use in delivery profiles. Once defined you cannot edit the configurations, and must use Email::dropTransport() to flush the configuration first.

When using an array of configuration data a new transport will be constructed for each message sent. When using a Closure, the closure will be evaluated for each message.

The className is used to define the class to use for a transport. It can either be a short name, or a fully qualified classname

Parameters

string|array $key

The configuration name to read/write. Or an array of multiple transports to set.

array|\Cake\Mailer\AbstractTransport|null $config

Either an array of configuration data, or a transport instance.

Throws

\BadMethodCallException

When modifying an existing configuration.

Returns

array|null —

Either null when setting or an array of data when reading.

configuredTransport()

configuredTransport() : array

Returns an array containing the named transport configurations

Returns

array —

Array of configurations.

dropTransport()

dropTransport(string  $key) : void

Delete transport configuration.

Parameters

string $key

The transport name to remove.

setProfile()

setProfile(string|array  $config) : $this

Sets the configuration profile to use for this instance.

Parameters

string|array $config

String with configuration name, or an array with config.

Returns

$this

getProfile()

getProfile() : string|array

Gets the configuration profile to use for this instance.

Returns

string|array

profile()

profile(null|string|array  $config = null) : string|array|$this

Get/Set the configuration profile to use for this instance.

Parameters

null|string|array $config

String with configuration name, or an array with config or null to return current config.

Returns

string|array|$this

send()

send(string|array|null  $content = null) : array

Send an email using the specified content, template and layout

Parameters

string|array|null $content

String with message or array with messages

Throws

\BadMethodCallException

Returns

array

deliver()

deliver(string|array|null  $to = null, string|null  $subject = null, string|array|null  $message = null, string|array  $config = 'default', boolean  $send = true) : static

Static method to fast create an instance of \Cake\Mailer\Email

Parameters

string|array|null $to

Address to send (see Cake\Mailer\Email::to()). If null, will try to use 'to' from transport config

string|null $subject

String of subject or null to use 'subject' from transport config

string|array|null $message

String with message or array with variables to be used in render

string|array $config

String to use Email delivery profile from app.php or array with configs

boolean $send

Send the email or just return the instance pre-configured

Throws

\InvalidArgumentException

Returns

static —

Instance of Cake\Mailer\Email

reset()

reset() : $this

Reset all the internal variables to be able to send out a new email.

Returns

$this

jsonSerialize()

jsonSerialize() : array

Serializes the email object to a value that can be natively serialized and re-used to clone this email instance.

It has certain limitations for viewVars that are good to know:

  • ORM\Query executed and stored as resultset
  • SimpleXMLElements stored as associative array
  • Exceptions stored as strings
  • Resources, \Closure and \PDO are not supported.

Throws

\Exception

When a view var object can not be properly serialized.

Returns

array —

Serializable array of configuration properties.

createFromArray()

createFromArray(array  $config) : $this

Configures an email instance object from serialized config.

Parameters

array $config

Email configuration array.

Returns

$this —

Configured email instance.

serialize()

serialize() : string

Serializes the Email object.

Returns

string

unserialize()

unserialize(string  $data) : static

Unserializes the Email object.

Parameters

string $data

Serialized string.

Returns

static —

Configured email instance.

_setEmail()

_setEmail(string  $varName, string|array  $email, string  $name) : $this

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 $name

Name

Throws

\InvalidArgumentException

Returns

$this

_validateEmail()

_validateEmail(string  $email, string  $context) : void

Validate email address

Parameters

string $email

Email address to validate

string $context

Which property was set

Throws

\InvalidArgumentException

If email address does not validate

_setEmailSingle()

_setEmailSingle(string  $varName, string|array  $email, string  $name, string  $throwMessage) : $this

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 $name

Name

string $throwMessage

Exception message

Throws

\InvalidArgumentException

Returns

$this

_addEmail()

_addEmail(string  $varName, string|array  $email, string  $name) : $this

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 $name

Name

Throws

\InvalidArgumentException

Returns

$this

_formatAddress()

_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.

Parameters

array $address

Addresses to format.

Returns

array

_logDelivery()

_logDelivery(array  $contents) : void

Log the email message delivery.

Parameters

array $contents

The content with 'headers' and 'message' keys.

flatten()

flatten(string|array  $value) : string

Converts given value to string

Parameters

string|array $value

The value to convert

Returns

string

_applyConfig()

_applyConfig(string|array  $config) : void

Apply the config to an instance

Parameters

string|array $config

Configuration options.

Throws

\InvalidArgumentException

When using a configuration that doesn't exist.

_encode()

_encode(string  $text) : string

Encode the specified string using the current charset

Parameters

string $text

String to encode

Returns

string —

Encoded string

_decode()

_decode(string  $text) : string

Decode the specified string

Parameters

string $text

String to decode

Returns

string —

Decoded string

_encodeString()

_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

Parameters

string $text

The text to be converted

string $charset

the target encoding

Returns

string

_wrap()

_wrap(string  $message, integer  $wrapLength = \Cake\Mailer\Email::LINE_LENGTH_MUST) : array

Wrap the message to follow the RFC 2822 - 2.1.1

Parameters

string $message

Message to wrap

integer $wrapLength

The line length

Returns

array —

Wrapped message

_createBoundary()

_createBoundary() : void

Create unique boundary identifier

_attachFiles()

_attachFiles(string|null  $boundary = null) : array

Attach non-embedded files by adding file contents inside boundaries.

Parameters

string|null $boundary

Boundary to use. If null, will default to $this->_boundary

Returns

array —

An array of lines to add to the message

_readFile()

_readFile(string  $path) : string

Read the file contents and return a base64 version of the file contents.

Parameters

string $path

The absolute path to the file to read.

Returns

string —

File contents in base64 encoding

_attachInlineFiles()

_attachInlineFiles(string|null  $boundary = null) : array

Attach inline/embedded files to the message.

Parameters

string|null $boundary

Boundary to use. If null, will default to $this->_boundary

Returns

array —

An array of lines to add to the message

_render()

_render(array  $content) : array

Render the body of the email.

Parameters

array $content

Content to render

Returns

array —

Email body ready to be sent

_getTypes()

_getTypes() : array

Gets the text body types that are in this email message

Returns

array —

Array of types. Valid types are 'text' and 'html'

_renderTemplates()

_renderTemplates(string  $content) : array

Build and set all the view properties needed to render the templated emails.

If there is no template set, the $content will be returned in a hash of the text content types for the email.

Parameters

string $content

The content passed in from send() in most cases.

Returns

array —

The rendered content with html and text keys.

_getContentTransferEncoding()

_getContentTransferEncoding() : string

Return the Content-Transfer Encoding value based on the set transferEncoding or set charset.

Returns

string

_getContentTypeCharset()

_getContentTypeCharset() : string

Return charset value for Content-Type.

Checks fallback/compatibility types which include workarounds for legacy japanese character sets.

Returns

string

_checkViewVars()

_checkViewVars(mixed  $item, string  $key) : void

Iterates through hash to clean up and normalize.

Parameters

mixed $item

Reference to the view var value.

string $key

View var key.