Search code examples
emailgoogle-apigmailgmail-apiincoming-mail

Advanced gmail search for received emails only


I'm using the Google gmail API to get emails from peoples gmail account. I filter for emails containing the word "CompanyName" which have a pdf as an attachment. using the following search query: CompanyName filename:pdf. I now want to further specify this by only getting emails that were received, instead of sent.

Does anybody know how I can filter by received emails in gmail? All tips are welcome!


Solution

  • You could specify that you don't want mails coming from you, or mails having your sent label etc.

    q = CompanyName filename:pdf AND -from:me AND -in:sent AND -is:chat AND -in:draft AND -in:trash
    

    Which would result in the following request:

    GET https://www.googleapis.com/gmail/v1/users/me/messages?q=CompanyName+filename%3Apdf+AND+-from%3Ame+AND+-in%3Asent+AND+-is%3Achat+AND+-in%3Adraft+AND+-in%3Atrash&key={YOUR_API_KEY}