emailModel.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. class emailClassModel extends Model
  3. {
  4. private $errorinfo = '';
  5. public function initModel()
  6. {
  7. $this->settable('email_cog');
  8. }
  9. /**
  10. * 系统邮件发送邮件
  11. * $to_uid 发送给。。。
  12. * $rows 内容
  13. */
  14. public function sendmail($title, $body, $to_uid, $rows=array(), $zjsend=0, $oparm=array())
  15. {
  16. if(!function_exists('socket_get_status') || !function_exists('fsockopen'))return '没有开启socket扩展无法使用';
  17. if(!function_exists('openssl_sign'))return '没有开启openssl扩展无法使用';
  18. $setrs = m('option')->getpidarr(-1);
  19. if(!$setrs)return '未设置发送邮件';
  20. $serversmtp = $this->rock->arrvalue($setrs, 'email_sendhost');
  21. $emailuser = $this->rock->arrvalue($setrs, 'email_sysuser');
  22. $emailname = $this->rock->arrvalue($setrs, 'email_sysname');
  23. $emailpass = $this->rock->arrvalue($setrs, 'email_syspass');
  24. $serverport = $this->rock->arrvalue($setrs, 'email_sendport');
  25. $emailsecure = $this->rock->arrvalue($setrs, 'email_sendsecure');
  26. if(isempt($serversmtp) || isempt($serverport) || isempt($emailuser)|| isempt($emailpass))return '未设置发送帐号';
  27. $to_em = $to_mn = $to_id = '';
  28. if(is_array($to_uid)){
  29. $to_id = arrvalue($to_uid,'receid','0');
  30. $to_em = arrvalue($to_uid,'receemail');
  31. $to_mn = arrvalue($to_uid,'recename');
  32. }else{
  33. $urs = $this->db->getall("select `email`,`name`,`id` from `[Q]admin` where `id` in($to_uid) and `email` is not null and `status`=1 order by `sort`");
  34. foreach($urs as $k=>$rs){
  35. $to_em.=','.$rs['email'];
  36. $to_mn.=','.$rs['name'];
  37. $to_id.=','.$rs['id'];
  38. }
  39. if(isempt($to_em))return '用户('.$to_uid.')没有设置邮箱';
  40. $to_em = substr($to_em, 1);
  41. $to_mn = substr($to_mn, 1);
  42. $to_id = substr($to_id, 1);
  43. }
  44. if(isempt($to_em))return '没有接收人1';
  45. $body = $this->rock->reparr($body, $rows);
  46. $title = $this->rock->reparr($title, $rows);
  47. $body = str_replace("\n", '<br>', $body);
  48. $msg = 'ok';
  49. if(!getconfig('asynsend') || $zjsend==1){
  50. $sarrs = array(
  51. 'emailpass' => $emailpass,
  52. 'serversmtp' => $serversmtp,
  53. 'serverport' => $serverport,
  54. 'emailsecure' => $emailsecure,
  55. 'emailuser' => $emailuser,
  56. 'emailname' => $emailname,
  57. 'receemail' => $to_em,
  58. 'recename' => $to_mn,
  59. 'title' => $title,
  60. 'body' => $body,
  61. );
  62. foreach($oparm as $k1=>$v1)$sarrs[$k1] = $v1;
  63. $bo = $this->sendddddd($sarrs, true);
  64. if(!$bo)$msg = $this->errorinfo;
  65. }else{
  66. //异步发送邮件
  67. $uarr['title'] = $title;
  68. $uarr['body'] = $body;
  69. $uarr['receid'] = $to_id;
  70. $uarr['recename'] = $to_mn;
  71. $uarr['receemail'] = $to_em;
  72. $uarr['optdt'] = $this->rock->now();
  73. $uarr['optid'] = $this->adminid;
  74. $uarr['optname'] = $this->adminname;
  75. $uarr['status'] = 0;
  76. foreach($oparm as $k1=>$v1)$uarr[$k1] = $v1;
  77. $sid = m('email_cont')->insert($uarr);
  78. m('reim')->asynurl('asynrun','sendemail', array(
  79. 'id' => $sid,
  80. 'stype' => 0
  81. ));//系统邮件提醒用的
  82. }
  83. return $msg;
  84. }
  85. /**
  86. *
  87. */
  88. public function sendtoemail($params=array())
  89. {
  90. $setrs = m('option')->getpidarr(-1);
  91. if(!$setrs)return '未设置发送邮件';
  92. $serversmtp = $this->rock->arrvalue($setrs, 'email_sendhost');
  93. $emailuser = $this->rock->arrvalue($setrs, 'email_sysuser');
  94. $emailname = $this->rock->arrvalue($setrs, 'email_sysname');
  95. $emailpass = $this->rock->arrvalue($setrs, 'email_syspass');
  96. $serverport = $this->rock->arrvalue($setrs, 'email_sendport');
  97. $emailsecure = $this->rock->arrvalue($setrs, 'email_sendsecure');
  98. $barr = array(
  99. 'emailpass' => $emailpass,
  100. 'serversmtp' => $serversmtp,
  101. 'serverport' => $serverport,
  102. 'emailsecure' => $emailsecure,
  103. 'emailuser' => $emailuser,
  104. 'emailname' => $emailname,
  105. 'receemail' => '',
  106. 'recename' => '',
  107. 'title' => '',
  108. 'body' => '',
  109. );
  110. foreach($params as $k=>$v)$barr[$k]=$v;
  111. return $this->sendddddd($barr, true);
  112. }
  113. //$jbs 密码是否加密 保存日志$log
  114. private function sendddddd($arr, $jbs, $log=false)
  115. {
  116. extract($arr);
  117. $pass = $emailpass;
  118. if($jbs)$pass = $this->rock->jm->uncrypt($pass);
  119. $mail = c('mailer');
  120. $mail->setHost($serversmtp, $serverport, $this->rock->repempt($emailsecure));
  121. $mail->setUser($emailuser, $pass);
  122. $mail->setFrom($emailuser, $emailname);
  123. $mail->addAddress($receemail, $recename);
  124. if(isset($ccemail) && !isempt($ccemail)){
  125. $mail->addCC($ccemail, $ccname);
  126. }
  127. if(isset($attachpath) && !isempt($attachpath)){
  128. $mail->addAttachment($attachpath, $attachname);
  129. }
  130. $mail->sendMail($title, $body);
  131. $bo = $mail->isSuccess();
  132. if(!$bo){
  133. $this->errorinfo = 'error:'.$mail->getErrror().';to:'.$receemail.'';
  134. }
  135. return $bo;
  136. }
  137. /**
  138. * 测试发送邮件
  139. */
  140. public function sendmail_test()
  141. {
  142. return $this->sendmail('测试邮件帐号','这只是一个测试邮件帐号,不要紧张!<br>来自:'.TITLE.'<br>发送人:'.$this->adminname.'<br>网址:'.URL.'<br>发送时间:'.$this->rock->now().'', $this->adminid, array(),1);
  143. }
  144. /**
  145. * 异步发送邮件
  146. */
  147. public function sendemailcont($id, $stype=-1)
  148. {
  149. $rs = m('email_cont')->getone($id);
  150. if(!$rs)return '记录不存在';
  151. if($stype==-1)$stype = (int)$this->rock->get('stype');
  152. if($stype == 0){
  153. $msg = $this->sendmail($rs['title'],$rs['body'], array(
  154. 'receid' => $rs['receid'],
  155. 'receemail' => $rs['receemail'],
  156. 'recename' => $rs['recename'],
  157. ), array(), 1, array(
  158. 'ccname' => $rs['ccname'],
  159. 'ccemail' => $rs['ccemail'],
  160. 'attachpath'=> $rs['attachpath'],
  161. 'attachname'=> $rs['attachname'],
  162. ));
  163. }else{
  164. $msg = $this->sendemailout($rs['optid'],array(
  165. 'title' => $rs['title'],
  166. 'body' => $rs['body'],
  167. 'receemail' => $rs['receemail'],
  168. 'recename' => $rs['recename'],
  169. 'ccname' => $rs['ccname'],
  170. 'ccemail' => $rs['ccemail'],
  171. 'attachpath'=> $rs['attachpath'],
  172. 'attachname'=> $rs['attachname'],
  173. 'mid' => $rs['mid'],
  174. ), 1);
  175. }
  176. $status = '2';
  177. if($msg=='ok')$status = '1';
  178. $uarr['status'] = $status;
  179. $uarr['senddt'] = $this->rock->now();
  180. m('email_cont')->update($uarr, $id);
  181. return $msg;
  182. }
  183. /**
  184. * 用户自己外发发送
  185. */
  186. public function sendemailout($sendid, $canarr = array(), $zjsend=0)
  187. {
  188. $sendarr = array(
  189. 'title' => '',
  190. 'body' => '',
  191. 'receemail' => '',
  192. 'recename' => '',
  193. 'ccname' => '',
  194. 'ccemail' => '',
  195. 'attachpath' => '',
  196. 'attachname' => '',
  197. );
  198. foreach($canarr as $k=>$v)$sendarr[$k] = $v;
  199. extract($sendarr);
  200. $setrs = m('option')->getpidarr(-1);
  201. if(!$setrs)return '未设置发送邮件';
  202. $serversmtp = $this->rock->arrvalue($setrs, 'email_sendhost');
  203. $serverport = $this->rock->arrvalue($setrs, 'email_sendport');
  204. $emailsecure = $this->rock->arrvalue($setrs, 'email_sendsecure');
  205. $myuser = m('admin')->getone($sendid,'name,email,emailpass');
  206. if(!$myuser)return '发送人不存在';
  207. $emailuser = $this->rock->arrvalue($myuser, 'email');
  208. $emailname = $this->rock->arrvalue($myuser, 'name');
  209. $emailpass = $this->rock->arrvalue($myuser, 'emailpass');
  210. if(isempt($serversmtp) || isempt($serverport) || isempt($emailuser)|| isempt($emailpass))return '用户未设置邮件帐号密码';
  211. $msg = 'ok';
  212. $outzt = 2;
  213. if(!getconfig('asynsend') || $zjsend==1){
  214. $bo = $this->sendddddd(array(
  215. 'emailpass' => $emailpass,
  216. 'serversmtp' => $serversmtp,
  217. 'serverport' => $serverport,
  218. 'emailsecure' => $emailsecure,
  219. 'emailuser' => $emailuser,
  220. 'emailname' => $emailname,
  221. 'receemail' => $receemail,
  222. 'recename' => $recename,
  223. 'ccname' => $ccname,
  224. 'ccemail' => $ccemail,
  225. 'attachpath' => $attachpath,
  226. 'attachname' => $attachname,
  227. 'title' => $title,
  228. 'body' => $body,
  229. ), false);
  230. if(!$bo)$msg = $this->errorinfo;
  231. if(isset($mid)){
  232. if($msg=='ok')$outzt=1;
  233. m('emailm')->update('`outzt`='.$outzt.'', $mid);
  234. }
  235. }else{
  236. //异步发送邮件
  237. $uarr['title'] = $title;
  238. $uarr['body'] = $body;
  239. $uarr['receid'] = '';
  240. $uarr['recename'] = $recename;
  241. $uarr['receemail'] = $receemail;
  242. $uarr['ccname'] = $ccname;
  243. $uarr['ccemail'] = $ccemail;
  244. $uarr['attachpath'] = $attachpath;
  245. $uarr['attachname'] = $attachname;
  246. $uarr['optdt'] = $this->rock->now();
  247. $uarr['optid'] = $this->adminid;
  248. $uarr['optname'] = $this->adminname;
  249. $uarr['status'] = 0;
  250. if(isset($mid))$uarr['mid'] = $mid;
  251. $sid = m('email_cont')->insert($uarr);
  252. c('rockqueue')->push('email,anaysend', array(
  253. 'id' => $sid,
  254. 'stype' => 1
  255. ));
  256. /*
  257. m('reim')->asynurl('asynrun','sendemail', array(
  258. 'id' => $sid,
  259. 'stype' => 1
  260. ));*/
  261. }
  262. return $msg;
  263. }
  264. }
粤ICP备19079148号