Msg.php
9.1 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
<?php
namespace Lib\Imap\Request;
use Lib\Imap\Imap;
use Lib\Imap\ImapSearch;
use Lib\Imap\Parse\Body as ParseBody;
use Lib\Imap\Parse\MessageItem;
use Lib\Imap\Parse\Messager;
/**
* 登录
* @author:dc
* @time 2024/9/13 17:09
* Class Login
* @package Lib\Imap\Request
*/
class Msg extends Request{
/**
* @var Folder
*/
public Folder $folder;
/**
* 读取列表条件
* @var array
*/
protected array $number = [];
/**
* 是否是uid
* @var bool
*/
private bool $isUid = false;
public function __construct(Imap $imap, Folder $folder)
{
parent::__construct($imap);
$this->folder = $folder;
}
public function exec(): static{return $this;}
/**
* 分页读取
* @param int $p
* @param int $limit
* @return $this
* @author:dc
* @time 2024/9/14 14:06
*/
public function forPage(int $p=1, int $limit=20):static {
$start = ($p-1)*$limit+1;
$end = $p*$limit;
// 如果开始大于 总数 就不要查询了
if($start <= $this->folder->getTotal()){
// 如果结束大于 总数 以总数为结束
if($end > $this->folder->getTotal()){
// 防止 有些邮件服务商 查询没有的邮件会异常
$end = $this->folder->getTotal();
}
}else{
// 如果 开始 大于总数 就始终 查询总数后的limit
$start = $this->folder->getTotal()+1;
$end = $start+$limit;
}
$this->msgno(range($start,$end));
return $this;
}
/**
* 使用uid进行查询
* @param array|int $uids
* @return $this
* @author:dc
* @time 2024/9/14 14:06
*/
public function uid(array|int $uids):static {
$this->isUid = true;
$this->setNumber($uids);
return $this;
}
/**
* 使用编号进行查询
* @param array|int $msgno
* @return $this
* @author:dc
* @time 2024/9/14 14:06
*/
public function msgno(array|int $msgno):static {
$this->isUid = false;
$this->setNumber($msgno);
return $this;
}
/**
* 设置邮件编号
* @param array|int $number
* @author:dc
* @time 2024/11/12 9:23
*/
protected function setNumber(array|int $number){
$this->number = array_map(function ($n){
return intval($n);
},is_array($number) ? $number : [$number]);
}
/**
* 搜索
* @param ImapSearch $search
* @return $this
* @author:dc
* @time 2024/9/14 15:48
*/
public function search(ImapSearch $search):static {
$this->folder->exec(); // 防止在其他文件夹下面
$this->cmd("UID SEARCH %s",$search->toString());
$uids = [];
foreach ($this->result as $item){
// 匹配uid * SEARCH 2 84 882
if(preg_match("/^\* SEARCH ([0-9\s]{1,})$/i",$item,$m)){
$uids = array_merge($uids,explode(' ',$m[1]));
}
}
$this->uid($uids);
return $this;
}
/**
* 只获取uid 读取远程的
* @return array
* @author:dc
* @time 2024/10/12 17:52
*/
public function getOriginUids():array {
$this->folder->exec(); // 防止在其他文件夹下面
$this->cmd(
"%sFETCH %s (UID)",
$this->isUid?'UID ':'',
implode(',',$this->number)
);
$uids = [];
$msg = new Messager($this->result, $this);
foreach ($msg->all() as $item){
$uids[] = $item->uid;
}
return $uids;
}
/**
* 读取当前对象下的uid
* @author:dc
* @time 2024/11/11 17:36
*/
public function getUids(){
if($this->isUid){
return $this->number;
}
return [];
}
/**
* 获取当前请求邮件的msgno或者uid
* @param false $isUid 是否返回uid
* @return array
* @author:dc
* @time 2024/11/11 17:40
*/
public function getNumber($isUid = false){
if($isUid){
return $this->getUids();
}
if(!$this->isUid){
return $this->number;
}
return [];
}
/**
* 读取邮件列表
* @return Messager
* @author:dc
* @time 2024/9/14 15:34
*/
public function get():Messager{
$this->folder->exec(); // 防止在其他文件夹下面
$this->cmd(
"%sFETCH %s (UID FLAGS INTERNALDATE RFC822.SIZE BODYSTRUCTURE RFC822.HEADER)",
$this->isUid?'UID ':'',
implode(',',$this->number)
);
return new Messager($this->result, $this);
}
/**
* 读取body 体
* @param MessageItem $msg
* @return ParseBody
* @author:dc
* @time 2024/9/20 17:09
*/
public function getBody(MessageItem $msg):ParseBody {
$this->folder->exec(); // 防止在其他文件夹下面
$this->cmd("UID FETCH %s (RFC822.TEXT)", $msg->uid);
return (new ParseBody(implode('',$this->result),$msg->header));
}
/**
* 添加/删除 标签
* @param string $flag
* @param bool $unset
* @return bool
* @author:dc
* @time 2024/9/24 9:51
*/
public function setFlags(string $flag, bool $unset = false):bool {
// FLAGS.SILENT 和 FLAGS 前者不返回新的标签 后者返回新的标签列表
// UID STORE 1 +FLAGS (\Seen) 这个是添加为已读
// UID STORE 1 -FLAGS (\Seen) 这个是标记为未读
$this->cmd("%sSTORE %s %sFLAGS.SILENT (\%s)",
$this->isUid ? 'UID ' : '',
implode(',',$this->number),
$unset ? '-' : '+',
$flag
);
return $this->isOk();
}
/**
* 标记为删除
* @return bool
* @author:dc
* @time 2024/9/18 11:52
*/
public function deleted():bool {
return $this->setFlags('\Deleted');
}
/**
* 取消删除标记
* @return bool
* @author:dc
* @time 2024/9/24 9:57
*/
public function undeleted():bool {
return $this->setFlags('\Deleted',true);
}
/**
* 标记为 已读
* @return bool
* @author:dc
* @time 2024/9/18 17:53
*/
public function seen():bool {
return $this->setFlags('\Seen');
}
/**
* 未读
* @return bool
* @author:dc
* @time 2024/9/24 9:58
*/
public function unseen():bool {
return $this->setFlags('\Seen',true);
}
/**
* 标记为 草稿
* @return bool
* @author:dc
* @time 2024/9/18 17:53
*/
public function draft():bool {
return $this->setFlags('\Draft');
}
public function undraft():bool {
return $this->setFlags('\Draft',true);
}
/**
* 标记为 星标
* @return bool
* @author:dc
* @time 2024/9/18 17:53
*/
public function flagged():bool {
return $this->setFlags('\Flagged');
}
/**
* 取消星标
* @return bool
* @author:dc
* @time 2024/9/24 9:59
*/
public function unflagged():bool {
return $this->setFlags('\Flagged',true);
}
/**
* 标记为已回复
* @return bool
* @author:dc
* @time 2024/9/18 17:52
*/
public function answered():bool {
return $this->setFlags('\Answered');
}
/**
* 取消标记 已回复
* @return bool
* @author:dc
* @time 2024/9/24 10:00
*/
public function unanswered():bool {
return $this->setFlags('\Answered',true);
}
/**
* 标记为最近
* @return bool
* @author:dc
* @time 2024/9/18 17:54
*/
public function recent():bool {
return $this->setFlags('\recent');
}
/**
* 取消最近标记
* @return bool
* @author:dc
* @time 2024/9/24 10:01
*/
public function unrecent():bool {
return $this->setFlags('\recent',true);
}
/**
* 移动邮件 到指定目录
* @param string $folder
* @param bool $utf7
* @return bool
* @author:dc
* @time 2024/9/24 10:47
*/
public function move(string $folder, bool $utf7 = true):bool {
if(!$utf7) $folder = mb_convert_encoding($folder,"UTF7-IMAP","UTF-8");
// "UID MOVE {$uids} \"{$folder}\""
$this->cmd('%s MOVE %s "%s"',
$this->isUid?'UID ':'',
implode(',',$this->number),
$folder
);
return $this->isOk();
}
/**
* 复制邮件到指定目录
* @param string $folder 目标文件夹
* @param bool $utf7 是否已转码
* @return bool
* @author:dc
* @time 2024/9/24 10:51
*/
public function copy(string $folder, bool $utf7 = true):bool {
if(!$utf7) $folder = mb_convert_encoding($folder,"UTF7-IMAP","UTF-8");
// "UID COPY {$uids} \"{$folder}\""
$this->cmd('%s COPY %s "%s"',
$this->isUid?'UID ':'',
implode(',',$this->number),
$folder
);
return $this->isOk();
}
}