emailmModel.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. class emailmClassModel extends Model
  3. {
  4. public function initModel()
  5. {
  6. $this->adminobj = m('admin');
  7. $this->optionobj = m('option');
  8. $this->upfileobj = c('upfile');
  9. $this->recehost = $this->optionobj->getval('email_recehost');
  10. $this->receyumi = $this->optionobj->getval('email_receyumi');
  11. }
  12. /**
  13. * 用户收邮件
  14. * $uid 用户id
  15. * return 返回是数组就说明成功,字符串就失败
  16. */
  17. public function receemail($uid)
  18. {
  19. $ukey = 'email_recexin_'.$uid.'';
  20. $myurs = $this->adminobj->getone($uid, 'email,emailpass');
  21. $time = $this->optionobj->getval($ukey,'',3);
  22. if(!isempt($time))$time = strtotime($time);
  23. if(isempt($this->receyumi))return '未设置收信邮箱域名';
  24. if(isempt($myurs['email']))return '未设置邮箱,可到[系统→邮件管理→用户邮箱设置]下设置';
  25. if(isempt($myurs['emailpass']))return '未设置邮箱密码,可到[系统→邮件管理→用户邮箱设置]下设置';
  26. if(!contain($myurs['email'], $this->receyumi))return '邮箱域名必须是['.$this->receyumi.'],当前用户邮箱:'.$myurs['email'].'';
  27. $rows = c('imap')->receemail($this->recehost, $myurs['email'], $myurs['emailpass'], $time);
  28. if(!is_array($rows))return $rows;
  29. $jf = 0;
  30. $bool = true;
  31. foreach($rows as $k=>$rs){
  32. $message_id = $rs['message_id'];
  33. if(isempt($message_id))$message_id = md5($rs['date']);
  34. $where = "message_id='$message_id'";
  35. $id = (int)$this->getmou('id',$where);
  36. if($id ==0)$where = '';
  37. if($id>0)continue;
  38. $uarr['message_id'] = $message_id;
  39. $uarr['title'] = $this->db->tocovexec($rs['subject'], 1);
  40. $uarr['content'] = $this->db->tocovexec($rs['body'], 1);
  41. $uarr['senddt'] = $rs['date'];
  42. $uarr['optdt'] = $this->rock->now;
  43. $uarr['size'] = $rs['size'];
  44. $uarr['fromemail'] = $this->gshemail($rs['fromemail']);
  45. $uarr['toemail'] = $this->gshemail($rs['toemail']);
  46. $uarr['reply_toemail'] = $this->gshemail($rs['reply_toemail']);
  47. $uarr['ccemail'] = $this->gshemail($rs['ccemail']);
  48. $uarr['isturn'] = 1;
  49. $uarr['isfile'] = count($rs['attach']) > 0 ? 1 : 0;
  50. $uarr['type'] = 1;
  51. $uarr['numoi'] = $rs['num'];
  52. $form = $rs['from'][0];
  53. $urs = $this->adminobj->emailtours($form->email);
  54. $uarr['uid'] = 0;
  55. $uarr['sendid'] = 0;
  56. $uarr['sendname'] = '';
  57. if(is_array($urs) && $urs){
  58. $uarr['sendid'] = $urs['id'];
  59. $uarr['uid'] = $urs['id'];
  60. $uarr['sendname'] = $urs['name'];
  61. }
  62. $uarr['sendname'] = $uarr['fromemail'];
  63. $uarr['recename'] = $uarr['toemail'];
  64. $bool = $this->record($uarr, $where);
  65. if($bool){
  66. if($id ==0)$id = $this->db->insert_id();
  67. $this->saveattach($rs['num'], $rs['attach'], $id);
  68. $toarr = $this->saveemails($id, 0, $rs['to']);
  69. $this->saveemails($id, 1, $rs['cc']);
  70. $this->saveemails($id, 2, $rs['from']);
  71. $uuarr['receid'] = $toarr[0];
  72. //$uuarr['recename'] = $toarr[1];
  73. $this->update($uuarr, $id);
  74. $jf++;
  75. }
  76. }
  77. if($bool)$this->optionobj->setval($ukey.'@-2', '共'.$jf.'封');//记录最后收信时间
  78. return array(
  79. 'count' => $jf
  80. );
  81. }
  82. private function gshemail($str)
  83. {
  84. $str = str_replace(array('"',' ','\''),'', $str);
  85. return $str;
  86. }
  87. //保存到子表
  88. public function saveemails($mid, $type, $arr)
  89. {
  90. $sid = $sna = '';
  91. foreach($arr as $k=>$rs){
  92. $where = "`mid`='$mid' and `type`=$type and `email`='$rs->email'";
  93. $id = (int)$this->db->getmou('[Q]emails','id',$where);
  94. if($id ==0)$where = '';
  95. $uarr = array();
  96. $uarr['mid'] = $mid;
  97. $uarr['email'] = $rs->email;
  98. $uarr['personal'] = $rs->personal;
  99. $uarr['type'] = $type;
  100. if($id==0){
  101. $uarr['optdt'] = $this->rock->now;
  102. }
  103. $urs = $this->adminobj->emailtours($rs->email);
  104. $uarr['uid'] = 0;
  105. if(is_array($urs) && $urs){
  106. $uarr['uid'] = $urs['id'];
  107. $sid .=',u'.$urs['id'].'';
  108. $sna .=','.$urs['name'].'';
  109. }
  110. $this->db->record('[Q]emails', $uarr, $where);
  111. }
  112. if($sid != ''){
  113. $sid = substr($sid, 1);
  114. $sna = substr($sna, 1);
  115. }
  116. return array($sid, $sna);
  117. }
  118. //保存邮件的附件
  119. private function saveattach($oi, $arr, $id)
  120. {
  121. $dbs = m('file');
  122. $doobj = c('down');
  123. foreach($arr as $k=>$rs){
  124. $fsad = explode('.', $rs['filename']);
  125. $sarr['filename'] = $rs['filename'];
  126. $sarr['filesize'] = $rs['filesize'];
  127. $sarr['fileext'] = strtolower($fsad[count($fsad)-1]);
  128. $sarr['keyoi'] = $oi.','.$rs['encoding'].','.$rs['filekey'];
  129. $sarr['mtype'] = 'emailm';
  130. $sarr['optid'] = $this->adminid;
  131. $sarr['optname'] = $this->adminname;
  132. $sarr['adddt'] = $this->rock->now;
  133. $sarr['mid'] = $id;
  134. $sarr['filesizecn'] = $this->upfileobj->formatsize($rs['filesize']);
  135. $where = "`mtype`='emailm' and `mid`='$id' and `filename`='".$sarr['filename']."' and `filesize`='".$sarr['filesize']."'";
  136. if($dbs->rows($where)==0){
  137. $sarr['filepath'] = $doobj->savefilecont($sarr['fileext'], $rs['attachcont']);//下载附件
  138. $fid = $dbs->insert($sarr);
  139. }
  140. }
  141. }
  142. /**
  143. * 统计我未读邮件
  144. */
  145. public function wdtotal($uid)
  146. {
  147. $to= $this->gettotsllss($uid, 1);
  148. return (int)$to;
  149. }
  150. //
  151. private function gettotsllss($uid, $lx=0, $glx='')
  152. {
  153. $whe= 'b.uid='.$uid.' and a.`isturn`=1 and b.`isdel`=0 and b.`type` in(0,1)';
  154. if($lx==1)$whe='b.uid='.$uid.' and a.`isturn`=1 and b.`isdel`=0 and b.`type` in(0,1) and b.`zt`=0';
  155. if($lx==2)$whe='a.`sendid`='.$uid.' and a.`isturn`=0';
  156. if($lx==3)$whe='b.uid='.$uid.' and b.`type` = 2 and b.`isdel`=0';
  157. if($lx==4)$whe='b.uid='.$uid.' and a.`isturn`=1 and b.`type` in(0,1) and b.`isdel`=1';
  158. if($glx=='whe')return $whe;
  159. $to = $this->db->rows('`[Q]emailm` a left join `[Q]emails` b on a.`id`=b.`mid`', $whe);
  160. return (int)$to;
  161. }
  162. public function gettowhere($uid, $lx)
  163. {
  164. return $this->gettotsllss($uid, $lx,'whe');
  165. }
  166. public function zongtotal($uid)
  167. {
  168. $zz = $this->gettotsllss($uid, 0); //所有邮件
  169. $wd = $this->gettotsllss($uid, 1); //未读邮件
  170. $cgx= $this->gettotsllss($uid, 2); //草稿箱
  171. $yfs= $this->gettotsllss($uid, 3); //已发送
  172. $ysc= $this->gettotsllss($uid, 4); //已删除
  173. return array(
  174. 'wd' => $wd,
  175. 'zz' => $zz,
  176. 'cgx' => $cgx,
  177. 'yfs' => $yfs,
  178. 'ysc' => $ysc
  179. );
  180. }
  181. /**
  182. * 标识已读了
  183. */
  184. public function biaoyd($uid,$sid)
  185. {
  186. m('emails')->update('`zt`=1','`uid`='.$uid.' and `mid` in('.$sid.')');
  187. }
  188. }
粤ICP备19079148号