MessageItem.php 2.9 KB
<?php

namespace Lib\Imap\Parse;

use Lib\Imap\Request\Msg;

/**
 * 邮件
 * @author:dc
 * @time 2024/9/18 11:30
 * @property Body $body
 * Class MessageItem
 * @package Lib\Imap\Parse
 */
class MessageItem {

    /**
     * 邮件的唯一id 文件夹下面唯一
     * @var int
     */
    public int $uid = 0;

    /**
     * 邮件编号
     * @var int
     */
    public int $msgno = 0;

    /**
     * 这个是邮件收到的时间
     * @var string
     */
    public string $date = '';

    /**
     * 邮件大小
     * @var int
     */
    public int $size = 0;

    /**
     * 标记
     * @var array
     */
    public array $flags = [];

    /**
     * 头部信息 主题 发件 收件 发送时间等消息
     * @var Header
     */
    public Header $header;


    /**
     * @var Msg
     */
    protected Msg $msg;

    /**
     * MessageItem constructor.
     * @param int $msgno
     */
    public function __construct(int $msgno, Msg $msg)
    {

        $this->msg = $msg;

        $this->msgno = $msgno;
    }

    /**
     * 是否已被标记为删除了
     * @return bool
     * @author:dc
     * @time 2024/9/18 11:52
     */
    public function isDeleted():bool {
        return in_array('deleted',$this->flags);
    }

    /**
     * 已读
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:53
     */
    public function isSeen():bool {
        return in_array('seen',$this->flags);
    }

    /**
     * 草稿
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:53
     */
    public function isDraft():bool {
        return in_array('seen',$this->flags);
    }

    /**
     * 星标
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:53
     */
    public function isFlagged():bool {
        return in_array('flagged',$this->flags);
    }

    /**
     * 已回复
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:52
     */
    public function isAnswered():bool {
        return in_array('answered',$this->flags);
    }

    /**
     * 最近
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:54
     */
    public function isRecent():bool {
        return in_array('recent',$this->flags);
    }

    /**
     * 获取邮件体 字段 内容
     * @param string $name
     * @return mixed
     * @author:dc
     * @time 2024/9/20 15:13
     */
    public function get(string $name){
        return $this->header->get($name);
    }

    /**
     * 获取body体
     * @return Body
     * @author:dc
     * @time 2024/9/20 15:14
     */
    public function getBody():Body {
        if(isset($this->body)){
            return $this->body;
        }

        $this->body = $this->msg->getBody($this);

        return $this->body;
    }


    public function __get(string $name)
    {
        if($name=='body'){
            return $this->getBody();
        }else{
            $this->get($name);
        }
    }

}