Receiveemail.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\library\ReceiveMail;
  4. use think\Request;
  5. /**
  6. * 邮箱管理
  7. */
  8. class Receiveemail extends StaffApi {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = [];
  11. protected $formater = ['gif','jpeg','png','jpg','bmp'];
  12. public $mailAccount;
  13. public $mailPasswd;
  14. public $mailAddress;
  15. public $mailServer = "pophz.qiye.163.com";
  16. public $serverType = "pop3";
  17. public $port = "110";
  18. public $now = 0;
  19. public $savePath;
  20. public $webPath = "../upload/";
  21. public function __construct(Request $request = null)
  22. {
  23. parent::__construct($request);
  24. $this->mailAccount=$this->auth->email;
  25. $this->mailPasswd=$this->auth->email_code;
  26. $this->mailAddress=$this->auth->email;
  27. $this->savePath = ROOT_PATH;
  28. }
  29. // 收件箱列表
  30. public function getEmails()
  31. {
  32. $limit = input("limit/d", 10);
  33. $isRead = input("is_read",'');
  34. $theme = input("name",'');
  35. if(isset($isRead) && is_numeric($isRead) && in_array($isRead,[0,1])){
  36. $where['is_read'] = $isRead;
  37. }
  38. if($theme){
  39. $where['theme'] = ['like','%'.$this.'%'];
  40. }
  41. $where['staff_id'] = $this->auth->id;
  42. $email = new \addons\qingdongams\model\ReceiveEmail();
  43. $list = $email->where($where)->field('createtime,updatetime',true)->field('content',true)->paginate($limit);
  44. $this->success('',$list);
  45. }
  46. // 获取详情
  47. public function detailEmail(){
  48. $id = input('id');
  49. $email = new \addons\qingdongams\model\ReceiveEmail();
  50. $email->isUpdate(true)->save(['is_read'=>1],['id'=>$id]);
  51. $detail = $email->with('staff')->where(['id'=>$id,'staff_id'=>$this->auth->id])->find();
  52. $this->success('',$detail);
  53. }
  54. // 删除邮件
  55. public function delEmail(){
  56. $id = input('id');
  57. $email = new \addons\qingdongams\model\ReceiveEmail();
  58. $res = $email->where('id',$id)->find();
  59. if(!$res){
  60. $this->error('邮件不存在');
  61. }
  62. if($email->destroy($id) !== false){
  63. $this->success('删除成功');
  64. }
  65. $this->error('稍后再试');
  66. }
  67. // 下载附件
  68. public function downloadFile(){
  69. $id = input('id');
  70. $email = new \addons\qingdongams\model\ReceiveEmail();
  71. $fileIds = $email->where('id',$id)->value('attach');
  72. if(!$fileIds){
  73. $this->error('无附件可下载');
  74. }
  75. $filePaths = \addons\qingdongams\model\File::where('id','in',explode(',',$fileIds))->column('file_path');
  76. foreach ($filePaths as $v){
  77. $file = ROOT_PATH.'public'.$v;
  78. download($file);
  79. }
  80. }
  81. /**
  82. * 无用,前期测试用到
  83. * mail Received()读取收件箱邮件
  84. *
  85. * @param
  86. * @access public
  87. * @return result
  88. */
  89. public function mailReceived()
  90. {
  91. // Creating a object of reciveMail Class
  92. $obj= new ReceiveMail($this->mailAccount,$this->mailPasswd,$this->mailAddress,$this->mailServer,$this->serverType,$this->port,false);
  93. //Connect to the Mail Box
  94. $res=$obj->connect(); //If connection fails give error message and exit
  95. if (!$res)
  96. {
  97. return array("msg"=>"Error: Connecting to mail server");
  98. }
  99. // Get Total Number of Unread Email in mail box
  100. $tot=$obj->getTotalMails(); //Total Mails in Inbox Return integer value
  101. if($tot < 1) { //如果信件数为0,显示信息
  102. return array("msg"=>"No Message for ".$this->mailAccount);
  103. }
  104. else
  105. {
  106. $res=array("msg"=>"Total Mails:: $tot<br>");
  107. for($i=$tot;$i>0;$i--)
  108. {
  109. $head=$obj->getHeaders($i); // Get Header Info Return Array Of Headers **Array Keys are (subject,to,toOth,toNameOth,from,fromName)
  110. //处理邮件附件
  111. $files=$obj->GetAttach($i,$this->savePath); // 获取邮件附件,返回的邮件附件信息数组
  112. $imageList=array();
  113. foreach($files as $k => $file)
  114. {
  115. //type=1为附件,0为邮件内容图片
  116. if($file['type'] == 0)
  117. {
  118. $imageList[$file['title']]=$file['pathname'];
  119. }
  120. }
  121. $body = $obj->getBody($i,$this->webPath,$imageList);
  122. $res['mail'][]=array('head'=>$head,'body'=>$body,"attachList"=>$files);
  123. }
  124. $obj->close_mailbox(); //Close Mail Box
  125. return $res;
  126. }
  127. }
  128. }