If you regularly receive invoices, reports, or project files via email, manually downloading each attachment and uploading it to the correct folder in Google Drive is a tedious daily chore. Over time, it is easy to forget a file, leading to disorganised records and frantic searches through your inbox.
While many third-party services like Zapier or IFTTT can automate this process, they often require paid subscriptions and force you to grant a third-party company full access to your private emails. Instead, you can use a simple, secure Google Apps Script to automatically scan your inbox and save specific attachments directly into a Google Drive folder of your choosing, keeping your data entirely within the Google ecosystem.
Creating the Google Apps Script
Google Apps Script is a free, built-in automation tool that allows different Google Workspace apps to talk to each other. We will use a standard script that searches for a specific Gmail label and extracts the attachments.
- Open your web browser and navigate to script.google.com. Ensure you are signed into your main Google Account.
- Click the New project button in the top left corner.
- Delete any default code in the editor window.
- Paste the following standard automation script:
function saveAttachments() {
var folder = DriveApp.getFoldersByName("Saved Attachments").next();
var threads = GmailApp.search('label:SaveToDrive has:attachment');
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var attachments = messages[j].getAttachments();
for (var k = 0; k < attachments.length; k++) {
folder.createFile(attachments[k]);
}
}
threads[i].removeLabel(GmailApp.getUserLabelByName('SaveToDrive'));
}
} - Click the Save icon (the floppy disk) at the top of the editor and name the project “Attachment Saver”.
Configuring Gmail and Drive
For the script to work, you must create the specific folder and label that the code is looking for.
- Open Google Drive and create a new folder exactly named
Saved Attachments. - Open Gmail and create a new label exactly named
SaveToDrive. - In Gmail, create a Filter. Set the criteria to match the emails you want to save (e.g., all emails from “[email protected]” with an attachment). Set the filter action to automatically apply the
SaveToDrivelabel.
Automating the Script
Finally, you need to tell the script to run automatically in the background, scanning your inbox for that label.
- Return to your Google Apps Script tab.
- Click on the Triggers icon on the left sidebar (it looks like a small alarm clock).
- Click + Add Trigger in the bottom right corner.
- Set the event source to Time-driven.
- Choose how often you want the script to check your inbox (for example, Hour timer and Every hour).
- Click Save. Google will ask you to authorise the script to access your Gmail and Drive. Follow the prompts to grant permission.
Now, whenever an email arrives that matches your Gmail filter, it will be labelled. Every hour, the script will find that label, extract the attachment, save it to your Drive folder, and remove the label so the email isn’t processed twice.