Mailer base class.
Mailer classes let you encapsulate related Email logic into a reusable and testable class.
Mailers make it easy for you to define methods that handle email formatting logic. For example:
class UserMailer extends Mailer { public function resetPassword($user) { $this ->setSubject('Reset Password') ->setTo($user->email) ->set(['token' => $user->token]); } }
Is a trivial example but shows how a mailer could be declared.
After you have defined some messages you will want to send them:
$mailer = new UserMailer(); $mailer->send('resetPassword', $user);
Mailers can also subscribe to application event allowing you to decouple email delivery from your application code. By re-declaring the implementedEvents()
method you can define event handlers that can convert events into email. For example, if your application had a user registration event:
public function implementedEvents(): array { return [ 'Model.afterSave' => 'onRegistration', ]; } public function onRegistration(EventInterface $event, EntityInterface $entity, ArrayObject $options) { if ($entity->isNew()) { $this->send('welcome', [$entity]); } }
The onRegistration method converts the application event into a mailer method. Our mailer could either be registered in the application bootstrap, or in the Table class' initialize() hook.
array
Configuration sets.
array
Mailer driver class map.
(callable|\Cake\Datasource\Locator\LocatorInterface)[]
A list of overridden model factory functions.
string
The model type to use.
array
Hold message, renderer and transport instance for restoring after runnning a mailer action.
array|null
\Cake\Mailer\Message
Message instance.
string
Message class name.
string|null
This object's primary model class name. Should be a plural form.
string
Mailer's name.
\Cake\Mailer\Renderer|null
Email Renderer
\Cake\Mailer\AbstractTransport|null
The transport instance to use for sending mail.
Gets attachments to the email message. {@see \Cake\Mailer\Message::getAttachments()}
Gets Read Receipt (Disposition-Notification-To header). {@see \Cake\Mailer\Message::getReadReceipt()}
Restore message, renderer, transport instances to state before an action was run.
Add attachments to the email message. {@see \Cake\Mailer\Message::setAttachments()}
This method can be used to define configuration adapters for an application.
Sets Read Receipt (Disposition-Notification-To header). {@see \Cake\Mailer\Message::setReadReceipt()}
__call(string $method, array $args)
Magic method to forward method class to Message instance.
string
$method Method name.
array
$args Method arguments
$this|mixed
__construct(mixed $config)
Constructor
array|string|null
$config optional Array of configs, or string to load configs from app.php
_setModelClass(string $name)
Set the modelClass property based on conventions.
If the property is already set it will not be overwritten
string
$name Class name.
addAttachments(mixed $attachments)
Add attachments. {@see \Cake\Mailer\Message::addAttachments()}
mixed
$attachments addBcc(mixed $email, mixed $name)
Add "bcc" address. {@see \Cake\Mailer\Message::addBcc()}
mixed
$email mixed
$name addCc(mixed $email, mixed $name)
Add "cc" address. {@see \Cake\Mailer\Message::addCc()}
mixed
$email mixed
$name addHeaders(array $headers)
Add header for the message. {@see \Cake\Mailer\Message::addHeaders()}
array
$headers addTo(mixed $email, mixed $name)
Add "To" address. {@see \Cake\Mailer\Message::addTo()}
mixed
$email mixed
$name configured()
Returns an array containing the named configurations
string[]
Array of configurations.
deliver(string $content)
Render content and send email using configured transport.
string
$content optional Content.
array
drop(string $config)
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.
string
$config An existing configuration you wish to remove.
bool
Success of the removal, returns false when the config does not exist.
flatten(mixed $value)
Converts given value to string
string|array
$value The value to convert
string
getAttachments()
Gets attachments to the email message. {@see \Cake\Mailer\Message::getAttachments()}
getBcc()
Gets "bcc" address. {@see \Cake\Mailer\Message::getBcc()}
getBody(?string $type = null)
Get generated message body as array. {@see \Cake\Mailer\Message::getBody()}
?string
$type = null getCc()
Gets "cc" address. {@see \Cake\Mailer\Message::getCc()}
getCharset()
Charset getter. {@see \Cake\Mailer\Message::getCharset()}
getConfig(string $key)
Reads existing configuration.
string
$key The name of the configuration.
mixed|null
Configuration data at the named key or null if the key does not exist.
getConfigOrFail(string $key)
Reads existing configuration for a specific key.
The config value for this key must exist, it can never be null.
string
$key The name of the configuration.
mixed
Configuration data at the named key.
InvalidArgumentException
getDomain()
Gets domain. {@see \Cake\Mailer\Message::getDomain()}
getDsnClassMap()
Returns the DSN class map for this class.
string[]
getEmailFormat()
Gets email format. {@see \Cake\Mailer\Message::getEmailFormat()}
getFrom()
Gets "from" address. {@see \Cake\Mailer\Message::getFrom()}
getHeaderCharset()
HeaderCharset getter. {@see \Cake\Mailer\Message::getHeaderCharset()}
getHeaders(array $include = [])
Get list of headers. {@see \Cake\Mailer\Message::getHeaders()}
array
$include = [] getMessage()
Get message instance.
\Cake\Mailer\Message
getMessageId()
Gets message ID. {@see \Cake\Mailer\Message::getMessageId()}
getModelType()
Get the model type to be used by this class
string
getReadReceipt()
Gets Read Receipt (Disposition-Notification-To header). {@see \Cake\Mailer\Message::getReadReceipt()}
getRenderer()
Get email renderer.
\Cake\Mailer\Renderer
getReplyTo()
Gets "Reply-To" address. {@see \Cake\Mailer\Message::getReplyTo()}
getReturnPath()
Gets return path. {@see \Cake\Mailer\Message::getReturnPath()}
getSender()
Gets "sender" address. {@see \Cake\Mailer\Message::getSender()}
getSubject()
Gets subject. {@see \Cake\Mailer\Message::getSubject()}
getTo()
Gets "to" address. {@see \Cake\Mailer\Message::getTo()}
getTransport()
Gets the transport.
\Cake\Mailer\AbstractTransport
implementedEvents()
Implemented events.
array
loadModel(?string $modelClass, ?string $modelType)
Loads and constructs repository objects required by this object
Typically used to load ORM Table objects as required. Can also be used to load other types of repository objects your application uses.
If a repository provider does not return an object a MissingModelException will be thrown.
string|null
$modelClass optional Name of model class to load. Defaults to $this->modelClass. The name can be an alias like 'Post'
or FQCN like App\Model\Table\PostsTable::class
.
string|null
$modelType optional The type of repository to load. Defaults to the getModelType() value.
\Cake\Datasource\RepositoryInterface
The model instance created.
Cake\Datasource\Exception\MissingModelException
UnexpectedValueException
logDelivery(array $contents)
Log the email message delivery.
array
$contents The content with 'headers' and 'message' keys.
modelFactory(string $type, mixed $factory)
Override a existing callable to generate repositories of a given type.
string
$type The name of the repository type the factory function is for.
callable|\Cake\Datasource\Locator\LocatorInterface
$factory The factory function used to create instances.
parseDsn(string $dsn)
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.
string
$dsn The DSN string to convert to a configuration array
array
The configuration array to be stored after parsing the DSN
InvalidArgumentException
render(string $content)
Render content and set message body.
string
$content optional Content.
$this
reset()
Reset all the internal variables to be able to send out a new email.
$this
restore()
Restore message, renderer, transport instances to state before an action was run.
$this
send(?string $action, array $args, array $headers)
Sends email.
string|null
$action optional The name of the mailer action to trigger. If no action is specified then all other method arguments will be ignored.
array
$args optional Arguments to pass to the triggered mailer action.
array
$headers optional Headers to set.
array
Cake\Mailer\Exception\MissingActionException
BadMethodCallException
set(mixed $key, mixed $value)
Sets email view vars.
string|array
$key Variable name or hash of view variables.
mixed
$value optional View variable value.
$this
setAttachments(mixed $attachments)
Add attachments to the email message. {@see \Cake\Mailer\Message::setAttachments()}
mixed
$attachments setBcc(mixed $email, mixed $name)
Sets "bcc" address. {@see \Cake\Mailer\Message::setBcc()}
mixed
$email mixed
$name setCc(mixed $email, mixed $name)
Sets "cc" address. {@see \Cake\Mailer\Message::setCc()}
mixed
$email mixed
$name setCharset(mixed $charset)
Charset setter. {@see \Cake\Mailer\Message::setCharset()}
mixed
$charset setConfig(mixed $key, mixed $config)
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.
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);
string|array
$key The name of the configuration, or an array of multiple configs.
array|object|null
$config optional An array of name => configuration data for adapter.
BadMethodCallException
LogicException
setDomain(mixed $domain)
Sets domain. {@see \Cake\Mailer\Message::setDomain()}
mixed
$domain setDsnClassMap(array $map)
Updates the DSN class map for this class.
string[]
$map Additions/edits to the class map to apply.
setEmailFormat(mixed $format)
Sets email format. {@see \Cake\Mailer\Message::getHeaders()}
mixed
$format setFrom(mixed $email, mixed $name)
Sets "from" address. {@see \Cake\Mailer\Message::setFrom()}
mixed
$email mixed
$name setHeaderCharset(mixed $charset)
HeaderCharset setter. {@see \Cake\Mailer\Message::setHeaderCharset()}
mixed
$charset setHeaders(array $headers)
Sets headers for the message. {@see \Cake\Mailer\Message::setHeaders()}
array
$headers setLogConfig(mixed $log)
Set logging config.
string|array|true
$log Log config.
setMessage(\Cake\Mailer\Message $message)
Set message instance.
\Cake\Mailer\Message
$message Message instance.
$this
setMessageId(mixed $message)
Sets message ID. {@see \Cake\Mailer\Message::setMessageId()}
mixed
$message setModelType(string $modelType)
Set the model type to be used by this class
string
$modelType The model type
$this
setProfile(mixed $config)
Sets the configuration profile to use for this instance.
string|array
$config String with configuration name, or an array with config.
$this
setReadReceipt(mixed $email, mixed $name)
Sets Read Receipt (Disposition-Notification-To header). {@see \Cake\Mailer\Message::setReadReceipt()}
mixed
$email mixed
$name setRenderer(\Cake\Mailer\Renderer $renderer)
Set email renderer.
\Cake\Mailer\Renderer
$renderer Render instance.
$this
setReplyTo(mixed $email, mixed $name)
Sets "Reply-To" address. {@see \Cake\Mailer\Message::setReplyTo()}
mixed
$email mixed
$name setReturnPath(mixed $email, mixed $name)
Sets return path. {@see \Cake\Mailer\Message::setReturnPath()}
mixed
$email mixed
$name setSender(mixed $email, mixed $name)
Sets "sender" address. {@see \Cake\Mailer\Message::setSender()}
mixed
$email mixed
$name setSubject(mixed $subject)
Sets subject. {@see \Cake\Mailer\Message::setSubject()}
mixed
$subject setTo(mixed $email, mixed $name)
Sets "to" address. {@see \Cake\Mailer\Message::setTo()}
mixed
$email mixed
$name setTransport(mixed $name)
Sets the transport.
When setting the transport you can either use the name of a configured transport or supply a constructed transport.
string|\Cake\Mailer\AbstractTransport
$name Either the name of a configured transport, or a transport instance.
$this
LogicException
InvalidArgumentException
setViewVars(mixed $key, mixed $value)
Sets email view vars.
string|array
$key Variable name or hash of view variables.
mixed
$value optional View variable value.
$this
viewBuilder()
Get the view builder.
\Cake\View\ViewBuilder
Configuration sets.
array
Mailer driver class map.
array
A list of overridden model factory functions.
(callable|\Cake\Datasource\Locator\LocatorInterface)[]
The model type to use.
string
Hold message, renderer and transport instance for restoring after runnning a mailer action.
array
array|null
Message instance.
\Cake\Mailer\Message
Message class name.
string
This object's primary model class name. Should be a plural form.
CakePHP will not inflect the name.
Example: For an object named 'Comments', the modelClass would be 'Comments'. Plugin classes should use Plugin.Comments
style names to correctly load models from the correct plugin.
Use empty string to not use auto-loading on this object. Null auto-detects based on controller name.
string|null
Mailer's name.
string
Email Renderer
\Cake\Mailer\Renderer|null
The transport instance to use for sending mail.
\Cake\Mailer\AbstractTransport|null
© 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.Mailer.html