Php 如何基于“进行IMAP搜索”;交付给;标题(或者如何使用gmail imap扩展?)

Php 如何基于“进行IMAP搜索”;交付给;标题(或者如何使用gmail imap扩展?),php,gmail,imap,gmail-imap,php-5.4,Php,Gmail,Imap,Gmail Imap,Php 5.4,我需要通过imap/php搜索gmail消息(谷歌工作应用程序)。然而,这些信息还不足以捕获有问题的消息 我使用的代码如下所示: $imap = imap_open("{imap.gmail.com:993/imap/ssl}Label1/label2", $user_email, $user_passwd); $msg_list = imap_search($imap, 'TEXT "Delivered-To: username+label@gmail.com"'); imap_close

我需要通过imap/php搜索gmail消息(谷歌工作应用程序)。然而,这些信息还不足以捕获有问题的消息

我使用的代码如下所示:

$imap = imap_open("{imap.gmail.com:993/imap/ssl}Label1/label2", $user_email, $user_passwd);

$msg_list = imap_search($imap, 'TEXT "Delivered-To: username+label@gmail.com"');

imap_close($imap);
imap_搜索呼叫没有返回任何内容

我做了一些研究,似乎我可以通过
X-GM-RAW
根据“Delivered To”标题字段过滤消息,但我就是无法做到这一点,我尝试了所有这些调用(以及更多):


但它不起作用,有人知道我的代码出了什么问题吗?

好的,要么我不知道如何提问,要么我很忙,要么我在问一些困难的问题

无论如何,现在我知道PHP中内置的imap_*函数不能处理直接的imap命令,所以我不得不使用zend框架(对于我的需求来说太重了),或者通过套接字直接连接到imap

我选择了第二个选项,代码如下(),以防有人需要它:

<?php
// Open a socket
if (!($fp = fsockopen('ssl://imap.gmail.com', 993, $errno, $errstr, 15))) die("Could not connect to host");

// Set timout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout");

// Fetch first line of response and echo it
echo fgets($fp);

// =========================================
fwrite($fp, "0001 LOGIN user.name@gmail.com YOUR_PASSWORD_HERE_WITHOUT_QUOTES\r\n");
// ie. fwrite($fp, "0001 LOGIN super.dude@gmail.com pass123\r\n");

// Keep fetching lines until response code is correct
while ($line = trim(fgets($fp)))
{
    echo "Line = [$line]\n";
    $line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
    $code = $line[0];

    if (strtoupper($code) == '0001') {
        break;
    }
}
// =========================================

fwrite($fp, "0002 SELECT Inbox\r\n");

// Keep fetching lines until response code is correct
while ($line = trim(fgets($fp)))
{
    echo "Line = [$line]\n";
    $line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
    $code = $line[0];

    if (strtoupper($code) == '0002') {
        break;
    }
}
// =========================================

fwrite($fp, "0003 SEARCH X-GM-RAW \"deliveredto:user.name+someLabel@gmail.com\"\r\n");
// Keep fetching lines until response code is correct
while ($line = fgets($fp))
{
    echo "Line = [$line]\n";
    $line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
    $code = $line[0];

    if (strtoupper($code) == '0003') {
        break;
    }
}

fclose($fp);
echo "I've finished!";
?>

也许将其绑定到
Label1/label2
文件夹限制了您的搜索域,因此没有任何合法结果?如果改用
imap_open({imap.gmail.com:993/imap/ssl}[gmail]/All-Mail',…)
@bishop:Thank,试过了,也没用:(@JamieNicolson:Thank,但是当我执行
imap\u search($imap,'search HEADER…')时,我得到了一个
未知搜索条件:search
搜索参数的正确imap语法是
HEADER DELIVERED-TO-username+label@gmail.com"
。使用Gmail,您还可以将
X-GM-RAW“发送到:用户名+label@gmail.com
。但是,查看PHP源代码和文档,它似乎使用了不推荐使用的“c-client”库来实现搜索,而该库可能不支持这两个搜索词中的任何一个。希望我对第二部分的理解是错误的。它将是
imap\u搜索($imap,'HEADER…')
。不要将单词search放在搜索条件中。
<?php
// Open a socket
if (!($fp = fsockopen('ssl://imap.gmail.com', 993, $errno, $errstr, 15))) die("Could not connect to host");

// Set timout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout");

// Fetch first line of response and echo it
echo fgets($fp);

// =========================================
fwrite($fp, "0001 LOGIN user.name@gmail.com YOUR_PASSWORD_HERE_WITHOUT_QUOTES\r\n");
// ie. fwrite($fp, "0001 LOGIN super.dude@gmail.com pass123\r\n");

// Keep fetching lines until response code is correct
while ($line = trim(fgets($fp)))
{
    echo "Line = [$line]\n";
    $line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
    $code = $line[0];

    if (strtoupper($code) == '0001') {
        break;
    }
}
// =========================================

fwrite($fp, "0002 SELECT Inbox\r\n");

// Keep fetching lines until response code is correct
while ($line = trim(fgets($fp)))
{
    echo "Line = [$line]\n";
    $line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
    $code = $line[0];

    if (strtoupper($code) == '0002') {
        break;
    }
}
// =========================================

fwrite($fp, "0003 SEARCH X-GM-RAW \"deliveredto:user.name+someLabel@gmail.com\"\r\n");
// Keep fetching lines until response code is correct
while ($line = fgets($fp))
{
    echo "Line = [$line]\n";
    $line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
    $code = $line[0];

    if (strtoupper($code) == '0003') {
        break;
    }
}

fclose($fp);
echo "I've finished!";
?>