已经注意到 imap_search 在 imap4 语法中会中断。要执行 imap 4 搜索,请使用 curl 并发送自定义命令,然后获取结果。最好执行 UID 搜索以获取以后使用的唯一 ID。以下是一个带有效 curl 函数的示例。
<?php
$host = 'your-server.tld';
$user = 'username';
$pass = 'password';
$folder = 'INBOX';
function send_imap_command($server, $user, $pass, $command, $folder="INBOX")
{ $result=["response"=>"", "error"=>""];
$url = "imaps://$server/". rawurlencode($folder);
$options=[CURLOPT_URL=>$url, CURLOPT_PORT=> 993, CURLOPT_USERNAME=> $user,
CURLOPT_PASSWORD=> $pass, CURLOPT_RETURNTRANSFER=> true, CURLOPT_HEADER=> true,
CURLOPT_CUSTOMREQUEST=> $command];
$ch = curl_init();
curl_setopt_array($ch, $options);
$result["response"] = curl_exec($ch);
if(curl_errno($ch)) $response["error"]="Error (". curl_errno($ch) ."): ". curl_error($ch);
return $result;
}
$response=send_imap_command($host, $user, $pass,
'UID SEARCH SINCE "01-Jan-2022" (OR FROM "mailer-daemon" FROM "postmaster") (OR SUBJECT "fail" (OR SUBJECT "undeliver" SUBJECT "returned"))',
$folder);
if($response["error"]!="")
{
echo $response["error"]."\n";
} elseif (strlen($response["response"])>5){
$response["response"]=str_replace("* SEARCH ","",$response["response"]);
$messages=explode(" ",$response["response"]);
}
print_r($messages);
?>