...
|
...
|
@@ -4,7 +4,9 @@ namespace Service; |
|
|
|
|
|
use Lib\Imap\ImapConfig;
|
|
|
use Lib\Imap\ImapPool;
|
|
|
use Lib\Imap\Parse\Folder\Folder;
|
|
|
use Model\emailSql;
|
|
|
use Model\folderSql;
|
|
|
|
|
|
/**
|
|
|
* 同步邮件
|
...
|
...
|
@@ -26,6 +28,11 @@ class SyncMail { |
|
|
protected $imap;
|
|
|
|
|
|
/**
|
|
|
* @var array
|
|
|
*/
|
|
|
protected $email;
|
|
|
|
|
|
/**
|
|
|
* SyncMail constructor.
|
|
|
* @param int|string|array $email
|
|
|
* @throws \Exception
|
...
|
...
|
@@ -40,7 +47,7 @@ class SyncMail { |
|
|
abort('未查询到邮箱');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$this->email = $email;
|
|
|
// 实例一个imap类
|
|
|
$this->imap = ImapPool::get(
|
|
|
(new ImapConfig())
|
...
|
...
|
@@ -49,12 +56,93 @@ class SyncMail { |
|
|
->setPassword(base64_decode($email['password']))
|
|
|
);
|
|
|
|
|
|
$this->login();
|
|
|
|
|
|
}
|
|
|
|
|
|
protected function emailId(){
|
|
|
return $this->email['id'];
|
|
|
}
|
|
|
|
|
|
public function syncFolder(){
|
|
|
/**
|
|
|
* 登录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();
|
|
|
}
|
|
|
|
|
|
|
...
|
...
|
|