1
votes

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.

2
To summarize from the linked answer, you can use the list method (Documentation here), adding SENT as the labelIds parameter. To use thgis from an Apps Script you will need to enable Google Advanced ServicesAMolina
@DaImTo yes, which is why I added the comment, the approach is the same however. Would that not make it a duplicate?AMolina
Fair enough, removed the comment.AMolina

2 Answers

1
votes

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

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".