1

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.

jcoppens
  • 5,306
  • 6
  • 27
  • 47
  • To summarize from the linked answer, you can use the `list` method ([Documentation here](https://developers.google.com/gmail/api/v1/reference/users/messages/list)), adding `SENT` as the `labelIds` parameter. To use thgis from an Apps Script you will need to [enable Google Advanced Services](https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services) – AMolina Sep 11 '19 at 12:01
  • @DaImTo yes, which is why I added the comment, the approach is the same however. Would that not make it a duplicate? – AMolina Sep 11 '19 at 12:02
  • Fair enough, removed the comment. – AMolina Sep 11 '19 at 12:05

2 Answers2

1

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)
      }
     }
    );
  }
}
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Hi, I tried using your code but every time i run it, it gives me a error ""Gmail" is not defined." Might i be doing something wrong. – Abhinav kumar Singh Sep 12 '19 at 10:23
  • gmail is the service that you need to setup – Linda Lawton - DaImTo Sep 12 '19 at 10:25
  • I am sorry if i am asking to much from you. I am very new to this. So according to whatever knowledge i have and what i got from internet "Gmail.Users.Messages.list" is from google Gmail restful api services. So do i need to do CONFIGURE something before using this restful service. A doubt that i have is, if this is a restful service i found nothing about configuring its endpoint over internet. Even if you can provide some reference's that will also be great. – Abhinav kumar Singh Sep 12 '19 at 12:13
0

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