php - 如何获得 imap 标志?

标签 php imap flags

我为 Dovecot 筛子使用了 imap4flag 插件:http://wiki.dovecot.org/LDA/Sieve#Flagging_or_Highlighting_your_mail

该标志在 thunderbird 中正确显示,但我搜索如何获取标志以在 roundcube 中显示它们。

提前致谢。

最佳答案

这是一个缺失的功能,请参阅 PHP 错误 #53043:http://bugs.php.net/bug.php?id=53043

直接使用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));

关于php - 如何获得 imap 标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3285690/

相关文章:

javascript - Google AdSense 在 WordPress 上显示空白广告

javascript - 如何让一个按钮对应不同的点击div html jquery php?

Python:IMAP 连接到 gmail 返回错误

python - 如何在 Python 的外部文件中保存全局变量?

javascript - 检查表单是否已通过php中的ajax提交

PHP IMAP - 电子邮件必须有 messageid 吗?

node.js - 我正在使用 IMAP npm 库来获取邮件,上面写着 "invalid credentials"

java - 在Android中,有没有办法设置标志/广播接收器来影响其他应用程序?

php - 如何获得序列化输出数据?