ImapSearch.php 5.1 KB
<?php
namespace Lib\Imap;

/**
 * 搜索邮箱
 * TODO:: 有些邮箱服务器并不支持搜索 或者 某些字段搜索
 * 日期类搜索是没问题的
 * @author:dc
 * @time 2024/9/14 15:48
 * Class ImapSearch
 * @package Lib\Imap
 */
class ImapSearch {

    /**
     * @var array
     */
    protected array $where = [];



    /**
     * 搜索已标记为 已回复 的邮件
     * @param bool $a 是否已标记为 已回复
     * @return $this
     * @author:dc
     * @time 2024/9/14 16:22
     */
    public function answered(bool $a = true):static {
        $this->where[$a?'ANSWERED':'UNANSWERED'] = true;
        return $this;
    }

    /**
     * 搜索已标记为删除的
     *  Messages with the \Deleted flag set.
     * @param bool $del 是否已删除的
     * @return $this
     * @author:dc
     * @time 2024/9/14 16:21
     */
    public function deleted(bool $del = true):static {
        $this->where[$del?'DELETED':'UNDELETED'] = true;
        return $this;
    }

    /**
     * 搜索标记为草稿的
     * Messages with the \Draft flag set.
     * @param bool $draft 是否是草稿
     * @return $this
     * @author:dc
     * @time 2024/9/14 16:20
     */
    public function draft(bool $draft = true):static {
        $this->where[$draft?'DRAFT':'UNDRAFT'] = true;
        return $this;
    }

    /**
     * 搜索标记为星标的邮件
     * @param bool $flagged 是否带星标
     * @return $this
     * @author:dc
     * @time 2024/9/14 16:19
     */
    public function flagged(bool $flagged = true):static {
        $this->where[$flagged?'FLAGGED':'UNFLAGGED'] = true;
        return $this;
    }

    /**
     * 搜索未读 还是已读
     * @param bool $seen 是否标记为已读
     * @return $this
     * @author:dc
     * @time 2024/9/14 16:23
     */
    public function seen(bool $seen = true):static {
        $this->where[$seen?'SEEN':'UNSEEN'] = true;
        return $this;
    }


    /**
     * 搜索密送 中有这个邮箱的
     * @param string $bcc 邮箱
     * @return $this
     * @author:dc
     * @time 2024/9/14 15:57
     */
    public function bcc(string $bcc):static {
        $this->where['BCC'] = $bcc;
        return $this;
    }

    /**
     * 搜索抄送 中有这个邮箱的
     * Messages that contain the specified string in the envelope structure's CC field.
     * @param string $cc 邮箱
     * @return $this
     * @author:dc
     * @time 2024/9/14 16:06
     */
    public function cc(string $cc):static {
        $this->where['CC'] = $cc;
        return $this;
    }

    /**
     * 搜索 发件人
     * @param string $from
     * @return $this
     * @author:dc
     * @time 2024/9/14 16:10
     */
    public function from(string $from):static {
        $this->where['FROM'] = $from;
        return $this;
    }

    /**
     * 搜索收件人
     * @param string $to
     * @return $this
     * @author:dc
     * @time 2024/9/14 16:17
     */
    public function to(string $to):static {
        $this->where['TO'] = $to;
        return $this;
    }

    /**
     * 搜索内容
     *  Messages that contain the specified string in the body of the message.
     * @param string $body
     * @return $this
     * @author:dc
     * @time 2024/9/14 16:05
     */
    public function body(string $body):static {
        $this->where['BODY'] = $body;
        return $this;
    }

    /**
     * 搜索 日期 小于某个日期的
     * Messages whose internal date (disregarding time and timezone) is earlier than the specified date.
     * @param string $date 日期
     * @author:dc
     * @time 2024/9/14 16:03
     */
    public function dateLt(string $date):static{
        $this->where['BEFORE'] = date('d-M-Y',strtotime($date));
        return $this;
    }

    /**
     * 搜索指定日期的邮件
     * Messages whose internal date (disregarding time and timezone) is within the specified date.
     * @param string $date
     * @author:dc
     * @time 2024/9/14 16:12
     */
    public function date(string $date):static{
        $this->where['ON'] = date('d-M-Y',strtotime($date));
        return $this;
    }

    /**
     * 搜索指定日期后的邮件 如 2024-04-04 显示这个日期过后的
     * Messages whose internal date (disregarding time and timezone) is within or later than the specified date.
     * @param string $date
     * @author:dc
     * @time 2024/9/14 16:14
     */
    public function dateGt(string $date):static{
        $this->where['SINCE'] = date('d-M-Y',strtotime($date));
        return $this;
    }

    /**
     * 模糊搜索主题
     * @param string $subject 主题
     * @author:dc
     * @time 2024/9/14 16:16
     */
    public function subject(string $subject):static {
        $this->where['SUBJECT'] = $subject;
        return $this;
    }


    /**
     * 返回搜索字符串
     * @author:dc
     * @time 2024/9/14 16:27
     */
    public function toString():string {
        $arr = [];
        foreach ($this->where as $k=>$v){
            if(is_bool($v)){
                $arr[] = $k;
            }else{
                $arr[] = $k.' "'.addcslashes($v,'"').'"';
            }
        }
        return implode(' ',$arr);
    }



}