Class yii\swiftmailer\Mailer
Inheritance | yii\swiftmailer\Mailer » yii\mail\BaseMailer » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable, yii\base\ViewContextInterface, yii\mail\MailerInterface |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-swiftmailer/blob/master/Mailer.php |
Mailer implements a mailer based on SwiftMailer.
To use Mailer, you should configure it in the application configuration like the following:
[
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
],
],
// ...
],
// ...
],
You may also skip the configuration of the $transport property. In that case, the default
\Swift_SendmailTransport
transport will be used to send emails.
You specify the transport constructor arguments using 'constructArgs' key in the config. You can also specify the list of plugins, which should be registered to the transport using 'plugins' key. For example:
'transport' => [
'class' => 'Swift_SmtpTransport',
'constructArgs' => ['localhost', 25],
'plugins' => [
[
'class' => 'Swift_Plugins_ThrottlerPlugin',
'constructArgs' => [20],
],
],
],
To send an email, you may use the following code:
Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
->setFrom('from@domain.com')
->setTo($form->email)
->setSubject($form->subject)
->send();
See also http://swiftmailer.org.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component |
$enableSwiftMailerLogging | boolean | Whether to enable writing of the SwiftMailer internal logs using Yii log mechanism. | yii\swiftmailer\Mailer |
$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 | Message default class name. | yii\swiftmailer\Mailer |
$messageConfig | array | The configuration that should be applied to any newly created email message instance by createMessage() or compose(). | yii\mail\BaseMailer |
$swiftMailer | array|\Swift_Mailer | Swift mailer instance or array configuration. This property is read-only. | yii\swiftmailer\Mailer |
$textLayout | string|boolean | Text layout view name. | yii\mail\BaseMailer |
$transport | array|\Swift_Transport | This property is read-only. | yii\swiftmailer\Mailer |
$useFileTransport | boolean | Whether to save email messages as files under $fileTransportPath instead of sending them to the actual recipients. | yii\mail\BaseMailer |
$view | yii\web\View | View instance. Note that the type of this property differs in getter and setter. See getView() and setView() for details. | 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 |
getSwiftMailer() | yii\swiftmailer\Mailer | |
getTransport() | yii\swiftmailer\Mailer | |
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 |
setTransport() | yii\swiftmailer\Mailer | |
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 |
createSwiftMailer() | Creates Swift mailer instance. | yii\swiftmailer\Mailer |
createSwiftObject() | Creates Swift library object, from given array configuration. | yii\swiftmailer\Mailer |
createTransport() | Creates email transport instance by its array configuration. | yii\swiftmailer\Mailer |
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\swiftmailer\Mailer |
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
Whether to enable writing of the SwiftMailer internal logs using Yii log mechanism. If enabled yii\swiftmailer\Logger plugin will be attached to the $transport for this purpose.
See also yii\swiftmailer\Logger.
Message default class name.
Swift mailer instance or array configuration. This property is read-only.
This property is read-only.
Method Details
Creates Swift mailer instance.
protected \Swift_Mailer createSwiftMailer ( ) | ||
return | \Swift_Mailer | Mailer instance. |
---|
Creates Swift library object, from given array configuration.
protected Object createSwiftObject ( array $config ) | ||
$config | array | Object configuration |
return | Object | Created object |
---|---|---|
throws | yii\base\InvalidConfigException | on invalid configuration. |
Creates email transport instance by its array configuration.
protected \Swift_Transport createTransport ( array $config ) | ||
$config | array | Transport configuration. |
return | \Swift_Transport | Transport instance. |
---|---|---|
throws | yii\base\InvalidConfigException | on invalid transport configuration. |
public array|\Swift_Mailer getSwiftMailer ( ) | ||
return | array|\Swift_Mailer | Swift mailer instance or array configuration. |
---|
public array|\Swift_Transport getTransport ( ) |
Sends the specified message.
This method should be implemented by child classes with the actual email sending logic.
protected boolean sendMessage ( $message ) | ||
$message | yii\mail\MessageInterface | The message to be sent |
return | boolean | Whether the message is sent successfully |
---|
public void setTransport ( $transport ) | ||
$transport | array|\Swift_Transport | |
throws | yii\base\InvalidConfigException | on invalid argument. |
---|