Как получить флаги IMAP? - PullRequest
4 голосов
/ 20 июля 2010

Я использовал плагин imap4flag для сита Dovecot: http://wiki.dovecot.org/LDA/Sieve#Flagging_or_Highlighting_your_mail

Флаг правильно отображается в Thunderbird, но я ищу, как получить флаги для отображения их в RoundCube.

Спасибо заранее.

1 Ответ

6 голосов
/ 09 августа 2011

Это отсутствующая функция, см. Ошибка PHP # 53043: http://bugs.php.net/bug.php?id=53043

Пример кода, использующего непосредственно протокол IMAP:

<?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));
...