利用redis构建消息队列来实现邮件的发送

1.具体思路

  1. 获取一定数量的用户邮件信息
  2. 使用redis的list数据类型,按照队列的形式将其保存在缓存中
  3. 利用队列先进先出的特性,将信息从缓存中读取
  4. 发送邮件,成功则将此记录从缓存中移除,失败则继续添加到缓存中

2.具体实现

  1. 构建缓存操作类
    redis = new Redis();
            $this->config = $config;
        }
        //仿制克隆对象
        private function __clone()
        {
            // TODO: Implement __clone() method.
        }
    
        /**
         * 实例化对象
         * @param $config
         * @return RedisList
         */
        public static function getInstance($config)
        {
            if(!self::$instance instanceof self){
                //检测$config
                if(empty($config)){
                    $config['host'] = '127.0.0.1';
                    $config['port'] = 6379;
                }
                self::$instance = new self($config);
            }
            return self::$instance;
        }
    
        /**
         * 连接redis服务器
         */
        public function connect()
        {
            $this->redis->connect($this->config['host'], $this->config['port']);
        }
    
        /**
         * 构建缓存队列
         * @param array $arr
         * @return bool
         */
        public function setList($arr = [])
        {
            //对输入数组进行检测
            if(empty($arr)){
                return false;
            }else{
                foreach ($arr as $k=>$v){
                    $this->redis->lPush('email', $v);
                }
            }
            return true;
        }
    
        /**
         * 弹出队列最后一个元素
         * @return string
         */
        public function getList()
        {
            return $this->redis->rPop('email');
        }
    }
    

     

  2. 利用PHPMailer,来构建邮件发送类

下载地址:https://github.com/PHPMailer/PHPMailer

config = $config;
        }
    }

    /**
     * 发送邮件
     * @param $toAddress
     * @param $toUsername
     * @param $subject
     * @param $body
     * @param $altBody
     * @return bool|string
     */
    public function sendEmail($toAddress, $toUsername, $subject, $body, $altBody)
    {
        // Instantiation and passing `true` enables exceptions
        $mail = new PHPMailer(true);

        try {
            $mail->isSMTP();                                            // Set mailer to use SMTP
            $mail->Host       = $this->config['Host'];//'smtp.aliyun.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
            $mail->Username   = $this->config['Username'];//'[email protected]';                     // SMTP username
            $mail->Password   = $this->config['Password'];//'gygsmwj1';                                 // SMTP password
            $mail->CharSet = $this->config['CharSet'];//'UTF-8';

            //Recipients
            $mail->setFrom($this->config['Username'], $this->config['FromName']);
            $mail->addAddress($toAddress, $toUsername);     // Add a recipient

            // Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;//'From aliyun-a';
            $mail->Body    = $body;//'I am aliyun-a;Hello 世界!';
            $mail->AltBody = $altBody;//'I am aliyun-a;';

            $mail->send();
            return true;
        } catch (Exception $e) {
            return $mail->ErrorInfo;
        }
    }
}

3.发送邮件

$v){
    $arr[] = $v;
}
$config = [
    'redis' => [
        'host' => '127.0.0.1',
        'port' => 6379
    ],
    'email' => [
        'Host' => 'emailServer',
        'Username' => 'emial',
        'Password' => 'password',
        'CharSet' => 'UTF-8',
        'FromName' => 'name'
    ]
];
$redis = RedisList::getInstance($config['redis']);
$mailer = new Mailer($config['email']);
$redis->connect();
if($redis->setList($arr)){
    echo '1111
'; }else{ echo '2222
'; } while(true){ $value = $redis->getList(); if(!$value){ break; } if($result = $mailer->sendEmail($value, $value, 'This is a test;' . $value, file_get_contents('model.html'), '这是一个测试啊!') !== true){ $redis->getList(["$value"]); var_dump($result); } }

 

你可能感兴趣的:(#,php)