How to read all emails in gmail using google apps script

16,072

Solution 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[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());
}

Solution 2

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.

Solution 3

I know this is coming a bit delayed, but having had the same problem and looking at some of the solutions offered here, I wanted to offer up my own solution, which also uses the search function:

function getEmails() {
  var allEmailThreads = GmailApp.search('label:all')
}

This actually filters for every email, regardless of the mailbox, and seems to me to be the simplest solution to the question.

Share:
16,072

Related videos on Youtube

James Carlyle-Clarke
Author by

James Carlyle-Clarke

Updated on September 03, 2022

Comments

  • James Carlyle-Clarke
    James Carlyle-Clarke over 1 year

    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?

  • Alan Wells
    Alan Wells almost 9 years
    If it's in the inbox, I think it doesn't have a label.
  • Alan Wells
    Alan Wells almost 9 years
    No, it won't get the inbox threads. I added something to the answer.
  • James Carlyle-Clarke
    James Carlyle-Clarke almost 9 years
    Hi Sandy, thanks for answering. There's some confusion here (my fault). TO BE CLEAR: Although the examples show counting the number of messages (the reality check I am using for the rest of the question), that is NOT what I am after - as stated, I am trying to READ them - I want to access various data about them one by one.
  • James Carlyle-Clarke
    James Carlyle-Clarke almost 9 years
    I tried to edit my original question but do not seem to be able to do so.
  • James Carlyle-Clarke
    James Carlyle-Clarke almost 9 years
    With regards your answer, I have it reading the threads (the first bit of code). The problem lies in reading everything else - I seem to come up short on the number of emails returned. That being said, your answer does make me wonder if I am reading the number of emails returned wrongly - I'm pretty sure I'm not (I'm basing on some other stackoverflow answers) but I will double check using your code and will come back to you IF that is the case.
  • James Carlyle-Clarke
    James Carlyle-Clarke almost 9 years
    To confirm, as far as I can tell it's not the method of counting... still looking for an answer.
  • Alan Wells
    Alan Wells almost 9 years
    Okay. Thanks for letting me know that it's not working. Maybe I should delete this answer so that people won't think it's answered. Just answer back so I know you got this msg.
  • James Carlyle-Clarke
    James Carlyle-Clarke almost 9 years
    Hi Sandy, nah, leave it there, it could help someone with a related question. Thanks! :)