0

I need saving the attachments im mails from one sender with file format 'gz' to folder GmailToDrive in Google-drive with replacement old file

Here Apps script saving attachments from last mail I get answer with script, but something is wrong. I think, this - query = 'in:inbox has:nouserlabels ' + query + ' AND from:entrepot@fmlogistic.fr AND older_than:4h';

Can you help me? thank you very much!

// GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['gz'];
//Name of the folder in google drive i which files will be put
var folderName = 'GmailToDrive';
//Name of the label which will be applied after processing the mail message
var labelName = 'GmailToDrive';

function GmailToDrive(){
  var query = '';
  //filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+formattedDate+
  for(var i in fileTypesToExtract){
    query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
  }

  //ADD the only email adress you want to check + EDIT : only last 24h emails
  query = 'in:inbox has:nouserlabels ' + query + ' AND from:entrepot@fmlogistic.fr AND older_than:4h';

  var threads = GmailApp.search(query);
  var label = getGmailLabel_(labelName);
  var parentFolder;
  if(threads.length > 0){
    parentFolder = getFolder_(folderName);
  }
  var root = DriveApp.getRootFolder();
  for(var i in threads){
    var mesgs = threads[i].getMessages();
    for(var j in mesgs){

      //ADD: check if the mail is unread:
      if (mesgs[j].isUnread()) {

        var attachments = mesgs[j].getAttachments();
        for(var k in attachments){
          var attachment = attachments[k];
          var isDefinedType = checkIfDefinedType_(attachment);
          if(!isDefinedType) continue;
            var attachmentBlob = attachment.copyBlob();
            var file = DriveApp.createFile(attachmentBlob);
            parentFolder.addFile(file);
            root.removeFile(file);
          }
        }
      } // close if unread
    } // close for msg loop

    //ADD: Set thread (all mail included) as read
    GmailApp.markThreadRead(threads[i]);

    threads[i].addLabel(label);
}

1 Answers1

0

As per documentation:

older_than can be used for messages older than a time period using d (day), m (month), and y (year)

So, 4h won't work.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33