GmailAttachment copyBlob() confusion

429 views
Skip to first unread message

Harry Hoffman

unread,
Jun 13, 2022, 7:39:12 AM6/13/22
to Google Apps Script Community
Hi Folks,
Hoping someone can have a look and tell me what I'm doing wrong. I'm trying to parse Gmail messages and when finding a gzip'd file use Utilities.ungzip to decompress and then parse the contents. I'm trying to get the attachment as a blob with this code
var attachment = files[k];
var attachmentBlob = attachment.copyBlob();

If I look at the console log output while running the code attachment and attachmentBlob both look to actually be GmailAttachment objects.

What am I doing wrong?

Code is below:
function getRelevantMessages()
{
  var threads = GmailApp.search("newer_than:1d",0,100);
  var messages=[];

  threads.forEach(function(thread)
  {
    messages.push(thread.getMessages()[0]);
  });
  return messages;
}


function parseMessageData(messages)
{
  var records=[];

  if(!messages)
  {
    console.log("No messages returned");
    return records;
   }
   for(var m=0;m < messages.length;m++)
   {
      var date = messages[m].getDate();
      var from = messages[m].getFrom();
      var subj = messages[m].getSubject();
      var recip = messages[m].getTo();
      var text = messages[m].getPlainBody();
      var files = messages[m].getAttachments();

       if (files)
      {
         //console.log("Files found");
        for (var k in files)
       {
          var attachment = files[k];
          var attachmentBlob = attachment.copyBlob();
          var file_name = attachment.getName();

          if ( file_name.match(/\.gz/) )
         {
            uncompressedBlob = Utilities.ungzip(attachmentBlob);
          }

      }
   }
  var origFrom = text.match(/From:\s+.*\<(.*)\>.*$\r\n/m)[1];
}


function doGet()
{
   var messages = getRelevantMessages();
   parseMessageData(messages);
}

Reply all
Reply to author
Forward
0 new messages