2 Ways To Save Gmail Attachments To Google Drive Automatically

0


You can save all Gmail attachments to Google Drive automatically to save them in a more appropriate file-managing interface and easily access them later.

Managing attachments in Gmail isn’t an easy task, you have to specifically click on an attachment to view and download it. It isn’t uncommon to miss an important file just because you didn’t notice the attachment icon.

Furthermore, the Gmail interface isn’t the best place to view all the attachments in one place and attachments are prone to accidentally getting deleted if you bulk delete emails or set up automatic deletion.

There is no built-in method to do that, so we’ll have to do some tinkering with scripts and third-party apps.

Below I am listing 2 ways to automatically save email attachments in Gmail to Google Drive.

Method #1: Use Google Apps Script

The first method is to use a Google Apps Script that will automatically save all new email attachments to a dedicated folder in Google Drive. Since we will be using our own script, it’s a good method for those who don’t want to depend on third-party apps.

I have created a script that automatically solves common problems that you may face while trying to automatically save Gmail attachments to Google Drive. Here’s what it exactly does:

  1. Check if there is a new email with an attachment in Gmail.
  2. Check if there is a folder in Google Drive where the attachment could be saved. If not, it will automatically create a new “Email Attachments” folder to save all future attachments.
  3. Check if the new attachment isn’t a duplicate of an already saved attachment. For reliability, I have configured it to check the MD5 hash value of files instead of names to ensure two different files with the same names aren’t skipped.
  4. Save the attachment to the Google Drive folder.

Creating Google Apps Script

Before we create the script, you need to know that Google services have limited quotas depending on whether you are a free user or have paid for a Google Workspace plan. Even as a free user, you should be able to run this script easily, but if you have multiple scripts they might reach the daily quota.

To create a script, open Google Apps Script and click on the New Project button at the top-left corner to start creating.

Give a new name to the project to easily identify it later and then remove all the existing code and copy/paste the below-mentioned code here (copy both sections in one go). Once copied, click on the Save button to save it.

function onNewEmail(e) {
  var threads = GmailApp.getInboxThreads(0, 1);
  var messages = threads[0].getMessages();
  
  var folderName = "Email Attachments";
  var folderIterator = DriveApp.getFoldersByName(folderName);
  var folder;

  if (folderIterator.hasNext()) {
    folder = folderIterator.next();
  } else {
    folder = DriveApp.createFolder(folderName);
  }
  
  for (var i = 0; i < messages.length; i++) {
    var message = messages[i];
    
    if (message.getAttachments().length > 0) {
      var attachments = message.getAttachments();
      
      for (var j = 0; j < attachments.length; j++) {
        var attachment = attachments[j];
        var attachmentHash = Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, attachment.getBytes()));
        var existingFiles = folder.getFiles();
        var isDuplicate = false;

        while (existingFiles.hasNext()) {
          var existingFile = existingFiles.next();
          var existingFileHash = Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, existingFile.getBlob().getBytes()));

          if (attachmentHash === existingFileHash) {
            isDuplicate = true;
            break;
          }
        }

        if (!isDuplicate) {
          folder.createFile(attachment);
        }
      }
    }
  }
}

That’s it, the script is created, but you need to create a trigger to automatically check for new emails with attachments.

Create Trigger

To create a trigger, click on Triggers in the left menu and then click on the Add Trigger button.

create-trigger

Here Google Workspace paid plan users can select From Gmail in the Event source section and On messages received in the Event type section.

Free users will have to use a time-based trigger instead. Therefore, select Time-driven in the Event source section and then select your preferred time interval. I will recommend you use Every hour to not go over the quota, but you can select Every minute too if saving faster matters to you.

set-up-trigger

After selection, click on Save. Google will ask you to authenticate this script for your email and also warn you that it isn’t verified. The warning is there as you have just created this script yourself and Google hasn’t reviewed it. Just click on Advanced and then click on Go to (project name).

authenticate-script

Once you allow the trigger, it will run according to your selected trigger settings automatically. You can always delete the trigger or script if you ever want to stop saving attachments to Google Drive automatically.

Method #2: Use a Third-Party App

If you don’t want to create your own script, you can also use a third-party app that could do it for you. Although there are a bunch of apps for this purpose, most of them are either paid or have limited functionality for free. I personally found Microsoft Power Automate the most reliable for this as it’s easy to set up and has no limitations.

There is a Save Gmail attachments to your Google Drive flow available for Microsoft Power Automate that will automate this process. Just open it and log in with your Microsoft account (or create a new account).

Once logged in, click on Sign in next to both Gmail and Google Drive to give Microsoft the permissions to manage these apps. After granting the permissions, click on Create Flow.

create-flow-save-attachment-google-drive

That’s it, the flow will be created and you can see its details on the next page along with the history of executed tasks.

microsoft-power-automate-save-gmail-attachment-automatically

To Conclude

Using the custom Google Apps Script will definitely be more reliable, especially if you know how to edit it to customize it to your needs. Although, Microsoft Power Automate should be the go-to solution when you are looking for ease. If you have any specific needs that the above script doesn’t fulfill, let me know in the comments and I’ll try to update it if possible.

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Source
Las Vegas News Magazine

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More