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 [email protected] YOUR_PASSWORD_HERE_WITHOUT_QUOTES\r\n");
// ie. fwrite($fp, "0001 LOGIN [email protected] pass123\r\n");
// Keep fetching lines until response code is correct
while ($line = trim(fgets($fp)))
{
echo "Line = [$line]\n";
$line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
$code = $line[0];
if (strtoupper($code) == '0001') {
break;
}
}
// =========================================
fwrite($fp, "0002 SELECT Inbox\r\n");
// Keep fetching lines until response code is correct
while ($line = trim(fgets($fp)))
{
echo "Line = [$line]\n";
$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:[email protected]\"\r\n");
// Keep fetching lines until response code is correct
while ($line = fgets($fp))
{
echo "Line = [$line]\n";
$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)
Label1/label2
folder is restricting your search domain so that there aren't any legitimate results? What if you instead usedimap_open('{imap.gmail.com:993/imap/ssl}[Gmail]/All Mail', ...);
– bishopUnknown search criterion: SEARCH
when I executeimap_search($imap, 'SEARCH HEADER ...');
– TheDudeHEADER DELIVERED-TO "[email protected]"
. With Gmail you can also doX-GM-RAW "deliveredto:[email protected]"
. However, looking over the PHP source and documentation, it appears to use the deprecated "c-client" library to implement search, and that library might not support either of these search terms. Hopefully I'm wrong about that second part. – Jamie Nicolsonimap_search($imap, 'HEADER...')
. Don't put the word SEARCH in the search criteria. – Jamie Nicolson