Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all draft message with specific label attached to it

I have written a script, where I want to pull all draft message whose tag is shc.

Code:

function getDraftsWithLabel_shc() {
 Logger.clear();
 Logger.log("fn start..");
 var drafts = GmailApp.getDraftMessages(); //getUserLabelByName(ulables[x].getName()).

 var label = GmailApp.getUserLabelByName("shc");
     Logger.log(label);
     var threads = label.getThreads();
     Logger.log("LBL NAME ="+label.getName());
     Logger.log("THREAD ="+threads.length);

 for (var i = 0; i < threads.length; i++) {

       var message=threads[i].getMessages();
       Logger.log(" thread ID = "+threads[i].getId());

       if(message.length<0)
       {
           for(m=0;m<message.length;m++){
              var isD=message[m].isDraft();
              Logger.log("is Draft ="+isD);
              if(isD)
              {
                    Logger.log(message[m].getSubject()+" = "+message[m].getTo()+" = "+message[m].getDate());
              }
           }
       } 
       else 
       {
           Logger.log("No mesage");
       }
 } 
   Logger.log("fn Ends..");

}

Here in code i am trying to get the labelname and then respective threads. Then loop over thread and by using getMessages() check for if current isDraft then based on conditon get all other details.

First I am not able to get messages. Second is there any efficient way to get direct all Draft threads with specific labels coz looping over all thread is taking so much time


Screenshot of Log:

enter image description here

like image 270
Satinder singh Avatar asked Oct 28 '25 11:10

Satinder singh


1 Answers

drafts.list() now accepts raw query param q so you can simply specify it to do what you want. Something like:

q=in:<label>

See Gmail API's Users.drafts: list docs for more details.

like image 152
Eric D Avatar answered Oct 30 '25 09:10

Eric D