Search code examples
phpgoogle-apigmail-apigoogle-api-php-client

Php implementation for GMAIL API - filtering using multiple strings


For the GMAIL API - Is there a way to use multiple strings in the query without looping through each string individually. Below are some use cases:

1 - Get all results for emails that contain either domain1.com or domain2.com

$list = $service->users_messages->listUsersMessages('me',
  ['maxResults' => 100, 'q' => 'domain1.com  domain2.com']);

2 - Get all results for emails that contain either domain1.com or domain2.com and each domain specifically matches a string

$list = $service->users_messages->listUsersMessages('me',
  ['maxResults' => 100, 
   'q' => 'domain1.com statement ready, domain2.com view your statement']);

3 - Get all results for emails that specifically matches a string

$list = $service->users_messages->listUsersMessages('me',
  ['maxResults' => 100, 
   'q' => 'statement ready, view your statement']);

The comma in all the scenarios above is the separator of the string. The query works fine if i have a single domain or string. But it fails to return any result that have both of the strings in it e.g statement ready and view your statement.


Solution

  • You want to use the query as "AND" and "OR". If my understanding is correct, how about this answer? As a sample string, here, it uses "sample1", "sample2" and "sample3".

    Pattern 1 :

    If you want to retrieve mails including all strings of "sample1", "sample2" and "sample3", you can use the following query. It uses the space.

    sample1 sample2 sample3
    

    Also you can use like this. When the strings is enclosed by the parentheses, the strings in the the parentheses are used as "AND".

    (sample1 sample2 sample3)
    

    Pattern 2 :

    If you want to retrieve mails including at least one of strings of "sample1", "sample2" and "sample3", you can use the following query. OR is uppercase letter.

    sample1 OR sample2 OR sample3
    

    Also you can use like this. When the strings is enclosed by the curly bracket, the strings in the curly bracket are used as "OR".

    {sample1 sample2 sample3}
    

    Pattern 3 :

    From pattern 1 and 2, if you want to retrieve mails including both "sample1" and "sample2" OR mails including "sample3", you can use the following query.

    {(sample1 sample2) sample3}
    

    Reference :