Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read all emails in gmail using google apps script

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?

like image 765
James Carlyle-Clarke Avatar asked May 19 '15 17:05

James Carlyle-Clarke


2 Answers

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[j].getMessageCount();
      //Logger.log('thread message count: ' + threads[j].getMessageCount());
      // You could do something with threads[j] here like
      // threads[j].moveToTrash();
      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());
}
like image 133
Alan Wells Avatar answered Oct 22 '22 03:10

Alan Wells


I had the same question. Reading a little bit more in the reference in the Google Developers Website, I discovered, reading about the function moveToInbox, a Google sample that used the Search to get all e-mails that weren't in the Inbox (https://developers.google.com/apps-script/reference/gmail/gmail-thread#movetoinbox). I decided to combine this with the getInboxThreads and with these two, my code was shorter and found every e-mail that I had received (less spam and junk).

function getEmails() {
  var generalThreads, inboxThreads;

  inboxThreads = GmailApp.getInboxThreads();
  generalThreads = GmailApp.search('-in:inbox');
}

Every single email that was in the folder "All mail" in the Gmail was in these two variables after this.

I don't know if this can help anyone, but surely helped me.

like image 23
LucasCardoso910 Avatar answered Oct 22 '22 04:10

LucasCardoso910