Class Mailer

Mailer base class.

Mailer classes let you encapsulate related Email logic into a reusable and testable class.

Defining Messages

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.

Sending Messages

After you have defined some messages you will want to send them:

$mailer = new UserMailer();
$mailer->send('resetPassword', $user);

Event Listener

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()
{
    return [
        'Model.afterSave' => 'onRegistration',
    ];
}

public function onRegistration(Event $event, Entity $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.

Abstract
Namespace: Cake\Mailer

Properties summary

  • $_clonedEmail protected
    \Cake\Mailer\Email

    Cloned Email instance for restoring instance after email is sent by mailer action.

  • $_email protected
    \Cake\Mailer\Email

    Email instance.

  • $_modelFactories protected
    array

    A list of overridden model factory functions.

  • $_modelType protected
    string

    The model type to use.

  • $modelClass public
    string|false|null

    This object's primary model class name. Should be a plural form.

  • $name public static
    string

    Mailer's name.

Method Summary

Method Detail

__call() public

__call(mixed $method, mixed $args)

Magic method to forward method class to Email instance.

Parameters

string $method

Method name.

array $args

Method arguments

Returns

$this|mixed

__construct() public

__construct(\Cake\Mailer\Email $email)

Constructor.

Parameters

\Cake\Mailer\Email|null $email optional

Email instance.

_setModelClass() protected

_setModelClass(mixed $name)

Set the modelClass and modelKey properties based on conventions.

If the properties are already set they will not be overwritten

Parameters

string $name

Class name.

addAttachments() public

addAttachments(mixed $attachments)

Parameters

mixed $attachments

addBcc() public

addBcc(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

addCc() public

addCc(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

addHeaders() public

addHeaders(array $headers)

Parameters

array $headers

addTo() public

addTo(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

attachments() public

attachments(mixed $attachments)

Parameters

mixed $attachments

bcc() public

bcc(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

cc() public

cc(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

charset() public

charset(mixed $charset)

Parameters

mixed $charset

domain() public

domain(mixed $domain)

Parameters

mixed $domain

emailFormat() public

emailFormat(mixed $format)

Parameters

mixed $format

emailPattern() public

emailPattern(mixed $regex)

Parameters

mixed $regex

from() public

from(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

getAttachments() public

getAttachments()

getBcc() public

getBcc()

getCc() public

getCc()

getCharset() public

getCharset()

getDomain() public

getDomain()

getEmailFormat() public

getEmailFormat()

getEmailPattern() public

getEmailPattern()

getFrom() public

getFrom()

getHeaderCharset() public

getHeaderCharset()

getHeaders() public

getHeaders(array $include = [])

Parameters

array $include = []

getHelpers() public

getHelpers()

getLayout() public

getLayout()

getMessageId() public

getMessageId()

getModelType() public

getModelType()

Get the model type to be used by this class

Returns

string

getName() public

getName()

Returns the mailer's name.

Returns

string

getProfile() public

getProfile()

getReadReceipt() public

getReadReceipt()

getReplyTo() public

getReplyTo()

getReturnPath() public

getReturnPath()

getSender() public

getSender()

getSubject() public

getSubject()

getTemplate() public

getTemplate()

getTheme() public

getTheme()

getTo() public

getTo()

getTransport() public

getTransport()

getViewRenderer() public

getViewRenderer()

getViewVars() public

getViewVars()

headerCharset() public

headerCharset(mixed $charset)

Parameters

mixed $charset

helpers() public

helpers(mixed $helpers)

Parameters

mixed $helpers

implementedEvents() public

implementedEvents()

Implemented events.

Returns

array

layout() public

layout(mixed $layout)

Sets layout to use.

Parameters

string $layout

Name of the layout to use.

Returns

$this

loadModel() public

loadModel(mixed $modelClass, mixed $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.

Parameters

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 modelType() value.

Returns

\Cake\Datasource\RepositoryInterface

The model instance created.

Throws

Cake\Datasource\Exception\MissingModelException
If the model class cannot be found.
InvalidArgumentException
When using a type that has not been registered.
UnexpectedValueException
If no model type has been defined

message() public

message(mixed $type)

Parameters

mixed $type

messageId() public

messageId(mixed $message)

Parameters

mixed $message

modelFactory() public

modelFactory(mixed $type, callable $factory)

Override a existing callable to generate repositories of a given type.

Parameters

string $type

The name of the repository type the factory function is for.

callable $factory

The factory function used to create instances.

modelType() public

modelType(mixed $modelType)

Set or get the model type to be used by this class

Parameters

string|null $modelType optional

The model type or null to retrieve the current

Returns

string|$this

profile() public

profile(mixed $config)

Parameters

mixed $config

readReceipt() public

readReceipt(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

replyTo() public

replyTo(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

reset() protected

reset()

Reset email instance.

Returns

$this

returnPath() public

returnPath(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

send() public

send(mixed $action, mixed $args, mixed $headers)

Sends email.

Parameters

string $action

The name of the mailer action to trigger.

array $args optional

Arguments to pass to the triggered mailer action.

array $headers optional

Headers to set.

Returns

array

Throws

Cake\Mailer\Exception\MissingActionException

BadMethodCallException

sender() public

sender(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

set() public

set(mixed $key, mixed $value)

Sets email view vars.

Parameters

string|array $key

Variable name or hash of view variables.

mixed $value optional

View variable value.

Returns

$this

setAttachments() public

setAttachments(mixed $attachments)

Parameters

mixed $attachments

setBcc() public

setBcc(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

setCc() public

setCc(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

setCharset() public

setCharset(mixed $charset)

Parameters

mixed $charset

setDomain() public

setDomain(mixed $domain)

Parameters

mixed $domain

setEmailFormat() public

setEmailFormat(mixed $format)

Parameters

mixed $format

setEmailPattern() public

setEmailPattern(mixed $regex)

Parameters

mixed $regex

setFrom() public

setFrom(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

setHeaderCharset() public

setHeaderCharset(mixed $charset)

Parameters

mixed $charset

setHeaders() public

setHeaders(array $headers)

Parameters

array $headers

setHelpers() public

setHelpers(array $helpers)

Parameters

array $helpers

setLayout() public

setLayout(mixed $layout)

Parameters

mixed $layout

setMessageId() public

setMessageId(mixed $message)

Parameters

mixed $message

setModelType() public

setModelType(mixed $modelType)

Set the model type to be used by this class

Parameters

string $modelType

The model type

Returns

$this

setProfile() public

setProfile(mixed $config)

Parameters

mixed $config

setReadReceipt() public

setReadReceipt(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

setReplyTo() public

setReplyTo(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

setReturnPath() public

setReturnPath(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

setSender() public

setSender(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

setSubject() public

setSubject(mixed $subject)

Parameters

mixed $subject

setTemplate() public

setTemplate(mixed $template)

Parameters

mixed $template

setTheme() public

setTheme(mixed $theme)

Parameters

mixed $theme

setTo() public

setTo(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

setTransport() public

setTransport(mixed $name)

Parameters

mixed $name

setViewRenderer() public

setViewRenderer(mixed $viewClass)

Parameters

mixed $viewClass

setViewVars() public

setViewVars(mixed $viewVars)

Parameters

mixed $viewVars

subject() public

subject(mixed $subject)

Parameters

mixed $subject

template() public

template(mixed $template, mixed $layout)

Parameters

mixed $template
mixed $layout

theme() public

theme(mixed $theme)

Parameters

mixed $theme

to() public

to(mixed $email, mixed $name)

Parameters

mixed $email
mixed $name

transport() public

transport(mixed $name)

Parameters

mixed $name

viewBuilder() public

viewBuilder()

Get Email instance's view builder.

Returns

\Cake\View\ViewBuilder

viewRender() public

viewRender(mixed $viewClass)

Parameters

mixed $viewClass

viewVars() public

viewVars(mixed $viewVars)

Parameters

mixed $viewVars

Property Detail

$_clonedEmail protected

Cloned Email instance for restoring instance after email is sent by mailer action.

Type

\Cake\Mailer\Email

$_email protected

Email instance.

Type

\Cake\Mailer\Email

$_modelFactories protected

A list of overridden model factory functions.

Type

array

$_modelType protected

The model type to use.

Type

string

$modelClass public

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 false to not use auto-loading on this object. Null auto-detects based on controller name.

Type

string|false|null

$name public static

Mailer's name.

Type

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/3.9/class-Cake.Mailer.Mailer.html