note 126758 added to function.imap-search

From: Date: Wed, 12 Jan 2022 12:07:58 +0000
Subject: note 126758 added to function.imap-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-217484@lists.php.net to get a copy of this message
It has been noted that imap_search breaks with imap4 syntax. To do an imap 4 search use curl and send a custom command, then grab the results. Its best to do a UID search to get the unique IDs to work with later. Here's an example with a working curl function. <?php $host = 'your-server.tld'; $user = 'username'; $pass = 'password'; $folder = 'INBOX'; function send_imap_command($server, $user, $pass, $command, $folder="INBOX") { //Send an imap command directly to the imap server $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; } //Pull out all the emails returned as undeliverable by the remote mail server in the inbox using curl $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){ //Server returns a string in the form * SEARCH uid1 uid2 uid3 ... Clean up and create array of UIDs. $response["response"]=str_replace("* SEARCH ","",$response["response"]); $messages=explode(" ",$response["response"]); } print_r($messages); ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 78.32.103.91) ---- Manual Page -- https://php.net/manual/en/function.imap-search.php Edit -- https://main.php.net/note/edit/126758 Del: integrated -- https://main.php.net/note/delete/126758/integrated Del: useless -- https://main.php.net/note/delete/126758/useless Del: bad code -- https://main.php.net/note/delete/126758/bad+code Del: spam -- https://main.php.net/note/delete/126758/spam Del: non-english -- https://main.php.net/note/delete/126758/non-english Del: in docs -- https://main.php.net/note/delete/126758/in+docs Del: other reasons-- https://main.php.net/note/delete/126758 Reject -- https://main.php.net/note/reject/126758 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217484) next »