How do I perform an IMAP search based on a Delivered-Before header (or how do I use the imap gmail extension?)
I need to search for gmail messages (google apps to work) via imap / php. However, imap_search is not enough to catch the messages in question.
The code I am using looks like this:
$imap = imap_open("{imap.gmail.com:993/imap/ssl}Label1/label2", $user_email, $user_passwd);
$msg_list = imap_search($imap, 'TEXT "Delivered-To: username+label@gmail.com"');
imap_close($imap);
The imap_search call returns nothing.
I have done some research, it seems that I can filter messages based on the Delivered-To header field using the gmail search syntax X-GM-RAW
, but I just couldn't get it, I tried all these calls (and more):
$msg_list = imap_search($imap, 'UID SEARCH X-GM-RAW "deliveredto:username+label@gmail.com"');
$msg_list = imap_search($imap, 'SEARCH X-GM-RAW "deliveredto:username+label@gmail.com"');
$msg_list = imap_search($imap, 'X-GM-RAW "deliveredto:username+label@gmail.com"');
But it didn't work, does anyone know what happened to my code?
source to share
Okay, either I don't know how to ask questions, SOers are busy, or I am asking difficult questions.
Anyway, I now know that the imap_ * functions built into PHP do not handle direct IMAP commands, so I had to either use the zend framework (too heavy for my needs) or connect directly to imap via sockets.
I chose the second option, the code looks like this ( code stolen from here and adapted for my own needs ) in case anyone 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 user.name@gmail.com YOUR_PASSWORD_HERE_WITHOUT_QUOTES\r\n");
// ie. fwrite($fp, "0001 LOGIN super.dude@gmail.com 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:user.name+someLabel@gmail.com\"\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 you now have access to gmail syntax right from PHP! (Hello if you want: p)
source to share