Imap.php
3.4 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
<?php
namespace Lib\Imap;
use Lib\Imap\Request\Folder;
use Lib\Imap\Request\Folders;
use Lib\Imap\Request\Login;
use Lib\Imap\Request\Logout;
use Lib\Imap\Request\Noop;
class Imap {
/**
* 配置
* @var ImapConfig
*/
public ImapConfig $config;
/**
* @var ImapClient
*/
public ImapClient $client;
/**
* 当前在那个文件夹下面目录
* @var string
*/
protected string $checkedFolder = '';
/**
* Imap constructor.
* @param ImapConfig $config
*/
public function __construct(ImapConfig $config)
{
$this->config = $config;
$this->client = new ImapClient($this->config->getHost());
$this->debug($config->isDebug());
}
/**
* 设置是否调试
* @param bool $debug
* @return $this
* @author:dc
* @time 2024/9/19 9:41
*/
public function debug(bool $debug = true):static {
$this->client->setDebug($debug);
return $this;
}
/**
* @return Login
* @author:dc
* @time 2024/9/25 18:12
*/
public function login():Login {
// 打开连接
try {
$this->client->open();
}catch (\Throwable $e){
return (new Login($this));
}
//有的服务器是强制要求设置id属性 已知 163 有的服务器没有id命令 这里就不处理结果了
$this->client->request('ID ("name" "测试本地 Client" "version" "1" "os" "测试本地" "os-version" "1.0")');
// 登录
return (new Login($this))->exec();
}
/**
* 选择文件夹
* @param string|\Lib\Imap\Parse\Folder\Folder $folder 目录
* @param bool $utf7 是否是utf7编码
* @return Folder
* @author:dc
* @time 2024/9/19 9:57
*/
public function folder(string|\Lib\Imap\Parse\Folder\Folder $folder, bool $utf7 = true):Folder {
if($folder instanceof \Lib\Imap\Parse\Folder\Folder){
$folder = $folder->folder;
}else{
if(!$utf7) $folder = mb_convert_encoding($folder,"UTF7-IMAP","UTF-8");
}
return new Folder($this,$folder);
}
/**
* 读取文件夹
* @return \Lib\Imap\Parse\Folder\Folders
* @author:dc
* @time 2024/9/19 14:10
*/
public function getFolders():\Lib\Imap\Parse\Folder\Folders {
$folders = new Folders($this);
return $folders->get();
}
/**
* 类似ping
* @return bool
* @author:dc
* @time 2024/9/20 14:45
*/
public function noop():bool {
return (new Noop($this))->exec()->isOk();
}
/**
* 读取或验证是否在当前目录下面
* @param string $folder
* @return bool|string
* @author:dc
* @time 2024/9/20 13:58
*/
public function checkedFolder(string $folder = ''):bool|string {
if($folder){
return $folder == $this->checkedFolder;
}
return $this->checkedFolder;
}
/**
* 设置当前进入那个文件夹
* @param string $folder
* @author:dc
* @time 2024/9/20 14:00
*/
public function setCheckedFolder(string $folder):void {
$this->checkedFolder = $folder;
}
public function __destruct()
{
// 退出登录
(new Logout($this))->exec();
unset($this->client);
// TODO: Implement __destruct() method.
}
}