首页 > 编程语言 >catchAdmin+phpEmailer批量发邮件

catchAdmin+phpEmailer批量发邮件

时间:2023-05-29 15:36:49浏览次数:49  
标签:发邮件 phpEmailer where email catchAdmin mail order type id

前端参数

 

 后端逻辑

    //多个邮箱配置
    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();
    }

 

标签:发邮件,phpEmailer,where,email,catchAdmin,mail,order,type,id
From: https://www.cnblogs.com/silen0119/p/17440592.html

相关文章

  • python 使用企业微信发邮件加加图形化界面
    importsysfromPyQt5.QtWidgetsimportQApplication,QWidget,QLabel,QLineEdit,QTextEdit,QPushButtonimportrequests,base64,hashlibimportsmtplibimportosimportglobfromemail.mime.textimportMIMETextfromemail.mime.multipartimportMIMEMultipa......
  • JavaMail读发邮件
    SMTP(SimpleMailTransferProtocol),即简单邮件传输协议,它定义了发送电子邮件的机制。在JavaMailAPI环境中,基于JavaMail的程序将和您的公司或因特网服务供应商的(InternetServiceProvider's,ISP's)SMTP服务器通信。SMTP服务器可将消息中转至接收方SMTP服务器,以便最终让用户......
  • JavaMail收发邮件的步骤
     发邮件1)获取Sessionn()方法ii)New一个上面类的实例,设置用户名和密码 andmail.smtp.auth属性iv)同过Session的静态方法,获取一个Session实例2)生成Messagei)没有附件的邮件 第一步:new一个MimeMessage实例(根据Session) 第二步:给Message实例设置su......
  • python 发邮件 图片作附件
    importsmtplibfromemail.mime.textimportMIMETextfromemail.mime.multipartimportMIMEMultipartfromemail.mime.imageimportMIMEImage#邮件发件人和收件人信息sender_email='你的Gmail地址'sender_password='你的Gmail密码'recipient_email='收......
  • python发邮件|4-20
    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。首先,我们来构造一个最简单的纯文本邮件:fromemail.mime.textimportMIMETextmsg=MIMEText('hello......
  • 每次记歌词——使用Python自动发邮件提醒你记歌词
    今日鸡汤八骏日行三万里,穆王何事不重来。大家好,我是Python进阶者。前言前几天在Python交流群里边,【冫马讠成】大佬分享了一个有趣的代码,用于定时发送歌词到邮箱,觉得挺有意思,这里拿出来给大家分享。实现思路实现思路倒是不难,其一是Python网络爬虫,将网页上的歌词信息抓取到,然后存起......
  • Zabbix监控使用ping判断主机是否存活并发邮件报警
    一、在Zabbix服务器安装gcc和fpingyum-yinstallgccwgethttp://www.fping.org/dist/fping-3.10.tar.gztar-xffping-3.10.tar.gzcdfping-3.10./configure--prefix......
  • 发邮件 In App Email (实例)
    InAppEmailInthistutorialiwillbeshowingyouhowtodirectlyemailfromyourappFeatures: 1RoundRectButton Inappemailingisagreatfeaturefor......
  • Google推短信收发邮件服务Gmail SMS 造福非洲人民
    Google刚刚在非洲推出了一项新的邮件服务GmailSMS,即用户可以通过手机内置的短信功能(SMS)收发Gmail邮件。该服务将首先在加纳、尼日利亚和肯尼亚三国试用。这一举措意味着,......
  • python发邮件
    importsmtplibfromemail.mime.textimportMIMEText#邮件正文fromemail.headerimportHeader#邮件头#登录邮件服务器smtp_obj=smtplib.SMTP_SSL("s......