8
votes

I am using the PHP IMAP libraray's imap_search() function to search mails in a Gmail inbox via the subject string.

imap_seach($mbox, 'ALL SUBJECT "<search string>"');

This search returns perfectly fine for alphanumeric strings but fails when it has special characters like slash, comma, colon, single quote, hyphen, and many other characters that I do not even know of. Escaping them doesn't help. Replacing a few of them with space helps sometime but not in all cases.

Is there a standard way to filter the search string so that it never errors out and returns some result? I have tried tokenizing the subject sting and removing all words from the search string which even one alphanumeric characters. This mostly works but fails when all the words have non-alphanumeric character (which is common for single or two word subject).

1
Is there a way to encode the search string in unicode and then pass on the search string. According t RFC 2047 these chars are some special chars especials = "(" / ")" / "<" / ">" / "@" / "," / ";" / ":" / " <"> / "/" / "[" / "]" / "?" / "." / "=" I can't even figure hout how to escape them - Nands
My searches work fine with the @ symbol, but only sporadically - sometimes the right results are found, sometimes not. Did you get anywhere with your problem yet? - Ben
I wasn't able to find any solution that works in 100% cases. - Nands
I'm also finding the same problem with IMAP in the SEARCH HEADER Message-ID "xyz" command. If the "xyz" contains ! or & (among other characters), gmail basically truncates the parameter at that point. Essentially "abc&def" becomes "abc", and "!abc" becomes "" (ie. search for everything). I can't find anywhere in the IMAP language spec that describes why these chars fail, or how to escape them. - rocketmonkeys
Put print_r(imap_errors()); right after your imap_search() line and then produce your bug. - fie

1 Answers

2
votes

I'm guessing that GMail search goes by the idea that only whole alphanumeric words can be used as search strings..

Therefore you'd have to remove all non-alphanumerics from your search string, and it will work...