-2

I need some help with saving attachments from last mails. Every morning and every evening I get automatic mailing on email.

I found one script in web but it need some edits. Mail comes at 9AM and 4PM, so script must take only todays last letter and save the attachment in folder "GmailToDrive" with replacing old file.

And I need to save mail only from special sender(one mail). Other senders are out of script.

Can you help me, what I need to change or edit here? Thank you a lot, friends!)

/*
 * Note:
 * If you need any paid assistant, please write to support.waqar@gmail.com
 * We provide Apps Script Development services at very reasonable price.
 */

// 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(){
  //build query to search emails
  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]));
  }
  query = 'in:inbox has:nouserlabels ' + query;
  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){
      //get attachments
      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);
      }
 }
 threads[i].addLabel(label);
  }
}

//This function will get the parent folder in Google drive
function getFolder_(folderName){
  var folder;
  var fi = DriveApp.getFoldersByName(folderName);
  if(fi.hasNext()){
    folder = fi.next();
  }
  else{
    folder = DriveApp.createFolder(folderName);
  }
  return folder;
}

//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
  n = parseInt(n);
  var date = new Date();
  date.setDate(date.getDate() - n);
  return Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy/MM/dd');
}

function getGmailLabel_(name){
  var label = GmailApp.getUserLabelByName(name);
  if(!label){
 label = GmailApp.createLabel(name);
  }
  return label;
}

//this function will check for filextension type.
// and return boolean
function checkIfDefinedType_(attachment){
  var fileName = attachment.getName();
  var temp = fileName.split('.');
  var fileExtension = temp[temp.length-1].toLowerCase();
  if(fileTypesToExtract.indexOf(fileExtension) !== -1) return true;
  else return false;
}

I understand, that I can finally after edits put it in trigger on time after 9AM and 4PM)))

  • What particular issue do you encounter with your code? Did you already test it? – Waxim Corp May 05 '21 at 16:06
  • This should help with the replacement aspect of your question: https://stackoverflow.com/questions/19926604/google-apps-scripts-how-to-replace-a-file – Cooper May 05 '21 at 16:29
  • @Waxim Corp the problem is this script checks all the mails in inbox for all the time and saves all attachments in folder GmailToDrive, but I need to check only last mail as it may be endless cycle – Dmitriy Rudakov May 06 '21 at 02:34
  • @ MetaMan thank you, I will check this when won first part of my task – Dmitriy Rudakov May 06 '21 at 02:36

1 Answers1

0

Below your script with 3 lines edited.

  1. Add the email you want to check in the query [EDIT] and only mail received in the last 24h
  2. Add if statment to only check unread mail (if you read it manually, put it back to unread for the script to work)
  3. Set the thread as read when all mail are checked
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:sender@sender AND newer_than:1d';

  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);
  }
}
Waxim Corp
  • 657
  • 4
  • 16