Is there a function in Google app script to get an array of all sent mails

I am developing a add-on program in google app script which gets all the gmail sent mails with there subject, body, attachment(if any). I have did this for the Inbox mail using getInboxThreads() function. Is there a function which does same for sent mails?

The program that i am writing is for any gmail user or a group of users how wants to save their gmail emails on the google drive for monitoring or any other operations.

1 answer

  • answered 2019-09-11 12:01 DaImTo

    You can use the user.messages.list method to get a list of all the message ids for the user. You will then have to use user.messages.get To get the data about the message.

    You can use the 'q': 'in:sent' parameter to get only the messages in the sent folder.

    function myFunction() {
      var myMessages=Gmail.Users.Messages.list("me",{'maxResults': 5 , 'q': 'in:sent' }).messages;
      Logger.log(myMessages);
      for(var i=0;i<myMessages.length;i++){
        var id=myMessages[i].id;
        Gmail.Users.Messages.get('me', id).payload.headers.forEach(function(e){
          if(e.name=="Subject"||e.name=="From"){
            Logger.log(e.name+": "+e.value)
          }
         }
        );
      }
    }