MailConfig.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: User
  5. * Date: 2019/6/19
  6. * Time: 下午 5:13
  7. */
  8. namespace backend\models;
  9. use Yii;
  10. /**
  11. * Class MailConfig 发件箱配置模型
  12. * @package backend\models
  13. */
  14. class MailConfig extends \yii\db\ActiveRecord
  15. {
  16. /**
  17. * @inheritdoc
  18. */
  19. public static function tableName()
  20. {
  21. return 'crm_mail_config';
  22. }
  23. /**
  24. * @return \yii\db\Connection the database connection used by this AR class.
  25. */
  26. public static function getDb()
  27. {
  28. return Yii::$app->get('dbXcrm');
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['smtp_username', 'host', 'password','port','add_time'], 'required'],
  37. [['is_check','is_enable'], 'number'],
  38. [['send_mail'], 'string'],
  39. ];
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => '主键ID',
  48. 'send_mail' => '发件人名称',
  49. 'smtp_username' => 'smtp用户名',
  50. 'host' => 'smtp服务域名',
  51. 'port' => '端口',
  52. 'add_time' => '添加时间',
  53. 'is_enable' => '是否启用',
  54. 'is_check' => '选中优先级',
  55. ];
  56. }
  57. /**
  58. * @param $subject string 邮箱发送标题
  59. * @param string $receiver string 收件人
  60. * @return \yii\db\ActiveRecord
  61. */
  62. public function getMailConfig($receiver='')
  63. {
  64. $time = time() - 300;//排除5分钟内给同一个客户发送邮件失败的发件箱配置
  65. $sql = "select distinct mail_num from crm_mail_record where status = 0 and receiver = '$receiver' and in_time > $time";
  66. $mail_arr = MailRecord::findBySql($sql)->asArray()->all();
  67. $mail_nums = implode(',',array_column($mail_arr,'mail_num'));
  68. if(empty($mail_nums)){
  69. $sql = "select * from crm_mail_config where is_enable = 0 order by is_check desc limit 1";
  70. }else{
  71. $sql = "select * from crm_mail_config where is_enable = 0 and id not in ($mail_nums) order by is_check desc limit 1";
  72. }
  73. $config_arr = MailConfig::findBySql($sql)->asArray()->all()[0];
  74. return $config_arr;
  75. }
  76. }