Swiftmailer setTransport() delivers NULL

Hi guys,
in my config file - common/config/main-local.php I use to define mail configuration like this:

  'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false, //set this property to false to send mails to real email addresses
        //comment the following array to send mail using php's mail function
        'transport' => [
            'class' => 'Swift_SmtpTransport',
        ],
    ],

In my Controller, I just noticed, that $mailer always will be null. This code worked for a long time, since now. What happend with setTransport(). Are there any alternatives defining mail server data dynamically?

private function FetchMailServerData() {
    try {
        $checkServerConf = Mailserver::find()->count('id');
        if ($checkServerConf < 1)
            return false;
        $serverId = Mailserver::find()->min('id');
        $host = Mailserver::findOne(['id' => $serverId])->serverHost;
        $username = Mailserver::findOne(['id' => $serverId])->username;
        $password = Mailserver::findOne(['id' => $serverId])->password;
        $port = Mailserver::findOne(['id' => $serverId])->port;
        $useEncryption = Mailserver::findOne(['id' => $serverId])->useEncryption;
        if ($useEncryption == 1)
            $encryption = Mailserver::findOne(['id' => $serverId])->encryption;
        else
            $encryption = null;
        if ($encryption != null)
            $mailer = Yii::$app->mailer->setTransport([
                'class' => 'Swift_SmtpTransport',
                'host' => $host,
                'username' => $username,
                'password' => $password,
                'port' => $port,
                'encryption' => $encryption
            ]);
        else
            $mailer = Yii::$app->mailer->setTransport([
                'class' => 'Swift_SmtpTransport',
                'host' => $host,
                'username' => $username,
                'password' => $password,
                'port' => $port
            ]);
        return $mailer;
    } catch (yii\db\Exception $e) {
        error_handling::error_without_id($e, MailController::RenderBackInCaseOfError);
    }
}

Following statement always will be null. Why??

          var_dump(Yii::$app->mailer->setTransport([
                'class' => 'Swift_SmtpTransport',
                'host' => $host,
                'username' => $username,
                'password' => $password,
                'port' => $port
            ]));

whereas var_dump(Yii::$app->mailer); will deliver this:

object(yii\swiftmailer\Mailer)#319 (16) { ["messageClass"]=&gt; string(23) "yii\swiftmailer\Message" ["enableSwiftMailerLogging"]=&gt; bool(false) ["_swiftMailer":"yii\swiftmailer\Mailer":private]=&gt; NULL ["_transport":"yii\swiftmailer\Mailer":private]=&gt; array(6) { ["class"]=&gt; string(19) "Swift_SmtpTransport" ["host"]=&gt; string(12) "mail.gmx.net" ["username"]=&gt; string(19) "kipp.thomas@gmx.net" ["password"]=&gt; string(8) "TopSecret" ["port"]=&gt; int(587) ["encryption"]=&gt; string(3) "tls" } ["htmlLayout"]=&gt; string(12) "layouts/html" ["textLayout"]=&gt; string(12) "layouts/text" ["messageConfig"]=&gt; array(0) { } ["useFileTransport"]=&gt; bool(false) ["fileTransportPath"]=&gt; string(13) "@runtime/mail" ["fileTransportCallback"]=&gt; NULL ["_view":"yii\mail\BaseMailer":private]=&gt; array(0) { } ["_viewPath":"yii\mail\BaseMailer":private]=&gt; string(42) "X:\xampp\htdocs\yii2_ErkanImmo\common/mail" ["_message":"yii\mail\BaseMailer":private]=&gt; NULL ["_events":"yii\base\Component":private]=&gt; array(0) { } ["_eventWildcards":"yii\base\Component":private]=&gt; array(0) { } ["_behaviors":"yii\base\Component":private]=&gt; NULL }

It’s because setTransport() returns nothing. If you want to set it dynamically just do it like you did but use mailer and not the result of setTransport().

Yii::$app->mailer // object
Yii::$app->mailer->setTransport(...) // null
Yii::$app->mailer // still object but now with configured transport so you can do like:

Yii::$app->mailer->compose(...)