IMAP search by subject fails on punctuation characters on Gmail
Asked Answered
E

1

8

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).

Evolution answered 29/9, 2010 at 7:46 Comment(5)
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 themEvolution
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?Confectioner
I wasn't able to find any solution that works in 100% cases.Evolution
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.Wolsky
Put print_r(imap_errors()); right after your imap_search() line and then produce your bug.Angary
S
2

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...

Successful answered 24/3, 2012 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.