Search code examples
pythonemailgoogle-apigmailgmail-api

Selectively get email messsages with Gmail API


I am trying to grab a list of messages that have a specific content e.g. billing emails and work on data in there. In order to get these messages, I run the following

service.users().messages().list(userId=user_id, page_token=page_token, q=query).execute()

which returns all the messages. I want to limit the messages that I get to confirm to the following criteria:

  • Sent in the last two days
  • Definitely deny if from: address not in a list of email addresses i.e. blacklist e.g. notifications, facebook
  • Definitely accept if from: address in a list of email addresses i.e. whitelist
  • Look if the subject: matches a set of strings

I understand that I can create a query that would match the email address and subject (from:[email protected] AND subject:"Your bill for this month"), but the blacklist and whitelist, as mentioned above, can become significantly large as the scope and the number of vendors I can accept increases, and similar is the case with subject. So my question is:

  • Is there a limit on the number of query terms?
  • Is there a way to achieve this other than generating a very long query string combining the black list whitelist and subject (from:[email protected] AND NOT from:[email protected] AND subject:"Your bill" AND subject:"This month's bill")?

Note: For project settings I mostly conform to https://developers.google.com/gmail/api/quickstart/python


Solution

  • There's no limit documented for the number of query terms you can use. Yes, you would have to create programmatically a long query string combining all the emails from the lists. Here [1] you can check the operators you can use, the best approach would be like this:

    1) Use "after" or "newer" operators with a timestamp from 2 days before the current date.

    2) -from:{[email protected] [email protected] ...}

    3) from:{[email protected] [email protected] ...}

    4) subject:{xxx xxx ...}

    [1] https://support.google.com/mail/answer/7190