Search code examples
google-apps-scriptgmail

Apps script for Gmail - search for exact subject


I get every day multiple automatic emails. One has the subject 'Daily Report' and one has the subject 'Daily Report details'. This is the script I was using, but once the emails started coming without chronological order, I sometimes catch the wrong one.

 var threads = GmailApp.search('Daily Report') //search gmail with the given query(partial name using * as a wildcard to find anything in the current subject name).
 var msgs = GmailApp.getMessagesForThreads(threads);

Is there a way to tell the search that I want the mail with exact subject 'Daily Report' and not the one with 'Daily Report Details'?

Thanks


Solution

  • Like @Pierre-Marie Richard and @Cooper mentioned, you can use the built-in Gmail search functions. Here are a list of the Gmail Search Functions, like @Cooper mentioned you may be best using something like:

    var threads = GmailApp.search('subject:"Daily Report"-Details')
    var msgs = GmailApp.getMessagesForThreads(threads);
    

    The quotes ("") work as an exact match and the minus ('-') is to exclude a word. If that still doesn't work let me know and I will look further into it.

    Good Luck :)