catchAdmin+phpEmailer批量发邮件

发布时间 2023-05-29 15:28:53作者: 糖粿

前端参数

 

 后端逻辑

    //多个邮箱配置
    public function system()
    {
        $email_type = input('email_type','1');

        $field = 'id,smtp,smtp_port,sender_email_adress,smtp_name,smtp_code,encryption_type';
        $where[] = ['deleted_at','=','0'];

        if($email_type == '1'){
            //随机全部邮箱配置
            return $this->field($field)
                ->where($where)
                ->orderRaw("rand()")
                ->limit(10)
                ->select()
                ->toArray();
        }elseif($email_type == '2'){  //选择指定邮箱
            return $this->field($field)
                ->where($where)
                ->select()
                ->toArray();
        }
    }

 

    public function getOrderInfo($ids,$page = 0,$limit = 30)
    {
        $order = new OrderModel();
        $field = 'buyer as name,order_id as order_num,product_name as product,id,email';

        return $order->whereIn('id',$ids)
            ->field($field)
            ->order('id','asc')
            ->limit($page,$limit)
            ->select()
            ->toArray();
    }

 

    public function tplInfo()
    {
        $type_id = input('type_id','');
        if(empty($type_id)){
            return CatchResponse::fail('请选择模板类型');
        }

        $temp[] = ['deleted_at','=','0'];
        $temp[] = ['type_id','=',$type_id];
        $info = $this->field('id,type_id,country_id,tpl_title,tpl_content,variable,remark')
            ->where($temp)
            ->find()->toArray();
        //$info['tpl_content'] = strip_tags(htmlspecialchars_decode($info['tpl_content']));
        return $info;
    }

 

 public function sendEmail()
    {
        $type_id = input('type_id','1');
        if(empty($type_id)){
            return CatchResponse::fail('模板类型不能为空');
        }
        $order_id = input('order_id','');
        $order_ids = explode(',',$order_id);
        //读取邮箱配置
        $systemModel = new emailSystem();
        $system = $systemModel->system();
        if(is_array($system)){
            $emailTotal = count($system);
            $order_idTotal = count($order_ids);
            $aug = floor($order_idTotal / $emailTotal);
            foreach ($system as $k =>$sty){
                $mail = new PHPMailer(true);
                //服务器配置
                $mail->CharSet ="UTF-8";                         //设定邮件编码
                $mail->SMTPDebug = 0;                            // 调试模式输出
                $mail->isSMTP();                                 // 使用SMTP
                $mail->SMTPAuth = true;                          // 允许 SMTP 认证
                $mail->Host = $sty["smtp"];                      // SMTP服务器
                $mail->Username = $sty['sender_email_adress'];   // SMTP 用户名  即邮箱的用户名
                $mail->Password = $sty['smtp_code'];             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
                $mail->SMTPSecure = $sty['encryption_type'];     // 允许 TLS 或者ssl协议
                $mail->Port = $sty['smtp_port'];                 // 服务器端口 25 或者465 具体要看邮箱服务器支持
                $mail->setFrom($sty['sender_email_adress']);     //发件人
                $mail->isHTML(true);                       // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容

                if($k == count($system)){
                    $orderlist = $this->getOrderInfo($order_ids,$k,($aug*2)); //订单信息
                    $orderlist2 = $this->getOrderInfo($order_ids,$k+1,$aug); //订单信息
                    $orderlist = array_merge($orderlist,$orderlist2);
                }else{
                    $orderlist = $this->getOrderInfo($order_ids,$k,$aug); //订单信息
                }

                //获取模板信息
                $template = new template();
                $temInfo  = $template->tplInfo();
                $mail->Subject = $temInfo['tpl_title'];  //主题
                $content = $temInfo['tpl_content'];
                $variable = explode(',',$temInfo['variable']);  //变量

                if(is_array($temInfo)){
                    foreach ($orderlist as $email){
                        $mail->addAddress($email['email']);// 收件人
                        $subject = str_replace($variable,$email,$content);
                        $mail->Body = $subject;

                        if(!$mail->send()){
                            EmailModel::create([
                                'template_type_id'=> $type_id,
                                'sender_email'    => $sty['sender_email_adress'],
                                'buyer_id'        => $email['id'],
                                'buyer_email'     => $email['email'],
                                'email_title'     => $temInfo['tpl_title'],
                                'email_content'   => $subject,
                                'status'          => 0,
                            ]);
                        }else{
                            EmailModel::create([
                                'template_type_id'=> $type_id,
                                'sender_email'    => $sty['sender_email_adress'],
                                'buyer_id'        => $email['id'],
                                'buyer_email'     => $email['email'],
                                'email_title'     => $temInfo['tpl_title'],
                                'email_content'   => $subject,
                                'status'          => 1,
                            ]);
                        }
                    }
                }
            }
        }
        $where[] = ['deleted_at','=','0'];
        $where[] = ['buyer_id','in',$order_id];
        $email = new EmailModel();
        return $email->where($where)->count();
    }