emailmModel.php 6.5 KB

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