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
.
Public Properties ​
Property | Description |
---|---|
from | string, array, craft\elements\User, craft\elements\User[], null – The default sender’s email address, or their user model(s). |
replyTo | string, array, craft\elements\User, craft\elements\User[], null – The default Reply-To email address, or their user model(s). |
template | string, 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).
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).
template
​
The email template that should be used
Public Methods ​
Method | Description |
---|---|
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.
php
$mailer = Craft::$app->getMailer();
$message = $mailer->composeFromKey('account_activation', [
'link' => $activationUrl
]);
$mailer->send($message);
Plugins can register additional emails using the craft\services\SystemMessages::EVENT_REGISTER_MESSAGES event.
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 ​
- yii\base\InvalidConfigException
if\craft\mail\messageConfig
or\craft\mail\class
is not configured to use craft\mail\Message
send()
​
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.