Search code examples
google-apps-scriptgoogle-apigmailgmail-api

Get message in Gmail with Google App Script with specific email


I have a google app script read email to google sheet , and it get all unread inbox email by var SEARCH_QUERY = "label:inbox is:unread to:me";

But I just want to get 10 newest message from email abc1@gmail.com , how can i achieve that ?

I have tried var SEARCH_QUERY = "mailfrom:abc1@gmail.com" but it does not work .


Solution

  • SUGGESTION

    NOTE: Please share a sample script that you're working on in case there's anything else missing on this answer.

    You can try this query:

    is:unread from:(email_address)

    Sample Script

    function myFunction() {
      // Find unread messages from info@send.grammarly.com
      var SEARCH_QUERY = 'is:unread from:(info@send.grammarly.com)';
      var threads = GmailApp.search(SEARCH_QUERY);
    
      //Only filter the 10 most recent messages
      var last10NewestMessages = threads.filter((_, index) => { return index <= 9 });
    
      //E.g. log result to return the recent 10 messages' date just to confirm that they're the 10 most recent messages 
      console.log(last10NewestMessages.map(sampleGMessageDate => { return [sampleGMessageDate.getLastMessageDate()] }))
    }
    

    Demonstration

    enter image description here

    References: