Yii::t('app', 'SMTP Host'), 'smtp_port' => Yii::t('app', 'SMTP Port'), 'smtp_username' => Yii::t('app', 'SMTP Username'), 'smtp_nickname' => Yii::t('app', 'Sender'), 'smtp_password' => Yii::t('app', 'SMTP Password'), 'smtp_encryption' => Yii::t('app', 'Encryption'), ]; } /** * @inheritdoc */ public function rules() { return [ [['smtp_host', 'smtp_username', 'smtp_nickname', 'smtp_password', 'smtp_encryption'], 'string'], [['smtp_port'], 'integer'], [['smtp_host', 'smtp_username'], 'required'], [['smtp_port'], 'compare', 'compareValue' => 0, 'operator' => '>='], [['smtp_port'], 'compare', 'compareValue' => 65535, 'operator' => '<='], ]; } /** * 填充smtp配置 * */ public function init() { parent::init(); $names = $this->getNames(); $models = Options::find()->where(["in", "name", $names])->indexBy("name")->all(); foreach ($names as $name) { if (isset($models[$name])) { $this->$name = $models[$name]->value; } else { $this->name = ''; } } } /** * 写入smtp配置 * * @return bool */ public function setSMTPSettingConfig() { $names = $this->getNames(); foreach ($names as $name) { $model = self::findOne(['name' => $name]); if ($model != null) { $value = $this->$name; $value === null && $value = ''; $model->value = $value; $result = $model->save(false); } else { $model = new Options(); $model->name = $name; $model->value = ''; $result = $model->save(false); } if ($result == false) { return false; } } return true; } /** * 获取smtp邮箱配置 * * @return array * @throws \yii\base\InvalidConfigException */ public static function getComponentConfig() { $config = Yii::createObject( self::className() ); return [ 'useFileTransport' => false, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => $config->smtp_host, 'username' => $config->smtp_username, 'password' => $config->smtp_password, 'port' => $config->smtp_port, 'encryption' => $config->smtp_encryption, ], 'messageConfig' => [ 'charset' => 'UTF-8', 'from' => [$config->smtp_username => $config->smtp_nickname] ], ]; } }