Home.php
15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
namespace Controller;
use Lib\Mail\Mail;
use Lib\Mail\MailFun;
use Lib\Verify;
use Model\bodySql;
use Model\emailSql;
use Model\folderSql;
use Model\listsSql;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
/**
* @author:dc
* @time 2023/2/13 11:28
* Class Home
* @package Controller
*/
class Home extends Base {
/**
* 邮件列表
* @author:dc
* @time 2023/2/17 14:12
*/
public function lists(){
// 分页 页数
$page = app()->request('page',1,'intval');
$page = $page ? $page : 1;
$limit = app()->request('limit',20,'intval');
$limit = $limit ? $limit : 1;
// 指定id
$ids = app()->request('mail_id');
$ids = is_array($ids) ? $ids : [$ids];
foreach ($ids as $i=>$d){
if(!is_numeric($d)){
unset($ids[$i]);
}
}
// 附件
$attachment = app()->request('attachment',0,'bool_Val');
// 已读/未读
$seen = app()->request('seen',-1,'intval');
// 软删
$deleted = app()->request('deleted',0,'intval');
$where = ['email_id'=>$this->getEmails('id')];
// 目录
$folder = app()->request('folder','收件箱');
$folderList = db()->all(folderSql::all($where['email_id']));
$folder_id = [];
// 文件夹id
if($folderList){
foreach ($folderList as $item){
if($item['folder'] == $folder){
$folder_id[] = $item['id'];
}
}
}
if(!$folder_id){
app()->e('folder_not_fount');
}
//目录
$where['folder_id'] = $folder_id;
if($ids) $where['id'] = $ids;
if($attachment) $where['is_file'] = 1; //附件
// 软删
$where['deleted'] = $deleted;
// 已读/未读
if(in_array($seen,[0,1])){
$where['seen'] = $seen;
}
$lists = db()->all(
listsSql::lists(
dbWhere($where),
$page,
$limit
)
);
// 总数
$total = db()->count(
listsSql::listCount(dbWhere($where))
);
app()->_json(listsPage($lists,$total,$page,$limit));
}
/**
* 检测邮箱状态
* @author:dc
* @time 2023/3/28 16:19
*/
public function check(){
$lists = db()->all(emailSql::getValues(['email'=>web_request_emails()],'`id`,`pwd_error`,`email`'));
return array_column($lists,'pwd_error','email');
}
/**
* 发送邮件
* @author:dc
* @time 2023/2/18 17:32
*/
public function send_mail(){
$email = $this->getEmail();
$yzemail = function(&$value,$field){
if($value){
if(!is_array($value)){
if(@json_decode($value,true)){
$value = json_decode($value,true);
}else{
$value = [['email'=>$value,'name'=>'']];
}
}
foreach ($value as $item){
if(!Verify::sEmail($item['email'])){
app()->e([$field.'_verify_error',$item['email']]);
}
}
}
};
$formData = Verify::checks([
'nickname|'.__('nickname') => ['required','max'=>50],
'subject|'.__('subject') => ['required','max'=>150],
'body|'.__('body_email') => ['required'],
'tos|'.__('to_email') => ['required',$yzemail],
'cc|'.__('to_cc') => [$yzemail],
'bcc|'.__('to_bcc') => [$yzemail],
'priority|'.__('priority_email') => ['in'=>[1,3,5]],
'attachment|'.__('files_email') => [
'file'=>[
'ext' => [],
'size' => 1024*1024*5,
'mine' => []
]
],
'receipt|'.__('receipt_email') => []
],[
]);
// 邮件对象
$mail = new PHPMailer();
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = $email['smtp']; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = $email['email']; //SMTP username
$mail->Password = base64_decode($email['password']); //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->CharSet = 'utf-8';
$mail->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE;
//Recipients,设置发件人
$mail->setFrom($email['email'], $formData['nickname']??'');// 显示邮件来自谁
// //设置收件人
foreach ($formData['tos'] as $to){
$mail->addAddress($to['email'], $to['name']??'');
}
// //回复到那个邮件
// $mail->addReplyTo($reply_to['email'], $reply_to['name']); //Add a recipient
// // 抄送
if(!empty($formData['cc'])){
foreach ($formData['cc'] as $to){
$mail->addCC($to['email'], $to['name']??'');
}
}
// 密送
if(!empty($formData['bcc'])){
foreach ($formData['bcc'] as $to){
$mail->addBCC($to['email'], $to['name']??'');
}
}
//Attachments 附件
$attachment = app()->file('attachment');
if($attachment){
foreach ($attachment as $file){
if($file->move()){
// 添加到邮箱中
$mail->addAttachment($file->savePath.$file->saveName, $file->name); //Add attachments
}else{
app()->e(['attachment_upload_error',$file->name]);
}
}
}
// 回执,阅读后收回执的邮箱
if(!empty($formData['receipt'])){
$mail->ConfirmReadingTo = true;
}
// 是否紧急邮件
// Options: null (default), 1 = High, 3 = Normal, 5 = low.
$mail->Priority = $formData['priority']??3;
//Content 主题,标题
$mail->Subject = $formData['subject'];
$mail->isHTML(true); //Set email format to HTML
$mail->Body = $formData['body'];// html格式的内容
// 发送
if($mail->send()){
app()->_json(['messageId' => $mail->getLastMessageID()]);
}
app()->e($mail->ErrorInfo);
}
/**
* 收到前端的同步请求操作
* @author:dc
* @time 2023/3/10 10:38
*/
public function sync(){
$emails = web_request_emails();
if(empty($emails)){
app()->e('sync_request_param_error');
}else{
// 查询id
if(count($emails)===1){
$emails = $emails[0];
}
$datas = db()->all(emailSql::getValues(['email'=>$emails],'`id`,`email`,`pwd_error`'));
foreach ($datas as $k=>$v){
if(!$v['pwd_error']){
redis()->rPush('sync_email_lists', $v['id']);
}
$datas[$k]['have_new'] = redis()->getDel('have_new_mail_'.$v['id']);
}
// 返回成功的参数值
app()->_json($datas);
}
}
/**
* 标记为已读
* @throws \Lib\Err
* @author:dc
* @time 2023/3/17 16:15
*/
public function seen_2_unseen(){
$this->setFlags('seen');
}
/**
* 邮件移动
* @author:dc
* @time 2023/3/21 11:41
*/
public function move(){
$emails = $this->getEmails();
$mail_ids = app()->request('mail_ids');
foreach ($mail_ids as $k=>$id){
if(!is_numeric($id)){
unset($mail_ids[$k]);
}
}
// 移动到的文件夹
$to_folder = app()->request('folder');
if(empty($to_folder)){
app()->e('folder_move_error');
}
$data = db()->all(listsSql::first(dbWhere(['id'=>$mail_ids,'email_id'=>array_column($emails,'id')]),'`id`,`uid`,`email_id`,`folder_id`'));
if($data){
// 查询邮箱
$emails = array_column($emails,null,'id');
$uids = [];
foreach ($data as $datum){
if(empty($uids[$datum['email_id']])){
$uids[$datum['email_id']][$datum['folder_id']] = [];
}
$uids[$datum['email_id']][$datum['folder_id']][] = [
'uid' => $datum['uid'],
'id' => $datum['id'],
];
}
foreach ($uids as $eid=>$arr){
// 查询需要移动的文件夹
$to_origin_folder = db()->first(folderSql::first(['email_id'=>$eid,'folder'=>$to_folder]));
if($to_origin_folder){
foreach ($arr as $fid=>$uid){
// 查询目录
$folder = db()->first(folderSql::first($fid));
if($folder){
// 开始远程
$mailInstance = new Mail($emails[$eid]['email'],base64_decode($emails[$eid]['password']),$emails[$eid]['imap']);
if($mailInstance->login()){
// TODO:: 这个过程无法保证原子性。没办法
// 先复制
$ret = $mailInstance->move(array_column($uid,'uid'),$folder['origin_folder'],$to_origin_folder['origin_folder']);
if($ret){
$uret = db()->update(listsSql::$table,['deleted'=>1],dbWhere(['id'=>array_column($uid,'id')]));
}
$mailInstance = null;
}
}
$folder = null;
}
}
}
}
app()->_json([
'mail_id' => $mail_ids
]);
}
/**
* 远程标签
* @param $d
* @throws \Lib\Err
* @author:dc
* @time 2023/3/21 14:28
*/
private function setFlags($d){
$emails = $this->getEmails();
$mail_ids = app()->request('mail_ids');
foreach ($mail_ids as $k=>$id){
if(!is_numeric($id)){
unset($mail_ids[$k]);
}
}
// 已读或未读
$fv = (int) app()->request($d);
$fv = $fv ? 1 : 0;
$data = db()->all(listsSql::first(dbWhere(['id'=>$mail_ids,'email_id'=>array_column($emails,'id')]),'`id`,`uid`,`email_id`,`folder_id`'));
if($data){
// 查询邮箱
$emails = array_column($emails,null,'id');
$uids = [];
foreach ($data as $datum){
if(empty($uids[$datum['email_id']])){
$uids[$datum['email_id']][$datum['folder_id']] = [];
}
$uids[$datum['email_id']][$datum['folder_id']][] = [
'uid' => $datum['uid'],
'id' => $datum['id'],
];
}
foreach ($uids as $eid=>$arr){
foreach ($arr as $fid=>$uid){
// 查询目录
$folder = db()->first(folderSql::first($fid));
if($folder){
// 开始远程
$mailInstance = new Mail($emails[$eid]['email'],base64_decode($emails[$eid]['password']),$emails[$eid]['imap']);
if($mailInstance->login()){
switch ($d){
// 已读 未读
case 'seen':{
$mailInstance->seen(array_column($uid,'uid'),$folder['origin_folder'],$fv);
break;
}
// 回收站,已删 未删,软删
// case 'deleted':{
// $mailInstance->recycle(array_column($uid,'uid'),$folder['origin_folder'],$fv);
// break;
// }
}
$mailInstance = null;
// 更新数据
db()->update(listsSql::$table,[
$d => $fv
],dbWhere([
'id' => array_column($uid,'id')
]));
}
}
$folder = null;
}
}
}
app()->_json([
'mail_id' => $mail_ids
]);
}
/**
* @author:dc
* @time 2023/4/1 9:24
*/
public function info(){
$id = app()->request('id',0,'intval');
// 没有,说明没有同步过来
$email = $this->getEmail();
$data = db()->first(listsSql::first(dbWhere(['id'=>$id,'email_id'=>$email['id']])));
if($data){
$sync_num = 0;
$data['to_name'] = json_decode($data['to_name']??"[]",true);
HOME_INFO_BODY:
$body = db()->first(bodySql::first($id));
if($body){
$data['body'] = json_decode($body['text_html'],true);
$charset = '';
foreach ($data['body'] as $bd){
if(!empty($bd['charset'])){
$charset = $bd['charset'];
break;
}
}
foreach ($data['body'] as $bdk=>$bd){
if(!empty($bd['path'])){
if($charset){
$data['body'][$bdk]['name'] = @iconv($charset,'utf-8',@base64_decode($bd['name']));
$data['body'][$bdk]['filename'] = @iconv($charset,'utf-8',@base64_decode($bd['filename']));
}
$data['body'][$bdk]['size'] = 0;
$data['body'][$bdk]['url'] = '';
if(is_file($bd['path'])){
// 文件大小
$data['body'][$bdk]['size'] = filesize($bd['path']);
// 文件访问地址
$data['body'][$bdk]['url'] = APP_HOST.str_replace(PUBLIC_PATH,'',$bd['path']);
}
unset($data['body'][$bdk]['path']);
}
}
return [
'data' => $data
];
}
// 循环几次
if($sync_num < 1){
$mail = new Mail($email['email'],base64_decode($email['password']),$email['imap']);
if($mail->login()){
$folder = db()->value(folderSql::first(['id'=>$data['folder_id']],'origin_folder'));
if($folder){
$ret = $mail->syncBody($folder,$data['uid'],$id);
$sync_num++;
if($ret){
goto HOME_INFO_BODY;
}
}
}
}
}
app()->e('mail_body_error');
}
}