Auto-Save Gmail Attachments to Google Drive (Free Script)
Quick answer: Gmail has no setting that saves attachments to Google Drive automatically. Google Apps Script does it for free: it finds emails with a label such as receipts, saves each attachment into a Drive folder such as tax-2026 with the date and sender domain in the file name, and runs every hour on a time-driven trigger. Setup takes about 10 minutes.
Saving one attachment to Drive by hand takes two clicks. Doing it for every invoice, receipt or CV that arrives is the part people want to stop doing. This tutorial gives you a working script and explains each step, so you can change it later.
What this tutorial covers:
- What the script does, and the five setup steps
- How it avoids duplicates and timeouts
- Which Apps Script limits matter
- How to customize it, and when a script is the wrong tool
Use the sections below as your playbook.
What the script does
Every time it runs, the script:
- Searches Gmail for
label:receipts has:attachment -label:saved-to-drive. - Saves each regular attachment into your Drive folder. Images embedded in the email body are skipped.
- Names each file
date_senderdomain_originalname, for example2026-03-14_acme.com_invoice-1043.pdf. - Skips a file if the folder already has one with that name.
- Adds the label
saved-to-driveto the thread so the next run ignores it.
It runs inside your Google account on Google’s servers. Your computer does not need to be on, and no third-party service sees the files.
Step 1: Label the emails you want saved
The script works on a Gmail label, so first make Gmail apply that label for you. Create a filter: search for the emails you want (for example from:billing@acme.com has:attachment), click Create filter, tick Apply the label, and choose or create receipts. Our Gmail filters guide has ready-made filter recipes, and the Gmail search builder helps you write the search.
Pick a label name without spaces. It keeps the search in the script simple.
Step 2: Create the Drive folder and copy its ID
Create the folder in Google Drive, for example tax-2026, and open it. The address bar shows something like:
https://drive.google.com/drive/folders/1AbCdEfGhIjKlMnOpQrStUvWxYz
The part after /folders/ is the folder ID. Copy it. The script uses the ID instead of the name, so it keeps working if you rename or move the folder.
Step 3: Paste the script
Go to script.google.com, click New project, delete the sample code, and paste this:
// Settings: change these three values.
const LABEL_NAME = 'receipts'; // Gmail label to watch
const FOLDER_ID = 'PASTE_FOLDER_ID_HERE'; // Drive folder ID, from the folder's URL
const DONE_LABEL_NAME = 'saved-to-drive'; // added to each thread after it is saved
const MAX_RUN_MS = 4.5 * 60 * 1000; // stop early, Apps Script ends a run at 6 minutes
function saveLabeledAttachments() {
const startedAt = Date.now();
const folder = DriveApp.getFolderById(FOLDER_ID);
const doneLabel = GmailApp.getUserLabelByName(DONE_LABEL_NAME) ||
GmailApp.createLabel(DONE_LABEL_NAME);
const query = 'label:' + LABEL_NAME + ' has:attachment -label:' + DONE_LABEL_NAME;
const threads = GmailApp.search(query, 0, 50);
for (const thread of threads) {
if (Date.now() - startedAt > MAX_RUN_MS) {
break; // the next run continues where this one stopped
}
for (const message of thread.getMessages()) {
const attachments = message.getAttachments({ includeInlineImages: false });
for (const attachment of attachments) {
const name = buildFileName(message, attachment.getName());
if (folder.getFilesByName(name).hasNext()) {
continue; // already saved by an earlier run
}
folder.createFile(attachment.copyBlob().setName(name));
}
}
thread.addLabel(doneLabel);
}
}
// 2026-03-14_acme.com_invoice-1043.pdf
function buildFileName(message, originalName) {
const date = Utilities.formatDate(
message.getDate(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
const match = message.getFrom().match(/@([^>\s]+)/);
const domain = match ? match[1].toLowerCase() : 'unknown-sender';
return date + '_' + domain + '_' + originalName;
}
Change the three values at the top: your label, your folder ID, and the name of the “done” label. Save the project.
Step 4: Run it once and allow access
In the toolbar, select saveLabeledAttachments and click Run. Google asks you to authorize the script to read Gmail and write to Drive. The script uses your own account only.
Because the script is yours and not reviewed by Google, Google may show an unverified app screen before the consent screen. Continue only for a script you pasted yourself and have read.
When the run finishes, open your Drive folder. The attachments from your labeled emails should be there, and the threads in Gmail now carry the saved-to-drive label.
Step 5: Make it run automatically
Add a time-driven trigger, which Google describes in its triggers guide:
- In the script editor, click Triggers (the alarm clock icon) on the left.
- Click Add Trigger.
- Choose the function
saveLabeledAttachments, the event source Time-driven, and an Hour timer set to every hour. - Click Save.
You can also create the trigger in code by running this once:
function createHourlyTrigger() {
ScriptApp.newTrigger('saveLabeledAttachments').timeBased().everyHours(1).create();
}
Google notes that trigger times can shift slightly, for example a 9 AM trigger runs between 9 and 10 AM, but the interval stays steady. Minute timers are possible too (every 1, 5, 10, 15 or 30 minutes), but hourly is enough for receipts and uses less of your daily quota.
How does the script avoid duplicates and timeouts?
Duplicates. Two checks work together. The saved-to-drive label removes finished threads from the search. If a run stops halfway through a thread, the name check skips the files that were already saved, so the next run only adds the missing ones.
Same-name files. Suppliers often send every invoice as invoice.pdf. The date and sender-domain prefix keeps those apart. One case stays: two attachments with the same name in the same email from the same day. The script saves the first and skips the second. If that matters for you, add the time to the date format (yyyy-MM-dd_HHmm).
Timeouts. Google stops any run after 6 minutes. The script checks the clock after each thread and stops at 4.5 minutes, so it never gets cut off mid-file. The next run picks up the remaining threads.
Replies in a saved thread. The label sits on the whole thread. If someone replies later with a new attachment, the thread is already marked and the script skips it. To catch it, remove the saved-to-drive label from that thread. The next run saves only the new file, because the old ones already exist in the folder.
Apps Script limits that affect this script
Google lists these limits on its quotas page. Quotas are per user and reset 24 hours after the first request.
| Limit | Gmail.com account | Google Workspace account |
|---|---|---|
| One run of a script | 6 min | 6 min |
| Total trigger run time per day | 90 min | 6 hr |
| Gmail reads and writes per day | 20,000 | 50,000 |
| Triggers per user per script | 20 | 20 |
An hourly run that finds a few new receipts takes seconds, far inside these limits. The limit you can hit is a big backfill. Run backfills by hand rather than on the trigger: the 90-minute daily cap is for triggers.
Customize it: only PDFs, one sender, monthly folders
Only PDFs. Add this line as the first line inside the for (const attachment ...) loop:
if (!/\.pdf$/i.test(attachment.getName())) continue;
One sender or domain. Narrow the search instead of the code. Change the query line to include from::
const query = 'label:' + LABEL_NAME + ' from:acme.com has:attachment -label:' + DONE_LABEL_NAME;
Monthly subfolders. Replace folder.createFile(...) with a call that picks a subfolder named after the email’s month:
const month = Utilities.formatDate(message.getDate(), Session.getScriptTimeZone(), 'yyyy-MM');
const sub = folder.getFoldersByName(month).hasNext()
? folder.getFoldersByName(month).next()
: folder.createFolder(month);
sub.createFile(attachment.copyBlob().setName(name));
If you use monthly folders, move the duplicate check to the subfolder too (sub.getFilesByName(name)).
When a script is the wrong tool
A script is best for new mail that keeps arriving. It is a poor fit in two cases:
- You don’t want to maintain code. Automation services such as Zapier do the same job without code, with their own pricing and with files passing through their servers. Our Gmail to Google Drive guide compares the options.
- You need the attachments already in your mailbox. Years of old emails mean many runs and many 6-minute windows. Save Bulk Gmail Attachments is built for that backlog: select the emails in Gmail and it saves every attachment to Google Drive or a ZIP in one run. The free plan saves 7 attachments a day; see pricing for unlimited. Many people use both: the extension once for the backlog, the script for everything after. The bulk download guide shows the one-time run step by step.
Frequently Asked Questions
Can Gmail automatically save attachments to Google Drive?
Not with a built-in setting. Gmail can save one attachment to Drive by hand. To do it automatically, use a Google Apps Script with a time-driven trigger, or an automation service such as Zapier.
Is the Apps Script method free?
Yes. Apps Script is free with a Google account. It runs within daily quotas: for gmail.com accounts, triggers can run 90 minutes a day in total and one run stops after 6 minutes.
How often does the script run?
As often as the trigger says. An hourly timer is a good default. Google may shift the exact run time slightly, but keeps the interval.
Will the script save the same file twice?
No. It marks each saved thread with a label and skips a file if one with the same name is already in the folder. Names start with the email date and sender domain, so files from different emails rarely collide.
Can it save attachments from emails I already have?
Yes. Label the old emails and run the script by hand a few times. Each run handles up to 50 threads and stops before the 6-minute limit. For thousands of old emails, a bulk tool is faster.
Does the script send my files anywhere?
No. It runs on Google's servers under your own account and copies files from Gmail to your Drive. Nothing goes to a third party.