Class yii\swiftmailer\Mailer
Inheritance | yii\swiftmailer\Mailer » yii\mail\BaseMailer |
---|---|
Available since version | 2.0 |
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 \yii\swiftmailer\transport property. In that case, the default
PHP mail()
function 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 |
---|---|---|---|
$enableSwiftMailerLogging | boolean | Whether to enable writing of the SwiftMailer internal logs using Yii log mechanism. | yii\swiftmailer\Mailer |
$messageClass | string | Message default class name. | yii\swiftmailer\Mailer |
Public Methods
Method | Description | Defined By |
---|---|---|
getSwiftMailer() | yii\swiftmailer\Mailer | |
getTransport() | yii\swiftmailer\Mailer | |
setTransport() | yii\swiftmailer\Mailer |
Protected Methods
Method | Description | Defined By |
---|---|---|
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 |
sendMessage() | yii\swiftmailer\Mailer |
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 \yii\swiftmailer\transport for this purpose.
See also yii\swiftmailer\Logger.
Message default class name.
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 ( ) |
protected void sendMessage ( $message ) | ||
$message |
public void setTransport ( $transport ) | ||
$transport | array|\Swift_Transport | |
throws | \yii\base\InvalidConfigException | on invalid argument. |
---|