Msg.php
3.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
<?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 {
$this->msgno(range(($p-1)*$limit+1,$p*$limit));
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->number = is_array($uids) ? $uids : [$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->number = is_array($msgno)?$msgno:[$msgno];
return $this;
}
/**
* 搜索
* @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 ",$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;
}
/**
* 读取邮件列表
* @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 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){
$this->folder->exec(); // 防止在其他文件夹下面
$this->cmd("UID FETCH %s (RFC822.TEXT)", $msg->uid);
return (new ParseBody(implode('',$this->result),$msg->header));
}
}