Class yii\swiftmailer\Mailer

Inheritanceyii\swiftmailer\Mailer » yii\mail\BaseMailer
Available since version2.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

Hide inherited properties

PropertyTypeDescriptionDefined 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

Protected Methods

Hide inherited methods

MethodDescriptionDefined 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

$enableSwiftMailerLogging public property

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.

$messageClass public property

Message default class name.

public string $messageClass 'yii\swiftmailer\Message'

Method Details

createSwiftMailer() protected method

Creates Swift mailer instance.

protected \Swift_Mailer createSwiftMailer ( )
return \Swift_Mailer

Mailer instance.

createSwiftObject() protected method

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.

createTransport() protected method

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.

getSwiftMailer() public method

public array|\Swift_Mailer getSwiftMailer ( )
return array|\Swift_Mailer

Swift mailer instance or array configuration.

getTransport() public method

public array|\Swift_Transport getTransport ( )
sendMessage() protected method

protected void sendMessage ( $message )
$message
setTransport() public method

public void setTransport ( $transport )
$transport array|\Swift_Transport
throws \yii\base\InvalidConfigException

on invalid argument.