Abstract Class yii\mail\BaseMailer
Inheritance | yii\mail\BaseMailer » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable, yii\base\ViewContextInterface, yii\mail\MailerInterface |
Subclasses | yii\swiftmailer\Mailer |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/mail/BaseMailer.php |
BaseMailer serves as a base class that implements the basic functions required by yii\mail\MailerInterface.
Concrete child classes should may focus on implementing the sendMessage() method.
See also yii\mail\BaseMessage For more details and usage information on BaseMailer, see the guide article on mailing.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$fileTransportCallback | callable | A PHP callback that will be called by send() when $useFileTransport is true. | yii\mail\BaseMailer |
$fileTransportPath | string | The directory where the email messages are saved when $useFileTransport is true. | yii\mail\BaseMailer |
$htmlLayout | string|boolean | HTML layout view name. | yii\mail\BaseMailer |
$messageClass | string | The default class name of the new message instances created by createMessage() | yii\mail\BaseMailer |
$messageConfig | array | The configuration that should be applied to any newly created email message instance by createMessage() or compose(). | yii\mail\BaseMailer |
$textLayout | string|boolean | Text layout view name. | yii\mail\BaseMailer |
$useFileTransport | boolean | Whether to save email messages as files under $fileTransportPath instead of sending them to the actual recipients. | yii\mail\BaseMailer |
$view | array|yii\web\View | View instance or its array configuration that will be used to render message bodies. | yii\mail\BaseMailer |
$viewPath | string | The directory that contains the view files for composing mail messages Defaults to '@app/mail'. | yii\mail\BaseMailer |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\BaseObject |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
afterSend() | This method is invoked right after mail was send. | yii\mail\BaseMailer |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
beforeSend() | This method is invoked right before mail send. | yii\mail\BaseMailer |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
compose() | Creates a new message instance and optionally composes its body content via view rendering. | yii\mail\BaseMailer |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
generateMessageFileName() | yii\mail\BaseMailer | |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
getView() | yii\mail\BaseMailer | |
getViewPath() | yii\mail\BaseMailer | |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
init() | Initializes the object. | yii\base\BaseObject |
off() | Detaches an existing event handler from this component. | yii\base\Component |
on() | Attaches an event handler to an event. | yii\base\Component |
render() | Renders the specified view with optional parameters and layout. | yii\mail\BaseMailer |
send() | Sends the given email message. | yii\mail\BaseMailer |
sendMultiple() | Sends multiple messages at once. | yii\mail\BaseMailer |
setView() | yii\mail\BaseMailer | |
setViewPath() | yii\mail\BaseMailer | |
trigger() | Triggers an event. | yii\base\Component |
Protected Methods
Method | Description | Defined By |
---|---|---|
createMessage() | Creates a new message instance. | yii\mail\BaseMailer |
createView() | Creates view instance from given configuration. | yii\mail\BaseMailer |
saveMessage() | Saves the message as a file under $fileTransportPath. | yii\mail\BaseMailer |
sendMessage() | Sends the specified message. | yii\mail\BaseMailer |
Events
Event | Type | Description | Defined By |
---|---|---|---|
EVENT_AFTER_SEND | yii\mail\MailEvent | An event raised right after send. | yii\mail\BaseMailer |
EVENT_BEFORE_SEND | yii\mail\MailEvent | An event raised right before send. | yii\mail\BaseMailer |
Property Details
A PHP callback that will be called by send() when $useFileTransport is true. The callback should return a file name which will be used to save the email message. If not set, the file name will be generated based on the current timestamp.
The signature of the callback is:
function ($mailer, $message)
The directory where the email messages are saved when $useFileTransport is true.
HTML layout view name. This is the layout used to render HTML mail body. The property can take the following values:
- a relative view name: a view file relative to $viewPath, e.g., 'layouts/html'.
- a path alias: an absolute view file path specified as a path alias, e.g., '@app/mail/html'.
- a boolean false: the layout is disabled.
The default class name of the new message instances created by createMessage()
The configuration that should be applied to any newly created
email message instance by createMessage() or compose(). Any valid property defined
by yii\mail\MessageInterface can be configured, such as from
, to
, subject
, textBody
, htmlBody
, etc.
For example:
[
'charset' => 'UTF-8',
'from' => 'noreply@mydomain.com',
'bcc' => 'developer@mydomain.com',
]
Text layout view name. This is the layout used to render TEXT mail body. Please refer to $htmlLayout for possible values that this property can take.
Whether to save email messages as files under $fileTransportPath instead of sending them to the actual recipients. This is usually used during development for debugging purpose.
See also $fileTransportPath.
View instance or its array configuration that will be used to render message bodies.
The directory that contains the view files for composing mail messages Defaults to '@app/mail'.
Method Details
This method is invoked right after mail was send.
You may override this method to do some postprocessing or logging based on mail send status. If you override this method, please make sure you call the parent implementation first.
public void afterSend ( $message, $isSuccessful ) | ||
$message | yii\mail\MessageInterface | |
$isSuccessful | boolean |
This method is invoked right before mail send.
You may override this method to do last-minute preparation for the message. If you override this method, please make sure you call the parent implementation first.
public boolean beforeSend ( $message ) | ||
$message | yii\mail\MessageInterface | |
return | boolean | Whether to continue sending an email. |
---|
Creates a new message instance and optionally composes its body content via view rendering.
public yii\mail\MessageInterface compose ( $view = null, array $params = [] ) | ||
$view | string|array|null | The view to be used for rendering the message body. This can be:
The view to be rendered can be specified in one of the following formats:
|
$params | array | The parameters (name-value pairs) that will be extracted and made available in the view file. |
return | yii\mail\MessageInterface | Message instance. |
---|
Creates a new message instance.
The newly created instance will be initialized with the configuration specified by $messageConfig. If the configuration does not specify a 'class', the $messageClass will be used as the class of the new message instance.
protected yii\mail\MessageInterface createMessage ( ) | ||
return | yii\mail\MessageInterface | Message instance. |
---|
Creates view instance from given configuration.
protected yii\web\View createView ( array $config ) | ||
$config | array | View configuration. |
return | yii\web\View | View instance. |
---|
public string generateMessageFileName ( ) | ||
return | string | The file name for saving the message when $useFileTransport is true. |
---|
public yii\web\View getView ( ) | ||
return | yii\web\View | View instance. |
---|
public string getViewPath ( ) | ||
return | string | The directory that contains the view files for composing mail messages Defaults to '@app/mail'. |
---|
Renders the specified view with optional parameters and layout.
The view will be rendered using the $view component.
public string render ( $view, $params = [], $layout = false ) | ||
$view | string | The view name or the path alias of the view file. |
$params | array | The parameters (name-value pairs) that will be extracted and made available in the view file. |
$layout | string|boolean | Layout view name or path alias. If false, no layout will be applied. |
return | string | The rendering result. |
---|
Saves the message as a file under $fileTransportPath.
protected boolean saveMessage ( $message ) | ||
$message | yii\mail\MessageInterface | |
return | boolean | Whether the message is saved successfully |
---|
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.
public boolean send ( $message ) | ||
$message | yii\mail\MessageInterface | Email message instance to be sent |
return | boolean | Whether the message has been sent successfully |
---|
Sends the specified message.
This method should be implemented by child classes with the actual email sending logic.
protected abstract boolean sendMessage ( $message ) | ||
$message | yii\mail\MessageInterface | The message to be sent |
return | boolean | Whether the message is sent successfully |
---|
Sends multiple messages at once.
The default implementation simply calls send() multiple times. Child classes may override this method to implement more efficient way of sending multiple messages.
public integer sendMultiple ( array $messages ) | ||
$messages | array | List of email messages, which should be sent. |
return | integer | Number of messages that are successfully sent. |
---|
public void setView ( $view ) | ||
$view | array|yii\web\View | View instance or its array configuration that will be used to render message bodies. |
throws | yii\base\InvalidConfigException | on invalid argument. |
---|
public void setViewPath ( $path ) | ||
$path | string | The directory that contains the view files for composing mail messages This can be specified as an absolute path or a path alias. |
Event Details
An event raised right after send.
An event raised right before send. You may set yii\mail\MailEvent::$isValid to be false to cancel the send.