inbox_obj = Db::name('inbox'); $this->inbox_attachment_obj = Db::name('inbox_attachment'); $this->journal_obj = Db::name('journal'); } /** * 获取收件箱邮件 * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getInbox(){ $server = new receiveMails(); $journals=[ [ 'journal_id'=>2, 'email'=>'1586428462@qq.com', 'epassword'=>'rthlprelzzfphigh' ] ]; foreach ($journals as $journal){ //1. 连接邮箱 传入参数 $server->connect($journal); //2. 获取邮件数 $emailNums = $server->getTotalMails(); if($emailNums){ $insertData=[]; foreach ($emailNums as $key=>$value){ //2.1 获取邮件头部信息 $head = $server->getHeaders($value); //2.2 处理邮件附件 $files = $server->getAttach($value); // 附件 $head['url'] =[]; //2.3 处理正文中的图片 $imageList=array(); $section = 2; if($files){ foreach($files as $k => $file) { //type=1为附件,0为邮件内容图片 if($file['type'] == 0) { $imageList[$k]=$file['pathname']; } if($file['type'] == 1) { array_push($head['url'],$file['url']); } if(($file['type'] == 0 || $file['type'] == 1) && !empty($file['url'])){ $section = 1.2 ; } } } $webPath = ROOT_PATH.'public' . DS . 'contentImg' . DS . date('Ymd') .DS ; // 正文内容 $head['content'] = $server->getBody($value,$webPath,$imageList,$section); array_push($insertData,$head); } $this->insertData($insertData,$journal); } } } // 后台展示 收件箱 public function getInboxList(){ $data = $this->request->post(); // 验证规则 $rule = new Validate([ 'pageIndex'=>'require|number', 'pageSize'=>'require|number' ]); if(!$rule->check($data)){ return json(['code' => 1,'msg'=>$rule->getError()]); } $res['data'] = $this->inbox_obj->field('t_inbox.*,t_inbox_attachment.attachmentUrl') ->join('t_inbox_attachment','t_inbox_attachment.emailId = t_inbox.id','LEFT') ->page($data['pageIndex'],$data['pageSize']) ->select(); $res['data']= $this->inbox_obj->field('id,sendEmail,title,content,creatTime,isaAtachment')->page($data['pageIndex'],$data['pageSize'])->select(); foreach ($res['data'] as $key=>$value ){ $res['data'][$key]['attachmentUrl'] = $this->inbox_attachment_obj->where('emailId',$value['id'])->column('attachmentUrl'); } $res['total'] = $this->inbox_obj->count(); return jsonSuccess($res); } /** * 已经存在邮件去除 * @param $datas * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ private function removeData($datas,$journal){ foreach ($datas as $key => $value){ $res = $this->inbox_obj->where(['journalId'=>$journal['journal_id'],'eid'=>$value['emailId']])->find(); if($res){ unset($datas[$key]); } } return $datas; } /** * 写入数据库 * @param $datas */ private function insertData($datas,$journal){ $datas = $this->removeData($datas,$journal); Db::startTrans(); try{ foreach ($datas as $key=>$value){ $insert = [ 'journalId'=>$journal['journal_id'], 'eid'=>$value['emailId'], 'sendEmail'=>$value['from'], 'title'=>$value['subject'], 'content'=>$value['content'], 'sendTime'=>$value['sendTime'], 'creatTime'=>time(), 'isaAtachment'=>empty($value['url'])?0:1, 'isReply'=>$value['isReply'] ]; $id = $this->inbox_obj->insertGetId($insert); if(!empty($value['url']) && is_array($value['url'])){ $urls=[]; foreach ($value['url'] as $k => $url){ $urls[$k]=[ 'emailId'=>$id, 'attachmentUrl'=>$url ]; } $this->inbox_attachment_obj->insertAll($urls); } } //提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); } } }