yiisoft / yii2-swiftmailer

Yii 2 swiftmailer extension.
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
116 stars 74 forks source link

yii\swiftmailer\Mailer::setTransport has no effect after sending of first message #61

Closed dmitry-kulikov closed 6 years ago

dmitry-kulikov commented 6 years ago

What steps will reproduce the problem?

// you have to setup these variables
$host = '';
$username = '';
$password = '';
$port = '';
$recipient = '';

$transport = [
    'class' => 'Swift_SmtpTransport',
    'encryption' => 'tls',
    'host' => $host,
    'username' => $username,
    'password' => $password,
    'port' => $port,
];

$message = (new \yii\swiftmailer\Message())
    ->setFrom('example@example.com')
    ->setTo($recipient)
    ->setSubject('Test')
    ->setTextBody('Hello world!');

Yii::$app->mailer->setTransport($transport);
Yii::$app->mailer->send($message);

$transport['password'] = null;
// wrong password, we expect Swift_TransportException

Yii::$app->mailer->setTransport($transport);
Yii::$app->mailer->send($message);

What's expected?

Message successfully sent for the first time, the second time - Swift_TransportException.

What do you get instead?

Message sent successfully twice.

Additional info

Q A
Yii version 2.0.15.1
Yii SwiftMailer version 2.1.0
SwiftMailer version 5.4.9
PHP version 5.5.9
Operating system Ubuntu 14.04

setTransport has no effect on further emails sending because Swift_Mailer object is cached after first call of getSwiftMailer https://github.com/yiisoft/yii2-swiftmailer/blob/0d5ad79a6debe5fbb753c2ce4a89a7139e4acfe6/src/Mailer.php#L105-L112 While it can be workarounded, I think current behavior is unexpected.