已注意到imap_search函数与imap4语法不兼容。要执行imap4搜索,请使用curl发送自定义命令,然后获取结果。最好进行UID搜索以获取唯一的ID,以便以后使用。以下是一个带有可运行curl函数的示例。
<?php
$host = '你的服务器地址.tld';
$user = '用户名';
$pass = '密码';
$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"]="错误 (". 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);
?>