SyncMail.php 3.6 KB
<?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();
    }















}