imap with curl on php



我创建了一个非常简单的函数:

function send($command) {
$url = 'imaps://myImapServer';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, 993);
curl_setopt($ch, CURLOPT_USERNAME, "MyUsername");
curl_setopt($ch, CURLOPT_PASSWORD, "MyPassword");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $command);
$res = curl_exec($ch);
return $res;
}

我可以用它来查询文件夹,但也可以,例如,我所有消息的UID:

echo send('UID SEARCH ALL');
# Output: * SEARCH 63 64 65 66

但有两个问题我找不到解决办法。

  • 如何创建数组HTML Body, Text Body, Reply to,…从我的输出?

  • 为什么我用下面的查询得到主题的长度,但主题本身没有显示?

    echo send('UID FETCH 63 BODY[HEADER]。字段(主题)]);输出:* 1 FETCH (UID 63 BODY[HEADER])。FIELDS (SUBJECT)] {29}

您可以尝试使用CURLOPT_WRITEFUNCTION

curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $chunk) {
//your imap handling here
return strlen($chunk);
});

或者如果你在上课https://www.php.net/manual/en/function.curl-setopt.php#98491

curl_setopt($this->curl_handle, 
CURLOPT_WRITEFUNCTION, 
array($this, "class_method_name"));

CURLOPT_WRITEFUNCTION一直在监听你的"分手"输入,提供回调函数中接收到的数据块。

您可以使用回调函数将数据连接起来,将其全部推入数组,或者仅在收到数据时处理它,这取决于您的用例是什么。

最新更新