duanche5149 2014-09-30 23:12
浏览 147
已采纳

如何根据“Delivered-To”标题(或如何使用gmail imap扩展名?)进行IMAP搜索?

I need to search gmail messages (google apps for work) via imap/php. However, imap_search criterias are not enough to catch the messages in question.

The code I use looks like this:

$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);

The imap_search call didn't return anything.

I did some research, it seems I can filter messages based on "Delivered-To" header field via the gmail search syntax X-GM-RAW, but I just couldn't achieve this, I tried all these calls (and more):

$msg_list = imap_search($imap, 'UID SEARCH X-GM-RAW "deliveredto:username+label@gmail.com"');
$msg_list = imap_search($imap, 'SEARCH X-GM-RAW "deliveredto:username+label@gmail.com"');
$msg_list = imap_search($imap, 'X-GM-RAW "deliveredto:username+label@gmail.com"');

But it didn't work, anyone knows what's wrong with my code?

  • 写回答

1条回答 默认 最新

  • dongmangji0950 2014-10-02 13:10
    关注

    OK, either I don't know how to ask questions, SOers are busy, or I'm asking difficult questions.

    Anyway, now I know the imap_* functions built-into PHP don't handle direct IMAP commands, so I had to either use the zend framework (too heavy for my needs), or directly connect to imap via sockets.

    I chose the second option, the code is as follow (code stolen from here and adapted for my own needs), in case someone needs it:

    <?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
    ");
    // ie. fwrite($fp, "0001 LOGIN super.dude@gmail.com pass123
    ");
    
    // Keep fetching lines until response code is correct
    while ($line = trim(fgets($fp)))
    {
        echo "Line = [$line]
    ";
        $line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
        $code = $line[0];
    
        if (strtoupper($code) == '0001') {
            break;
        }
    }
    // =========================================
    
    fwrite($fp, "0002 SELECT Inbox
    ");
    
    // Keep fetching lines until response code is correct
    while ($line = trim(fgets($fp)))
    {
        echo "Line = [$line]
    ";
        $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\"
    ");
    // Keep fetching lines until response code is correct
    while ($line = fgets($fp))
    {
        echo "Line = [$line]
    ";
        $line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
        $code = $line[0];
    
        if (strtoupper($code) == '0003') {
            break;
        }
    }
    
    fclose($fp);
    echo "I've finished!";
    ?>
    

    Voila! Just copy and paste and now you have access to the gmail syntax right from PHP! (Hey vote if you like :p)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程