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
Since
3.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
$behaviorsyii\base\Behavior – List of behaviors attached to this component
$enableSwiftMailerLoggingboolean – Whether to enable writing of the SwiftMailer internal logs using Yii log mechanism.
$fileTransportCallbackcallable – A PHP callback that will be called by send() when $useFileTransport is true.
$fileTransportPathstring – The directory where the email messages are saved when $useFileTransport is true.
$fromstring, array, craft\elements\User, craft\elements\User[], null – The default sender’s email address, or their user model(s).
$htmlLayoutstring, boolean – HTML layout view name.
$messageClassstring – The default class name of the new message instances created by createMessage()
$messageConfigarray – The configuration that should be applied to any newly created email message instance by createMessage() or compose().
$swiftMailerarray, \Swift_Mailer – Swift mailer instance or array configuration.
$templatestring, null – The email template that should be used
$textLayoutstring, boolean – Text layout view name.
$transportarray, \Swift_Transport
$useFileTransportboolean – Whether to save email messages as files under $fileTransportPath instead of sending them to the actual recipients.
$viewarray, yii\web\View – View instance or its array configuration that will be used to render message bodies.
$viewPathstring – 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

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

View source

Signature

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

$template

Type
string, null

The email template that should be used

View source

Signature

public string, null $template = null

Public Methods

MethodDescription
__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 an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to 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.
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
]);

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

Signature

public craft\mail\Message composeFromKey ( \craft\mail\string $key, array $variables = [] )

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.

View source

Arguments

Returns

boolean – Whether the message has been sent successfully.

Signature

public boolean send ( $message )

Protected Methods

MethodDescription
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.