从模型配置邮件程序参数 - Yii2 [英] Config mailer parameters from model - Yii2

查看:33
本文介绍了从模型配置邮件程序参数 - Yii2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Yii2,我想配置邮件程序参数以从 db 获取数据.

im using Yii2 and i want to config mailer parameters geting the data from 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 不能从模型调用方法,我试过但抛出错误

but from web.php can't call methods from models, i tried but throws a error

推荐答案

感谢@Onedev.Link 和@arogachev 的回答.这给了我一个想法,我解决了问题.

thanks to @Onedev.Link and @arogachev for his answer.that gave me an idea and i solve the problem.

我解决了修改 swiftmailer 组件的问题,在 Mailer.php 中添加了这个:

i solve the problem modyfing swiftmailer component, in Mailer.php added this:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆