Craft 3 Class Reference

Class craft\mail\Mailer

Inheritance
craft\mail\Mailer » yii\swiftmailer\Mailer
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/mail/Mailer.php

The Mailer component provides APIs for sending email in Craft.

An instance of the Mailer component is globally accessible in Craft via craft\web\Application::mailer.

Public Properties
Property Type Description Defined By
$from string, array, craft\elements\User, craft\elements\User[], null The default sender’s email address, or their user model(s). craft\mail\Mailer
$template string, null The email template that should be used craft\mail\Mailer
Public Methods
Method Description Defined By
composeFromKey() Composes a new email based on a given key. craft\mail\Mailer
send() Sends the given email message. craft\mail\Mailer

Property Details

$from public property #

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

public string, array, craft\elements\User, craft\elements\User[], null $from null

$template public property #

The email template that should be used

public string, null $template null

Method Details

composeFromKey() public method #

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.

Craft::$app->mailer->composeFromKey('account_activation', [
    'link' => $activationUrl
]);
public craft\mail\Message composeFromKey ( \craft\mail\string $key, array $variables = [] )
$key string The email key
$variables array Any variables that should be passed to the email body template
return 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() public method #

Sends the given email message.

This method will log a message about the email being sent. If \craft\mail\useFileTransport is true, it will save the email as a file under \craft\mail\fileTransportPath. Otherwise, it will call \craft\mail\sendMessage() to send the email to its recipient(s). Child classes should implement \craft\mail\sendMessage() with the actual email sending logic.

public boolean send ( $message )
$message yii\mail\MessageInterface The email message instance to be sent.
return boolean Whether the message has been sent successfully.