SettingSMTPForm.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-10-21 14:40
  7. */
  8. namespace backend\models\form;
  9. use Yii;
  10. use common\models\Options;
  11. class SettingSMTPForm extends \common\models\Options
  12. {
  13. public $smtp_host;
  14. public $smtp_port = 25;
  15. public $smtp_username;
  16. public $smtp_nickname;
  17. public $smtp_password;
  18. public $smtp_encryption = 'tls';
  19. /**
  20. * @inheritdoc
  21. */
  22. public function attributeLabels()
  23. {
  24. return [
  25. 'smtp_host' => Yii::t('app', 'SMTP Host'),
  26. 'smtp_port' => Yii::t('app', 'SMTP Port'),
  27. 'smtp_username' => Yii::t('app', 'SMTP Username'),
  28. 'smtp_nickname' => Yii::t('app', 'Sender'),
  29. 'smtp_password' => Yii::t('app', 'SMTP Password'),
  30. 'smtp_encryption' => Yii::t('app', 'Encryption'),
  31. ];
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['smtp_host', 'smtp_username', 'smtp_nickname', 'smtp_password', 'smtp_encryption'], 'string'],
  40. [['smtp_port'], 'integer'],
  41. [['smtp_host', 'smtp_username'], 'required'],
  42. [['smtp_port'], 'compare', 'compareValue' => 0, 'operator' => '>='],
  43. [['smtp_port'], 'compare', 'compareValue' => 65535, 'operator' => '<='],
  44. ];
  45. }
  46. /**
  47. * 填充smtp配置
  48. *
  49. */
  50. public function init()
  51. {
  52. parent::init();
  53. $names = $this->getNames();
  54. $models = Options::find()->where(["in", "name", $names])->indexBy("name")->all();
  55. foreach ($names as $name) {
  56. if (isset($models[$name])) {
  57. $this->$name = $models[$name]->value;
  58. } else {
  59. $this->name = '';
  60. }
  61. }
  62. }
  63. /**
  64. * 写入smtp配置
  65. *
  66. * @return bool
  67. */
  68. public function setSMTPSettingConfig()
  69. {
  70. $names = $this->getNames();
  71. foreach ($names as $name) {
  72. $model = self::findOne(['name' => $name]);
  73. if ($model != null) {
  74. $value = $this->$name;
  75. $value === null && $value = '';
  76. $model->value = $value;
  77. $result = $model->save(false);
  78. } else {
  79. $model = new Options();
  80. $model->name = $name;
  81. $model->value = '';
  82. $result = $model->save(false);
  83. }
  84. if ($result == false) {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. /**
  91. * 获取smtp邮箱配置
  92. *
  93. * @return array
  94. * @throws \yii\base\InvalidConfigException
  95. */
  96. public static function getComponentConfig()
  97. {
  98. $config = Yii::createObject( self::className() );
  99. return [
  100. 'useFileTransport' => false,
  101. 'transport' => [
  102. 'class' => 'Swift_SmtpTransport',
  103. 'host' => $config->smtp_host,
  104. 'username' => $config->smtp_username,
  105. 'password' => $config->smtp_password,
  106. 'port' => $config->smtp_port,
  107. 'encryption' => $config->smtp_encryption,
  108. ],
  109. 'messageConfig' => [
  110. 'charset' => 'UTF-8',
  111. 'from' => [$config->smtp_username => $config->smtp_nickname]
  112. ],
  113. ];
  114. }
  115. }