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