Class yii\swiftmailer\Mailer

Inheritanceyii\swiftmailer\Mailer » yii\mail\BaseMailer
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-swiftmailer/blob/master/src/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

Hide inherited 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
$swiftMailer \Swift_Mailer Swift mailer instance. yii\swiftmailer\Mailer
$transport \Swift_Transport yii\swiftmailer\Mailer

Protected Methods

Hide inherited 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

Hide inherited properties

$enableSwiftMailerLogging public property (available since version 2.0.4)

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.

$messageClass public property

Message default class name.

public string $messageClass 'yii\swiftmailer\Message'
$swiftMailer public property

Swift mailer instance.

public \Swift_Mailer $swiftMailer null
$transport public property
public \Swift_Transport $transport null

Method Details

Hide inherited methods

createSwiftMailer() protected method

Creates Swift mailer instance.

protected \Swift_Mailer createSwiftMailer ( )
return \Swift_Mailer

Mailer instance.

                protected function createSwiftMailer()
{
    return new \Swift_Mailer($this->getTransport());
}

            
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.

                protected function createSwiftObject(array $config)
{
    if (isset($config['class'])) {
        $className = $config['class'];
        unset($config['class']);
    } else {
        throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
    }
    if (isset($config['constructArgs'])) {
        $args = [];
        foreach ($config['constructArgs'] as $arg) {
            if (is_array($arg) && isset($arg['class'])) {
                $args[] = $this->createSwiftObject($arg);
            } else {
                $args[] = $arg;
            }
        }
        unset($config['constructArgs']);
        $object = Yii::createObject($className, $args);
    } else {
        $object = Yii::createObject($className);
    }
    if (!empty($config)) {
        $reflection = new \ReflectionObject($object);
        foreach ($config as $name => $value) {
            if ($reflection->hasProperty($name) && $reflection->getProperty($name)->isPublic()) {
                $object->$name = $value;
            } else {
                $setter = 'set' . $name;
                if ($reflection->hasMethod($setter) || $reflection->hasMethod('__call')) {
                    $object->$setter($value);
                } else {
                    throw new InvalidConfigException('Setting unknown property: ' . $className . '::' . $name);
                }
            }
        }
    }
    return $object;
}

            
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.

                protected function createTransport(array $config)
{
    if (!isset($config['class'])) {
        $config['class'] = 'Swift_SendmailTransport';
    }
    if (isset($config['plugins'])) {
        $plugins = $config['plugins'];
        unset($config['plugins']);
    } else {
        $plugins = [];
    }
    if ($this->enableSwiftMailerLogging) {
        $plugins[] = [
            'class' => 'Swift_Plugins_LoggerPlugin',
            'constructArgs' => [
                [
                    'class' => 'yii\swiftmailer\Logger'
                ]
            ],
        ];
    }
    /* @var $transport \Swift_Transport */
    $transport = $this->createSwiftObject($config);
    if (!empty($plugins)) {
        foreach ($plugins as $plugin) {
            if (is_array($plugin) && isset($plugin['class'])) {
                $plugin = $this->createSwiftObject($plugin);
            }
            $transport->registerPlugin($plugin);
        }
    }
    return $transport;
}

            
getSwiftMailer() public method

public \Swift_Mailer getSwiftMailer ( )
return \Swift_Mailer

Swift mailer instance.

                public function getSwiftMailer()
{
    if (!is_object($this->_swiftMailer)) {
        $this->_swiftMailer = $this->createSwiftMailer();
    }
    if (!$this->_transport->ping()) {
        $this->_transport->stop();
        $this->_transport->start();
    }
    return $this->_swiftMailer;
}

            
getTransport() public method

public \Swift_Transport getTransport ( )

                public function getTransport()
{
    if (!is_object($this->_transport)) {
        $this->_transport = $this->createTransport($this->_transport);
    }
    return $this->_transport;
}

            
sendMessage() protected method

protected void sendMessage ( $message )
$message

                protected function sendMessage($message)
{
    /* @var $message Message */
    $address = $message->getTo();
    if (is_array($address)) {
        $address = implode(', ', array_keys($address));
    }
    Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
    return $this->getSwiftMailer()->send($message->getSwiftMessage()) > 0;
}

            
setTransport() public method

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

on invalid argument.

                public function setTransport($transport)
{
    if (!is_array($transport) && !is_object($transport)) {
        throw new InvalidConfigException('"' . get_class($this) . '::transport" should be either object or array, "' . gettype($transport) . '" given.');
    }
    $this->_transport = $transport;
    $this->_swiftMailer = null;
}