How to Save Files in Gmail to a Google Drive Folder

 How to Save Files in Gmail to a Google Drive Folder

Many users have files tucked away in Gmail that they wish they could organize and push to Google Drive, giving their emails a better way to access their Google Drive files. This is a “dummies” step-by-step guide that should be able to assist any user. Yes, this is a dummies guide written in plain English with no technical jargon! Screenshots are used to better illustrate key steps in the guide. Despite the fact that it contains some script code, I guarantee you will only need to replace two things:

  1. Your own email/gmail details
  2. Your Google Drive Folder 

This solution works for both personal Gmail Accounts as well as Google Workspace Accounts. So here are the Dummies guide steps to make this possible:


Step 1: Create a Gmail Label from where you want to fetch your Gmail attachments

In Gmail, create a filter+ label (a search criteria for messages + attachments) you want to save attachments from to drive. 

  • In my case, I wanted to save all my Credit Card Statements from my bank to a Google Drive folder. So I created a filter using the senders email [email protected] and added “only those that have attachments” as I only want to fetch attachments. 
  • Get the name of this Label. You can create more complicated filters and labels: for details on how to create filters and labels, refer to this detailed manual here
Search/Filter CriteriaLabel Created

Step 2: Create a Google Drive Folder from where you want to save the Gmail attachments

  • In Google Drive, Go to Create–>New–>Folder–> {{folder name}}; For my case I named it “CardCentreDTB” even though the name of my label is different.
  • Get the Folder ID by clicking on the link and copy the Folder ID from the URL path. The part below;
The folder ID is the most important part. Save it

Step 3: Create a Google App Script {This is the Part I tell you: DON’T PANIC}

LET ME REPEAT; DO NOT PANIC IF YOU ARE NOT A DEVELOPER/CODER; NOTHING IS GOING TO BITE OR BREAK.

Of course if you are a coder/developer, I am surprised you are reading this, but pleased at the same time 🙂

  • So Open a new Browser tab and go to https://script.google.com/ 
  • It will show some projects or it might not; Create New Project; just like you do with a new Google Doc in Google Drive Doc as shown below;
  • In the open IDE ( read Google Doc editor for script) delete everything you get there: Select All and delete; See before and after below
Before Select+ DeletingAfter Selecting + deleting

NOW COPY AND Paste this script in the empty space and you should have it looking like this:

//start of script
//Source:https://stackoverflow.com/questions/60551975/copying-gmail-attachments-to-google-drive-using-apps-script
function saveAttachmentInFolder(){
 var folder = DriveApp.getFolderById('<myfodlerid19xxxxv>');// your Google Drive Folder ID to save the files
 var userId = "<[email protected]>";// your email.I mean your own Gmail to
 var query = "label:mylabelingmail";// the label you created to fetch attachments from
 var res = Gmail.Users.Messages.list(userId, {q: query});//The searching and using the results; never mind
 res.messages.forEach(function(m){
   var attA=GmailApp.getMessageById(m.id).getAttachments();
   attA.forEach(function(a){
     var ts=Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "yyMMddHHmmss");
     folder.createFile(a.copyBlob()).setName(a.getName()+ts);
   });
 });
}
// end of script
//No Copyright nor IP is claimed on this script
//You can check and adpt the complex and dynamic one here:https://github.com/ahochsteger/gmail2gdrive

Now let us change and input the parts inside the <text in red> in RED above/below as shown;

From thisTo this
var folder = DriveApp.getFolderById(‘<myfodlerid19xxxxv>‘)var folder = DriveApp.getFolderById(‘19wA123Hbanv65ybna‘)
var userId = “<[email protected]>var userId = “[email protected]
var query = “<label:mylabelingmail>var query = “label:[email protected]
  • This is to make it do the work for you; as in fetch your attachments from your label and save them in your folder! That is why we are here, right?
  • You should be able to give it a name, and save it when done to have something like below;

Step 4: RUN your Google App Script { MAKE IT FETCH FOLDERS}

You are 1 minute way from fetching your Attachments; infact, a few clicks away;  I promise;

  • Click 1; Click to Run the Script/ a.k.a make it work; 
  • Click this small button  labeled “Run”as shown below here;
  • After you click RUN, you will get a pop up that asks you to authorize and grant permissions to the script to perform its activities.
  • Now you need to click Review Permissions. That is, grant this script permission to do what you have instructed it to do; move files from Gmail to Drive Folder. To allow/authorize, you must go through a set of click-through steps. Here are the steps:

ActionDemonstration
Review PermissionsInitiate the process to use your Google Account to perform these actions
Security Check: Oops; That does not look good. Don’t worry; Google needs you to be sure you want to grant this script, which you have created the rights to your email and drive data. 
Click ADVANCED and then go to{script name} to proceed you can revoke this later.
Authorize using your Account
Grant Access to Gmail and Drive; To fetch and save data obviously

Step 5:  ENABLE THE GMAIL API

Your second last step. You need to enable the Gmail API. 

  • Click plus sign (+) on the Services on the Right Side
  • Scroll and Find Gmail and Enable as shown below;

Step6: RUN your Google App Script AGAIN; EUREKA!!

This time, you script will run and you should start to see files stream into your indicated Google Drive Folder.

CONGRATULATIONS. YOU ARE NOT A DUMMY ANYMORE:-)

If you need any help or such simple solutions, you know where and how to find us:


Other Popular Posts