How to read all emails in gmail using google apps script

ぃ、小莉子 提交于 2019-12-10 22:54:23

问题


I'm trying to read ALL email in my gmail account - inbox, sent, draft, trash, emails with labels, archive, etc. I could live without the junk but I want everything else.

(all examples below use try {} catch {} to avoid errors with empty labels etc.)

I've tried

for (var i=StartLabel; i<=EndLabel; i++)
{
  var label = labels[i].getName();

  // get all messages, then join them into a single dimension array
  var messages = GmailApp.getMessagesForThreads(GmailApp.search("label:" + label))
                   .reduce(function(a, b) {return a.concat(b);});
  CountByLabels += messages.length;
}

That gives me everything in the labels (I think) but not the other stuff.

I tried other things, to get the inbox (to combine with the above) or all of the emails

var messages = GmailApp.getMessagesForThreads(GmailApp.getInboxThreads()).reduce(function(a, b) {return a.concat(b);});
CountInbox += messages.length;

but I only get about 549 results (GMail shows 5,478). If I add in the results from getPriorityInboxThreads I get 1,829 results.

I tried

// get all messages, then join them into a single dimension array
var messages = GmailApp.getMessagesForThreads(GmailApp.search("(is:unread OR is:read) in:anywhere")).reduce(function(a, b) {return a.concat(b);});
CountByLabels += messages.length;

I get 598 results. I tried different search terms in the code directly above, eg:

is:unread = 528 results

is:read = 1,037 results

is:read OR is:unread = 599 results

None of them gave the right number, or even close, and incidentally if I try those search terms directly in gmail I get a totally different, and much higher, result for each - several thousand, or 'many'.

I don't think this is related to How to use Google App Scripts to retrieve Gmail emails in a customised way? as the numbers returned are not round numbers (eg 500).

I'm assuming that I can use getSpamThreads, getStarredThreads, getTrashThreads, getDraftMessages to get the relevant folders but until I understand why I'm only getting some emails from the inbox I don't trust those to give me everything.

Can anyone help?


回答1:


Try this:

function allEmailsInLabels() {
  var allLabels,i,j,L,L2,msgCount,theCount,threads,thisLabel;

  msgCount = 0;
  theCount = 0;

  allLabels = GmailApp.getUserLabels();
  L = allLabels.length;

  for (i = 0; i < L; i++) {
    Logger.log("label: " + allLabels[i].getName());
    thisLabel = allLabels[i];
    threads = thisLabel.getThreads();
    //Logger.log('threads: ' + threads);

    L2 = threads.length;

    for (j = 0; j < L2; j++) {
      msgCount = threads[i].getMessageCount();
      //Logger.log('thread message count: ' + threads[j].getMessageCount());
      theCount = theCount + msgCount;
    };
  };
  //Logger.log('theCount: ' + theCount);
};

It first gets all the labels, then the threads, then the message count in each thread, and keeps a running count. You'll also need to get the messages in the inbox, that code doesn't include them. This is the sample code from the documentation that shows the basic concept:

// Log the subject lines of your Inbox
var threads = GmailApp.getInboxThreads();
for (var i = 0; i < threads.length; i++) {
  Logger.log(threads[i].getFirstMessageSubject());
}



回答2:


This is not an answer to your problem (but is probably one of the reasons your total results returned don't agree with what you are seeing in gmail inbox) but highlights one of the problems I encountered when calling getPriorityInboxThreads() is that it ignores any thread that is not flagged as "important" in the primary inbox.

//returns 10 threads and 1st message for each thread
function getThreads(){
  var ret = '';
  var threads = GmailApp.getPriorityInboxThreads(0, 10);
  for (var i = 0 ; i < threads.length; i++) {
    var id = threads[i].getId();
    var message = GmailApp.getMessageById(id);
    ret += "subject: " + message.getSubject() +'\n';
    Logger.log("subject: " + message.getSubject());

    /*Edited this out as it doesn't return anything
    //check for labels  on this thread 
    var labels = threads[i].getLabels();
    for (var j = 0; j < labels.length; j++) {
      Logger.log(labels[j].getName());
    } */
  }

 return ret;
}

"Important" is classed as a system flag and getPriorityInboxThreads() ignores any thread that is not flagged important....

I would like to select all threads in "Primary" inbox irrespective of being labelled as "important".

To test, simply change any thread in inbox to important or not etc.



来源:https://stackoverflow.com/questions/30332670/how-to-read-all-emails-in-gmail-using-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!