举杯邀月

Yii2 动态加载邮箱配置发送邮件,避免发送频繁而发送失败

摘要:工作中有些业务需要频繁的发送邮件进行通知,但是发送频繁会导致邮件发送失败,这是邮箱服务的一种限制机制,不管你是用的QQ、163还是谷歌都会遇到这样的情况,然后经过讨论找了个‘野路子’减低发送失败的可能。

工作中有些业务需要频繁的发送邮件进行通知,但是发送频繁会导致邮件发送失败,这是邮箱服务的一种限制机制,不管你是用的QQ、163还是谷歌都会遇到这样的情况,然后经过讨论找了个‘野路子’减低发送失败的可能。

EmailConfigService.php

class EmailConfigService
{

    public static $config = [
        ['email'=>'email1', 'password'=>'password', 'host'=>'smtp.163.com'],
        ['email'=>'email2', 'password'=>'password', 'host'=>'smtp.163.com'],
        ['email'=>'email3', 'password'=>'password', 'host'=>'smtp.sina.com'],
        ['email'=>'email4', 'password'=>'password', 'host'=>'smtp.mxhichina.com'],
    ];


    public function getConfig()
    {
        $num = \Yii::$app->redis->get('email_polling');
        $num = $num && isset(self::$config[$num]) ? $num : 0;
        $config = self::$config[$num];
        \Yii::$app->redis->set('email_polling', ($num+1));
        return [
            'transport' => [
                'host'       => $config['host'],
                'username'   => $config['email'],
                'password'   => $config['password'],
            ],
            'messageConfig'  => [
                'from'       => [$config['email'] => '玖伍玖网络科技有限公司']
            ],
        ];
    }

    public function setConfig($num=0)
    {
        try{
            $result = \Yii::$app->getComponents()['mailer'];
            unset($result['messageConfig']['from']);
            $config = \yii\helpers\ArrayHelper::merge($result, $this->getConfig());
            \Yii::$app->set('mailer', $config);
            $ping = \Yii::$app->mailer->transport->ping();
            if(empty($ping)){
                if($num > 3) return false;
                $num++;$this->setConfig($num);
            }
            return $config;
        }catch (\Exception $e){
            ToolService::writeInfo('邮件配置异常,错误文件:'.$e->getFile().',错误行数:'.$e->getLine().',错误信息:'.$e->getMessage(), 'email_config_error');
            return false;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

$num = \Yii::$app->redis->get(‘email_polling’); 是用来记录发送的位置。

SendEmailService.php

class SendEmailService extends EmailConfigService
{

    public $params;

    public function send()
    {
        $config = $this->setConfig();
        if(empty($config)) return '邮箱配置错误';
        if(empty($this->params['to_email'])) return '接收邮箱不能为空';
        if(empty($this->params['content'])) return '发送内容不能为空';
        $subject = !empty($this->params['subject']) ?  $this->params['subject'] : '重要通知';
        $result = \Yii::$app->mailer->compose()
            ->setTo($this->params['to_email'])
            ->setSubject($subject)
            ->setHtmlBody($this->params['content'])
            ->send();
        if(!$result) return '邮件发送失败!';
      
        return true;
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

然后执行就可以实现轮询配置发送邮件了

$config = [
    'class'=>'SendEmailService',
    'params' => [
        'to_email' => ['to_email'],
        'content' => '发送内容',
        'subject' => 'subject'
    ]
];
\Yii::createObject($config)->send();
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9

作者:举杯邀月

出处: http://www.hug-code.cn/archives/5fca0579488be.html

2020-09-10 标签: phpyii2