1

Using Gmail API, I try to find specific e-mails by size. I want to be able to read the message size.

In the API site I cannot find such a method.

I've seen other methods with .js script, but I'm using google script as below:

function label_messages_without_response() {
  var emailAddress = Session.getEffectiveUser().getEmail();
  //Logger.log(emailAddress);

  threads = GmailApp.search("less");

  var size_total = 0;

  Logger.log(size_total);
  for (var i = 0; i < threads.length; i++)
  {
    var thread = threads[i];
    var lastMessage = thread.getMessages()[thread.getMessageCount()-1];
    //size_total+= thread.getMessages()[0].getSubject();   
    Logger.log(thread.getMessages()[0].getSubject());
   }

  Logger.log(size_total);
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
meleneemil
  • 21
  • 4

1 Answers1

1
  • You want to retrieve the total size of emails which are retrieved by GmailApp.search().
    • The unit of size you think is "bytes".
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this sample script? The size using as size:, larger: and smaller: of the search query is the body size. In this sample script, I used this.

Sample script:

function label_messages_without_response() {
  var threads = GmailApp.search("less");
  var size_total = 0;
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      var mailSize = messages[j].getBody().length; // Or  messages[j].getRawContent().length
      size_total += mailSize;
      Logger.log("Subject: %s, size: %s", messages[j].getSubject(), mailSize);
    }
  }
  Logger.log("Total size: %s", size_total);
}

Note:

  • If you want to retrieve whole size of each email, please replace var mailSize = messages[j].getBody().length to var mailSize = messages[j].getRawContent().length. By this, the size which includes the special characters in the body, the attachment files and headers can be retrieved.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • If I'm not mistaken, `getBody()` returns string and you're calculating length of that string(i.e., number of characters). So, this is based on `1 byte=1 character`. Right? – TheMaster May 29 '19 at 07:34
  • 1
    @TheMaster Thank you for your comment. Yes. If the body is only a text of ``sample``, the value is ``6`` bytes. In this case, I corresponded the value to ``size:`` using at the search query. For example, if you want to retrieve the value of whole size of an email, please replace ``var mailSize = messages[j].getBody().length`` to ``var mailSize = messages[j].getRawContent().length``. But, unfortunately, I'm not sure whether the size OP is thinking is them. – Tanaike May 29 '19 at 07:42
  • 1
    @TheMaster Thank you for replying. When the method of messages.get in Gmail API is used, the raw data which is the byte array can be retrieved. This value includes the special characters, the attachment files and so on. And, when I compare the sizes of the values retrieved by the method of messages.get in Gmail API and the getRawContent method of GmailApp, it was found that both size was the same. So if OP needs to whole size of each email, I would like to recommend to use ``getRawContent()``. About this, I updated my answer. – Tanaike May 29 '19 at 08:17
  • Second line: search("less"), shouldn't this be "smaller:1M" or something? – meleneemil May 31 '19 at 08:12
  • @meleneemil Thank you for replying. About your replying, I had thought that you want to retrieve the size (body or raw) of emails including the value of ``less`` from your question. When you set the query as ``smaller:1M``, the emails which has the body size less than 1 MByte can be retrieved. If you want the emails with the size of less than 1 MByte as the raw size, ``smaller:1M`` is not suitable. It is required to use the script. Can I ask you about ``the message size`` you want? Which do you want the body size or raw size of email? And is my understanding for your question correct? – Tanaike May 31 '19 at 09:27