从模型配置邮件程序参数 - Yii2
Config mailer parameters from model - Yii2
我正在使用 Yii2,我想配置从 db 获取数据的邮件程序参数。
示例:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'enableSwiftMailerLogging' =>true,
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => $model->getSmtpHost(),
'username' => $model->getSmtpUser(),
'password' => $model->getSmtpPass(),
'port' => $model->getSmtpPort(),
'encryption' => $model->getSmtpEncryption(),
],
]
但是 web.php 无法从模型中调用方法,我尝试了但是抛出错误
Yii 从此配置初始化了应用程序。在运行 yii2 之前,您不能使用 yii2。
$application = new yii\web\Application($config);
作为替代方案,您可以在 bootstrap.php 文件中创建应用程序后配置邮件程序,如下所示:Yii::$app->set('mailer', (new MailerConfigurator())->getConfig());
感谢@Onedev.Link 和@arogachev 的 answer.that 给了我一个想法,我解决了这个问题。
我解决了modyfing swiftmailer组件的问题,在Mailer.php中添加了这个:
use app\models\Administracion; //The model i needed for access bd
class Mailer extends BaseMailer
{
...
...
//this parameter is for the config (web.php)
public $CustomMailerConfig = false;
...
...
...
/**
* Creates Swift mailer instance.
* @return \Swift_Mailer mailer instance.
*/
protected function createSwiftMailer()
{
if ($this->CustomMailerConfig) {
$model = new Administracion();
$this->setTransport([
'class' => 'Swift_SmtpTransport',
'host' => $model->getSmtpHost(),
'username' => $model->getSmtpUser(),
'password' => $model->getSmtpPass(),
'port' => $model->getSmtpPort(),
'encryption' => $model->getSmtpEncryption(),
]);
}
return \Swift_Mailer::newInstance($this->getTransport());
}
并在 Web.php 中添加:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'enableSwiftMailerLogging' =>true,
'CustomMailerConfig' => true, //if its true use the bd config else set the transport here
'useFileTransport' => false,
],
我正在使用 Yii2,我想配置从 db 获取数据的邮件程序参数。
示例:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'enableSwiftMailerLogging' =>true,
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => $model->getSmtpHost(),
'username' => $model->getSmtpUser(),
'password' => $model->getSmtpPass(),
'port' => $model->getSmtpPort(),
'encryption' => $model->getSmtpEncryption(),
],
]
但是 web.php 无法从模型中调用方法,我尝试了但是抛出错误
Yii 从此配置初始化了应用程序。在运行 yii2 之前,您不能使用 yii2。
$application = new yii\web\Application($config);
作为替代方案,您可以在 bootstrap.php 文件中创建应用程序后配置邮件程序,如下所示:Yii::$app->set('mailer', (new MailerConfigurator())->getConfig());
感谢@Onedev.Link 和@arogachev 的 answer.that 给了我一个想法,我解决了这个问题。
我解决了modyfing swiftmailer组件的问题,在Mailer.php中添加了这个:
use app\models\Administracion; //The model i needed for access bd
class Mailer extends BaseMailer
{
...
...
//this parameter is for the config (web.php)
public $CustomMailerConfig = false;
...
...
...
/**
* Creates Swift mailer instance.
* @return \Swift_Mailer mailer instance.
*/
protected function createSwiftMailer()
{
if ($this->CustomMailerConfig) {
$model = new Administracion();
$this->setTransport([
'class' => 'Swift_SmtpTransport',
'host' => $model->getSmtpHost(),
'username' => $model->getSmtpUser(),
'password' => $model->getSmtpPass(),
'port' => $model->getSmtpPort(),
'encryption' => $model->getSmtpEncryption(),
]);
}
return \Swift_Mailer::newInstance($this->getTransport());
}
并在 Web.php 中添加:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'enableSwiftMailerLogging' =>true,
'CustomMailerConfig' => true, //if its true use the bd config else set the transport here
'useFileTransport' => false,
],