Abhinav kumar Singh
Abhinav kumar Singh

Reputation: 11

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.

Upvotes: 1

Views: 707

Answers (2)

Abhinav kumar Singh
Abhinav kumar Singh

Reputation: 11

Thank you @DalmTo

Your post help me lot. I did some research got a similar but little bit different solution. I found a method in GmailApp class called search and sent a query(in:sent) as you suggested. This returned me all the sent mails.

I did something like this var _sentMail = GmailApp.search('in:sent'); This returned me array of GmailThread.

The one thing i did found that sent label in our gmail dashboard is not actually a label, its a method which takes a parameter "in:sent".

Upvotes: 0

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116908

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)
      }
     }
    );
  }
}

Upvotes: 1

Related Questions