Mail.php
6.2 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
<?php
namespace Lib\Mail;
use Event\Event;
use Event\syncMail;
use Lib\DbPool;
use Model\bodySql;
use Model\folderSql;
use Model\listsSql;
/**
* 操作邮件
* @author:dc
* @time 2023/2/5 10:10
* Class MailFun
* @package Helper\Mail
*/
class Mail {
/**
* imap服务器连接实例
* @var Imap
*/
public Imap $client;
/**
* @var string
*/
private string $username;
/**
* @var string
*/
private string $password;
/**
* @var string
*/
private string $server;
/**
* Mail constructor.
* @param string $email
* @param string $password
* @param string $imap
*/
public function __construct(string $email,string $password,string $imap)
{
$this->username = $email;
$this->password = $password;
$this->server = $imap;
$this->client = new Imap();
}
/**
* 登录imap服务器
* @param bool $pass_err
* @return int
* @author:dc
* @time 2023/3/14 10:03
*/
public function login($pass_err=true):int {
// 处理url
$host = MailFun::getHostPort($this->server,993,'ssl://');
try {
// 是否初始成功
$this->client->login($host['host'].':'.$host['port'],$this->username,$this->password);
}catch (\Throwable $e){
logs($this->username.'===>'.$e->getMessage());
if($pass_err){
// 是否是密码错误
foreach ([
'NO [ALERT] Invalid credentials (Failure)',// 登录失败
'NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)',// 登录失败
'NO [AUTHENTICATIONFAILED] Authentication failed.',// 登录失败 权限
'NO LOGIN Login error',// 登录失败
'NO LOGIN auth error',// 登录失败
'NO ERR.LOGIN.PASSERR',// 登录失败 密码错误
'NO Login fail.',// 登录失败
'NO LOGIN failed.', // 登录失败
// 'NO ERR.LOGIN.REQCODE', // 未知错误
'NO [ALERT] Application-specific password', // 这个错误是没有提供特定的授权码
'NO LOGIN Login error, user name or password error'
] as $em){
if(str_contains($e->getMessage(), $em)){
db()->update(
\Model\emailSql::$table,
['pwd_error'=>1],
dbWhere(['email'=>$this->username])
);
}
}
// 一天中超过 3次失败说明密码错误了
// if(redis()->incr('email_login_error:'.md5($this->username),86400) > 10){
// 登录失败了 ,
// db()->update(\Model\emailSql::$table,['pwd_error'=>1],dbWhere(['email'=>$this->username]));
// }
return -1;
}
return $e->getCode() == 403 ? 0 : -1;
}
// redis()->delete('email_login_error:'.md5($this->username));
return 1;
}
/**
* 设置为未读
* @param $uids
* @return bool
* @throws \Exception
* @author:dc
* @time 2022/10/26 17:11
*/
public function seen($uids,$folder,$seen):bool{
// 选择目录
$status = $this->client->selectFolder($folder);
return $this->client->flags($uids,[Imap::FLAGS_SEEN],$seen ? '+' : '-',true);
}
/**
* 删除标记
* @param $uids
* @param $folder
* @param $del
* @return bool
* @throws \Exception
* @author:dc
* @time 2024/3/9 16:50
*/
public function deleted($uids,$folder,$del=true):bool{
// 选择目录
$status = $this->client->selectFolder($folder);
return $this->client->flags($uids,[Imap::FLAGS_DELETED],$del ? '+' : '-',true);
}
/**
* 回复标记
* @param $uids
* @param $folder
* @param $seen
* @return bool
* @throws \Exception
* @author:dc
* @time 2023/4/6 17:10
*/
public function answered($uids,$folder,$seen):bool{
// 选择目录
$status = $this->client->selectFolder($folder);
return $this->client->flags($uids,[Imap::FLAGS_ANSWERED],$seen ? '+' : '-',true);
}
/**
* 回复标记
* @param $uids
* @param $folder
* @param $flagged
* @return bool
* @throws \Exception
* @author:dc
* @time 2023/4/6 17:10
*/
public function flagged($uids,$folder,$flagged):bool{
// 选择目录
$status = $this->client->selectFolder($folder);
return $this->client->flags($uids,[Imap::FLAGS_FLAGGED],$flagged ? '+' : '-',true);
}
/**
* 复制
* @param $uids
* @param $folder
* @param $to_folder
* @return bool
* @throws \Exception
* @author:dc
* @time 2023/3/22 16:38
*/
public function copy($uids,$folder,$to_folder){
// 选择目录
$status = $this->client->selectFolder($folder);
return $this->client->copy($uids,$to_folder);
}
/**
* 移动邮件
* @param $uids
* @param $folder
* @param $to_folder
* @return bool
* @throws \Exception
* @author:dc
* @time 2023/3/22 18:06
*/
public function move($uids,$folder,$to_folder){
// 选择目录
$status = $this->client->selectFolder($folder);
return $this->client->move($uids,$to_folder);
}
/**
* 清空标记为已删除的邮件,不可还原邮件
* @author:dc
* @time 2024/3/14 14:11
*/
public function expunge(){
return $this->client->expunge();
}
// /**
// * 删除
// * @param $uids
// * @param $folder
// * @return bool
// * @throws \Exception
// * @author:dc
// * @time 2023/3/22 17:52
// */
// public function delete($uids,$folder){
// // 选择目录
// $status = $this->client->selectFolder($folder);
//
// return $this->client->delete($uids);
// }
}