Save attachments to a google drive folder from each email id respectively

Viewed 131

I am trying to create a script that can save all attachments sent by an email id from time to time to a particular drive folder.

I am new to GAS and found this script however it doesn't seem to fit my requirement. Can anyone help, Please??

      function GmailToDrive() {
var query = '';
query = 'in:inbox from:noreply@agyo.io has:nouserlabels ';
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();
        var message_body = mesgs[j].getBody();
        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);
            file.setName(renameFile_(attachment, message_body))
            parentFolder.addFile(file);
            root.removeFile(file);
        }
    }
    threads[i].addLabel(label);
}

}

I am unable to understand the usage of getFolder_(folderName) and checkIfDefinedType_(attachment);

All I need is this: Create a new folder when a new email sends me attachments and save them to that folder...

1 Answers

Issue:

The code you provided is using several functions which are not defined (getGmailLabel_, getFolder_, checkIfDefinedType_, renameFile_). Their definitions should probably be wherever you first found this script.

Goal and solution:

In any case, based on the goal you mentioned, none of these non-defined functions is actually needed:

save all attachments sent by an email id from time to time to a particular drive folder

  • No need to get labels (getGmailLabel_), since you just want to retrieve attachments from messages between certain dates, and don't want to add any label to any message.
  • If you want to retrieve the threads between certain dates, you should use the search operators before: and after: (see Search operators you can use with Gmail).
  • You can retrieve your desired folder by doing DriveApp.getFolderById(id) (if you don't know how to get the folder id for a specific folder, this id is part of the folder URL, as shown here. If you want to look for name instead, just use DriveApp.getFoldersByName(name) and loop through it with while and next() to find your desired folder.
  • There's no reason to use getRootFolder, since you can create the file directly to your desired folder via Folder.createFile(blob).
  • Not sure why checkIfDefinedType_ is used, but that's not necessary to create the corresponding file in Drive.
  • By default the created file will have the same name as the attachment, but in any case you can customize it with file.setName(attachment.getName() + " WHATEVER CUSTOM STRING YOU WANT TO ADD");.

Code snippet:

function GmailToDrive() {
  const query = 'from:noreply@agyo.io before:2021-05-04 after:2021-05-01'; // Set sender and dates you prefer
  const threads = GmailApp.search(query);
  const folder = DriveApp.getFolderById("YOUR_FOLDER_ID");
  threads.forEach(thread => {
    const mesgs = thread.getMessages();
    mesgs.forEach(message => {
      const attachments = message.getAttachments();
      attachments.forEach(attachment => {
        const attachmentBlob = attachment.copyBlob();
        const file = folder.createFile(attachmentBlob);
        file.setName(attachment.getName() + " CREATED IN DRIVE");
      });
    });
  });
}
Related