Mailer ​
- Type
- Class
- Namespace
- craft\mail
- Inherits
- craft\mail\Mailer » yii\swiftmailer\Mailer » yii\mail\BaseMailer » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable, yii\base\ViewContextInterface, yii\mail\MailerInterface
- 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 |
|---|---|
| behaviors | yii\base\Behavior – List of behaviors attached to this component. |
| enableSwiftMailerLogging | boolean – Whether to enable writing of the SwiftMailer internal logs using Yii log mechanism. |
| fileTransportCallback | callable, null – A PHP callback that will be called by send() when useFileTransport is true. |
| fileTransportPath | string – The directory where the email messages are saved when useFileTransport is true. |
| from | string, array, craft\elements\User, craft\elements\User[], null – The default sender’s email address, or their user model(s). |
| htmlLayout | string, boolean – HTML layout view name. |
| messageClass | string – Message default class name. |
| messageConfig | array – The configuration that should be applied to any newly created email message instance by createMessage() or compose(). |
| replyTo | string, array, craft\elements\User, craft\elements\User[], null – The default Reply-To email address, or their user model(s). |
| swiftMailer | \Swift_Mailer – Swift mailer instance. |
| template | string, null – The email template that should be used |
| textLayout | string, boolean – Text layout view name. |
| transport | \Swift_Transport |
| useFileTransport | boolean – Whether to save email messages as files under fileTransportPath instead of sending them to the actual recipients. |
| view | yii\web\View – View instance. |
| viewPath | string – The directory that contains the view files for composing mail messages Defaults to '@app/mail'. |
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 |
|---|---|
| __call() | Calls the named method which is not a class method. |
| __clone() | This method is called after the object is created by cloning an existing one. |
| __construct() | Constructor. |
| __get() | Returns the value of a component property. |
| __isset() | Checks if a property is set, i.e. defined and not null. |
| __set() | Sets the value of a component property. |
| __unset() | Sets a component property to be null. |
| afterSend() | This method is invoked right after mail was send. |
| attachBehavior() | Attaches a behavior to this component. |
| attachBehaviors() | Attaches a list of behaviors to the component. |
| beforeSend() | This method is invoked right before mail send. |
| behaviors() | Returns a list of behaviors that this component should behave as. |
| canGetProperty() | Returns a value indicating whether a property can be read. |
| canSetProperty() | Returns a value indicating whether a property can be set. |
| className() | Returns the fully qualified name of this class. |
| compose() | Creates a new message instance and optionally composes its body content via view rendering. |
| composeFromKey() | Composes a new email based on a given key. |
| detachBehavior() | Detaches a behavior from the component. |
| detachBehaviors() | Detaches all behaviors from the component. |
| ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. |
| generateMessageFileName() | |
| getBehavior() | Returns the named behavior object. |
| getBehaviors() | Returns all behaviors attached to this component. |
| getSwiftMailer() | |
| getTransport() | |
| getView() | |
| getViewPath() | |
| hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. |
| hasMethod() | Returns a value indicating whether a method is defined. |
| hasProperty() | Returns a value indicating whether a property is defined for this component. |
| init() | Initializes the object. |
| off() | Detaches an existing event handler from this component. |
| on() | Attaches an event handler to an event. |
| render() | Renders the specified view with optional parameters and layout. |
| send() | Sends the given email message. |
| sendMultiple() | Sends multiple messages at once. |
| setTransport() | |
| setView() | |
| setViewPath() | |
| trigger() | Triggers an event. |
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.
Craft::$app->mailer->composeFromKey('account_activation', [
'link' => $activationUrl
]);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 messageConfig or\craft\mail\classis not configured to use craft\mail\Message
send() ​
Sends the given email message.
This method will log a message about the email being sent. If useFileTransport is true, it will save the email as a file under fileTransportPath. Otherwise, it will call sendMessage() to send the email to its recipient(s). Child classes should implement sendMessage() with the actual email sending logic.
Arguments ​
$message(yii\mail\MessageInterface) – Email message instance to be sent
Returns ​
boolean – Whether the message has been sent successfully
Protected Methods ​
| Method | Description |
|---|---|
| createMessage() | Creates a new message instance. |
| createSwiftMailer() | Creates Swift mailer instance. |
| createSwiftObject() | Creates Swift library object, from given array configuration. |
| createTransport() | Creates email transport instance by its array configuration. |
| createView() | Creates view instance from given configuration. |
| saveMessage() | Saves the message as a file under fileTransportPath. |
| sendMessage() | Sends the specified 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.