如何获取imap标志? [英] How to get imap flags?

查看:86
本文介绍了如何获取imap标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将imap4flag插件用于Dovecot筛子: http://wiki.dovecot.org/LDA/Sieve#Flagging_or_Highlighting_your_mail

I used the imap4flag plugin for Dovecot sieve: http://wiki.dovecot.org/LDA/Sieve#Flagging_or_Highlighting_your_mail

该标志正确显示在雷鸟中,但我搜索如何在圆形立方体中显示这些标志.

The flag is correctly show in thunderbird but I search how get the flags for show them in roundcube.

先谢谢了.

推荐答案

这是一项缺少的功能,请参见PHP错误#53043:

This is a missing feature, see the PHP bug #53043 : http://bugs.php.net/bug.php?id=53043

直接使用IMAP协议的示例代码:

A example code using directly the IMAP protocol:

<?php
declare(strict_types=1);

class ImapSocket
{
    private $socket;

    public function __construct($options, $mailbox = '')
    {
        $this->socket = $this->connect($options['server'], $options['port'], $options['tls']);
        $this->login($options['login'], $options['password']);

        if ($mailbox !== null) {
            $this->select_mailbox($mailbox);
        }
    }

    private function connect(string $server, int $port, bool $tls)
    {
        if ($tls === true) {
            $server = "tls://$server";
        }

        $fd = fsockopen($server, $port, $errno);
        if (!$errno) {
            return $fd;
        }
        else {
            throw new \Exception('Unable to connect');
        }
    }

    private function login(string $login, string $password): void
    {
        $result = $this->send("LOGIN $login $password");
        $result = array_pop($result);

        if (substr($result, 0, 5) !== '. OK ') {
            throw new \Exception('Unable to login');
        }
    }

    public function __destruct()
    {
        fclose($this->socket);
    }

    public function select_mailbox(string $mailbox): void
    {
        $result = $this->send("SELECT $mailbox");
        $result = array_pop($result);

        if (substr($result, 0, 5) !== '. OK ') {
            throw new \Exception("Unable to select mailbox '$mailbox'");
        }
    }

    public function get_flags(int $uid): array
    {
        $result = $this->send("FETCH $uid (FLAGS)");
        preg_match_all("|\\* \\d+ FETCH \\(FLAGS \\((.*)\\)\\)|", $result[0], $matches);
        if (isset($matches[1][0])) {
            return explode(' ', $matches[1][0]);
        }
        else {
            return [];
        }
    }

    private function send(string $cmd, string $uid = '.')
    {
        $query = "$uid $cmd\r\n";
        $count = fwrite($this->socket, $query);
        if ($count === strlen($query)) {
            return $this->gets();
        }
        else {
            throw new \Exception("Unable to execute '$cmd' command");
        }
    }

    private function gets()
    {
        $result = [];

        while (substr($str = fgets($this->socket), 0, 1) == '*') {
            $result[] = substr($str, 0, -2);
        }
        $result[] = substr($str, 0, -2);

        return $result;
    }
}

用法:

<?php

$imap = new ImapSocket([
    'server' => 'localhost',
    'port' => 143,
    'login' => 'login',
    'password' => 'secret',
    'tls' => false,
], 'INBOX');
var_dump($imap->get_flags(0));

这篇关于如何获取imap标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆