Skip to content

Mailer ​

Type
Class
Namespace
craft\mail
Inherits
craft\mail\Mailer » yii\symfonymailer\Mailer
Extended by
craft\test\TestMailer
Since
3.0.0

The Mailer component provides APIs for sending email in Craft.

An instance of the Mailer component is globally accessible in Craft via Craft::$app->mailer.

View source

Public Properties ​

PropertyDescription
fromstring, array, craft\elements\User, craft\elements\User[], null – The default sender’s email address, or their user model(s).
replyTostring, array, craft\elements\User, craft\elements\User[], null – The default Reply-To email address, or their user model(s).
templatestring, null – The email template that should be used

from ​

Type
string, array, craft\elements\User, craft\elements\User[], null
Default value
null

The default sender’s email address, or their user model(s).

View source

replyTo ​

Type
string, array, craft\elements\User, craft\elements\User[], null
Default value
null
Since
3.4.0

The default Reply-To email address, or their user model(s).

View source

template ​

Type
string, null
Default value
null

The email template that should be used

View source

Public Methods ​

MethodDescription
composeFromKey()Composes a new email based on a given key.
send()

composeFromKey() ​

Composes a new email based on a given key.

Craft has four predefined email keys: account_activation, verify_new_email, forgot_password and test_email. Plugins can register additional email keys using the registerEmailMessages hook, and by providing the corresponding language strings.

php
Craft::$app->mailer->composeFromKey('account_activation', [
    'link' => $activationUrl
]);

View source

Arguments ​

  • $key (string) – The email key
  • $variables (array) – Any variables that should be passed to the email body template

Returns ​

craft\mail\Message – The new email message

Throws ​

send() ​

View source

Arguments ​

  • $message

Events ​

EVENT_BEFORE_PREP ​

Type
yii\mail\MailEvent
Since
3.6.5

The event that is triggered before a message is prepped to be sent.