SyncMail.php
3.6 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
<?php
namespace Service;
use Lib\Imap\ImapConfig;
use Lib\Imap\ImapPool;
use Lib\Imap\Parse\Folder\Folder;
use Model\emailSql;
use Model\folderSql;
/**
* 同步邮件
* @author:dc
* @time 2024/9/26 9:31
* Class SyncMail
* @package Service
*/
class SyncMail {
/**
* @var \Lib\Db|\Lib\DbPool
*/
protected $db;
/**
* @var \Lib\Imap\Imap
*/
protected $imap;
/**
* @var array
*/
protected $email;
/**
* SyncMail constructor.
* @param int|string|array $email
* @throws \Exception
*/
public function __construct(int|string|array $email)
{
$this->db = db();
if(!is_array($email)){
$email = $this->db->first(emailSql::first($email));
if(!$email){
abort('未查询到邮箱');
}
}
$this->email = $email;
// 实例一个imap类
$this->imap = ImapPool::get(
(new ImapConfig())
->setHost($email['imap'])
->setEmail($email['email'])
->setPassword(base64_decode($email['password']))
);
$this->login();
}
protected function emailId(){
return $this->email['id'];
}
/**
* 登录imap
* @throws \Exception
* @author:dc
* @time 2024/9/26 9:58
*/
private function login(){
$login = $this->imap->login();
if(!$login->isOk()){
abort($login->getMessage()?:'连接服务器异常');
}
}
/**
* 同步文件夹
* @author:dc
* @time 2024/9/26 10:22
*/
protected function folders(){
$uuids = [];
// 获取文件夹
$folders = $this->imap->getFolders();
$tmp = function ($folder) use ($folders,&$uuids){
foreach ($folder as $item){
/** @var Folder $item*/
$uuid = md5($this->emailId().$item->folder);
$uuids[$uuid] = $uuid;
$folder_name = '';
// 已发送
if(in_array('Send',$item->flags)){
$folder_name = folderAlias('Send');
}
// 草稿
elseif(in_array('Drafts',$item->flags)){
$folder_name = folderAlias('Drafts');
}
// 垃圾
elseif(in_array('Junk',$item->flags)){
$folder_name = folderAlias('Junk');
}
// 回收站
elseif(in_array('Trash',$item->flags)){
$folder_name = folderAlias('Trash');
}
if(!$folder_name){
$fn = explode('/',$item->getParseFolder());
$folder_name = folderAlias(end($fn));
}
// 是否存在
$id = $this->db->value(folderSql::has(['email_id'=>$this->emailId(),'uuid'=>$uuid]));
$data = [
'email_id' => $this->emailId(),
'folder' => folderAlias($folder_name),
'origin_folder' => $folder['folder'],
'uuid' => $uuid,
'pid' => $pid
];
if ($id){
}
}
};
$tmp($folders->getTopFolder());
if($uuids){
// 删除以前的
$this->db->delete(folderSql::$table,['uuid.notin'=>$uuids,'email_id'=>$this->emailId()]);
}
return $folders;
}
public function sync(){
$folders = $this->folders();
}
}