EmailSends.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Kevin
  5. * Date: 2018/11/14
  6. * Time: 15:51
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\model\CustomerModel;
  10. use app\admin\model\EmailSendModel;
  11. use app\admin\model\EmailsModel;
  12. use app\admin\model\EmailTemplateModel;
  13. use app\admin\model\UserModel;
  14. use PHPMailer\PHPMailer\PHPMailer;
  15. use think\Db;
  16. class EmailSends extends Base
  17. {
  18. public function index(){
  19. if (request()->isAjax()) {
  20. extract(input());
  21. $map = [];
  22. if (isset($email) && $email != "") {
  23. $map['email'] = ['like', "%" . $email . "%"];
  24. }
  25. $map['is_deleted'] = 0;
  26. //$map['spread_id']=session('uid');
  27. $Nowpage = input('page') ? input('page') : 1;
  28. $limits = input("limit") ? input("limit") : 10;//获取总条数;
  29. $field = input('field');//字段
  30. $order = input('order');//排序方式
  31. if ($field && $order) {
  32. $od = $field . " " . $order;
  33. } else {
  34. $od = "create_time desc";
  35. }
  36. $emails =new EmailTemplateModel();
  37. $emailSend = new EmailSendModel();
  38. $count = $emails->getCount($map);
  39. $lists = $emailSend->getByWhere($map, $od, $Nowpage, $limits);
  40. foreach($lists as $k=>&$v){
  41. $v['shou']='<a href="export?uni='.$v['uni'].'" style="color:#3BA1FF">下载全部收件人邮箱</a>';
  42. $v['count']=Db::table('think_email_send')->where('uni',$v['uni'])->count();//全部
  43. $v['count1']=Db::table('think_email_send')->where('uni',$v['uni'])->where('status',1)->count();//已发送
  44. $v['count2']=Db::table('think_email_send')->where('uni',$v['uni'])->where('status',0)->count();//未发送
  45. $v['count3']=Db::table('think_email_send')->where('uni',$v['uni'])->where('status',2)->count();//已取消
  46. // if($v['type']==2){
  47. // $v['remark']='草稿';
  48. // $v['is_edit']=1;
  49. // }else
  50. if($v['count']>0 && $v['count2']>0 && $v['count3']==0){
  51. $v['remark']='发送中('.$v['count1'].'/'.$v['count'].')';
  52. $v['is_edit']=0;
  53. $v['is_stop']=1;
  54. }elseif ($v['count2']==0 && $v['count3']>0){
  55. $v['remark']='已取消('.$v['count1'].'/'.$v['count'].')';
  56. $v['is_edit']=1;
  57. $v['is_go']=1;
  58. }elseif(!$v['count']){
  59. $v['remark']='未发送';
  60. $v['is_edit']=1;
  61. }elseif($v['count2']==0 && $v['count3']==0){
  62. $v['remark']='已完成';
  63. $v['is_edit']=1;
  64. }else{
  65. $v['remark']='处理中';
  66. $v['is_edit']=1;
  67. }
  68. $v['send_time']=Db::table('think_email_send')->where('uni',$v['uni'])->where('status', 1)->order('send_time desc')->value('send_time');
  69. if($v['send_time']){
  70. $v['send_time']=date('Y-m-d H:i:s',$v['send_time']).'-('.$v['count1'].'/'.$v['count'].')';
  71. }else{
  72. $n['send_time']=Db::table('think_email_send')->where('uni',$v['uni'])->order('send_time desc')->value('send_time');
  73. $v['send_time']=date('Y-m-d H:i:s',$n['send_time']).'-('.$v['count1'].'/'.$v['count'].')';
  74. }
  75. $v['username']=Db::table('think_admin')->where('id',$v['spread_id'])->value('username');
  76. }
  77. return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
  78. }
  79. $this->assign('status', 100);
  80. return $this->fetch('index');
  81. }
  82. public function add(){
  83. $emails =new EmailsModel();
  84. if(request()->isPost()){
  85. $param = input('param.');
  86. if($param['send_type']==1){
  87. if(empty($param['send_time'])){
  88. return json(['code' => 100, 'msg' => '请先选择发送时间']);
  89. }
  90. }
  91. if(empty($param['email_id'])){
  92. return json(['code' => 100, 'msg' => '请先选择发件人']);
  93. }
  94. if(empty($param['to'])){
  95. return json(['code' => 100, 'msg' => '请先输入收件人邮箱']);
  96. }
  97. if(empty($param['theme'])){
  98. return json(['code' => 100, 'msg' => '请先选择邮件主题']);
  99. }
  100. if(empty($param['content'])){
  101. return json(['code' => 100, 'msg' => '请先填写邮件内容']);
  102. }
  103. $to_email=Db::table('think_email')->where('id',$param['email_id'])->find();
  104. $count=Db::table('think_email_send')->where('is_deleted',0)->where('email_id',$to_email['id'])->count();
  105. if($count+1>$to_email['max_num']){
  106. return json(['code' => 100, 'msg' => '请切换邮箱发送,该邮箱已超过最大发送次数']);
  107. }
  108. $theme=Db::table('think_email_template')->where('id',$param['theme'])->value('theme');
  109. $data['from']=$to_email['email'];
  110. $data['from_name']='发件人姓名';
  111. $data['to']=$param['to'];
  112. $data['theme']=$theme;
  113. $data['theme_id']=$param['theme'];
  114. $data['content']=$param['content'];
  115. $data['email_id']=$param['email_id'];
  116. $data['send_type']=$param['send_type'];
  117. $data['spread_id']=session('uid');
  118. $data['create_time']=time();
  119. if($data['send_type']==0){
  120. $mail= new PHPMailer();
  121. $mail->SMTPDebug = 1;
  122. $mail->isSMTP();
  123. $mail->SMTPDebug = false;
  124. $mail->SMTPAuth = true;
  125. $mail->Host =$to_email['smtp'];
  126. $mail->SMTPSecure = $to_email['encryption'];
  127. $mail->Port = $to_email['port'];
  128. $mail->CharSet = 'UTF-8';
  129. $mail->FromName = '发件人昵称';
  130. $mail->Username = $to_email['email'];
  131. $mail->Password = $to_email['password'];
  132. $mail->From = $to_email['email'];
  133. $mail->isHTML(true);
  134. $mail->addAddress($param['to']);
  135. $mail->Subject = $theme;
  136. $mail->Body = $param['content'];
  137. $status = $mail->send();
  138. $data['status']=1;
  139. $data['send_status']=!$status?0:1;
  140. $data['send_time']=time();
  141. }else{
  142. $data['send_time']=strtotime($param['send_time']);
  143. }
  144. $r=Db::table('think_email_send')->insertGetId($data);
  145. if($r){
  146. return json(['code' => 200, 'msg' => '提交成功']);
  147. }else{
  148. return json(['code' => 100, 'msg' => '提交失败']);
  149. }
  150. }
  151. $emails=Db::table('think_email')
  152. ->where('is_deleted',0)
  153. ->where('spread_id',session('uid'))
  154. ->select();
  155. $this->assign('emails',$emails);
  156. $templates=Db::table('think_email_template')
  157. ->where('is_deleted',0)
  158. ->where('type',1)
  159. ->where('spread_id',session('uid'))
  160. ->select();
  161. $this->assign('templates',$templates);
  162. return $this->fetch();
  163. }
  164. public function edit(){
  165. $emails =new EmailsModel();
  166. if(request()->isPost()){
  167. $param = input('param.');
  168. $flag = $emails->edit($param);
  169. return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
  170. }
  171. $id = input('id');
  172. $map = [];
  173. $this->assign('res',$emails->getInfo($id));
  174. return $this->fetch();
  175. }
  176. public function del(){
  177. $id = input('param.id');
  178. $emails =new EmailsModel();
  179. $flag = $emails->del($id);
  180. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
  181. }
  182. /**
  183. * batchDelUser 批量删除
  184. * @return \think\response\Json
  185. */
  186. public function batchDelUser()
  187. {
  188. extract(input());
  189. if (empty($ids)) {
  190. return json(['code' => 100, 'msg' => '请选择要删除的记录!']);
  191. }
  192. $ids = explode(',', $ids);
  193. $emails =new EmailsModel();
  194. $flag = $emails->batchDel($ids);
  195. return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
  196. }
  197. public function email_template_info(){
  198. $id = input('param.id');
  199. if(empty($id)){
  200. return json(['code' => 100, 'msg' => '参数错误!']);
  201. }
  202. $model=new EmailTemplateModel();
  203. $res=$model->getInfo($id);
  204. return json(['code' => 200, 'data' => $res, 'msg' => '请求成功']);
  205. }
  206. public function edit_status(){
  207. $id = input('param.id');
  208. $status = input('param.status');
  209. if(empty($id) || empty($status)){
  210. return json(['code' => 100, 'msg' => '参数错误']);
  211. }
  212. $map['spread_id']=session('uid');
  213. $map['is_deleted']=0;
  214. $map['theme_id']=$id;
  215. if($status==1){
  216. $map['status']=2;
  217. $status=0;
  218. }else{
  219. $map['status']=0;
  220. $status=2;
  221. }
  222. $ids=Db::table('think_email_send')->where($map)->column('id');
  223. if(empty($ids)){
  224. return json(['code' => 200, 'msg' => '操作成功']);
  225. }
  226. Db::table('think_email_send')->whereIn('id',$ids)->update(['status'=>$status]);
  227. return json(['code' => 200, 'msg' => '操作成功']);
  228. }
  229. public function export(){
  230. $id = input('param.id');
  231. //$map['spread_id']=session('uid');
  232. $map['is_deleted']=0;
  233. $map['theme_id']=$id;
  234. $data=Db::table('think_email_send')->where($map)->field('to')->select();
  235. $cellname = [
  236. ['to', '邮箱', 15, 'LEFT'],
  237. ];
  238. $res = exportExcels('收件邮箱', 'email', $cellname, $data);
  239. return json($res);
  240. }
  241. }