Guide · Gmail
How to automatically save Gmail attachments to Google Drive
Gmail has no setting for it, but two free things together do the job: a Gmail filter that puts a label on the emails, and a short Apps Script that copies the attachments from that label to a Google Drive folder every hour. It takes about 5 minutes to set up, and after that invoices and receipts land in Drive without you touching them.
Updated 20 November 2026 · 5 min read
Set it up in five steps
For one email, you do not need any of this: hover over the attachment in Gmail and click Add to Drive. The steps below are for when you want every new invoice or receipt saved by itself.
- 1
Put a label on the emails
In Gmail, search for the emails you want, for example from:[email protected] has:attachment. Click the filter icon in the search bar, then Create filter, tick Apply the label, choose or create a label such as Invoices, and save. New emails now get the label as they arrive.
- 2
Make a Drive folder and copy its ID
Create the folder in Google Drive and open it. The ID is the last part of the address in your browser, after /folders/.
- 3
Paste the script
Go to script.google.com, click New project, delete what is there and paste the script below. Put your label and folder ID in the first lines.
- 4
Run it once
Click Run and allow the access Google asks for. The attachments from your labelled emails appear in the Drive folder, named with the date first.
- 5
Make it run every hour
Click the clock icon (Triggers) on the left, then Add Trigger. Choose saveAttachments, Time-driven, Hour timer, Every hour, and save. From now on it runs by itself, even when Gmail is closed.
The script
Paste it into a new project on script.google.com and change the first three lines.
const LABEL = 'Invoices'; // the Gmail label to watch
const SAVED_LABEL = 'Invoices/Saved'; // created for you, marks what is done
const FOLDER_ID = 'paste-your-drive-folder-id-here';
const ONLY_PDF = true; // false = save every type of file
function saveAttachments() {
const folder = DriveApp.getFolderById(FOLDER_ID);
const saved = GmailApp.getUserLabelByName(SAVED_LABEL) || GmailApp.createLabel(SAVED_LABEL);
const q = (name) => 'label:' + name.replace(/[\/ ]/g, '-');
const threads = GmailApp.search(q(LABEL) + ' has:attachment -' + q(SAVED_LABEL), 0, 50);
threads.forEach(function (thread) {
thread.getMessages().forEach(function (msg) {
const day = Utilities.formatDate(msg.getDate(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
msg.getAttachments({ includeInlineImages: false }).forEach(function (att) {
if (ONLY_PDF && att.getContentType() !== 'application/pdf') return;
folder.createFile(att.copyBlob()).setName(day + ' ' + att.getName());
});
});
thread.addLabel(saved); // so the same email is never saved twice
});
}Each file is named with the date the email arrived, such as 2026-11-20 invoice-4411.pdf, so the folder sorts by date. The script handles up to 50 emails per run, and the next run picks up the rest.
What the script does not do
- It saves the files, not the email. The text of the email, who sent it and when, is not in the folder.
- One folder for everything. Sorting attachments into a folder per client or supplier means one copy of the script per label.
- Replies are skipped. Once an email thread is marked as saved, a new reply with a new attachment in the same thread is not picked up.
- Broad access. The script asks to read your Gmail and to see and change your Google Drive files.
Save the email itself as a PDF: MailFile
If you keep records for an accountant, a client or a case file, you often need the email as well as the attachment. MailFile is our Gmail add-on for that. Open an email, click MailFile in the side panel, and it saves the email as a PDF with From, To and Date at the top, and puts the attachments in the same Google Drive folder. It remembers the folder for each sender, so the next email from that client goes to the right place.
25 emails a month are free, with no card. Pro is €6 a month with no limit, and you can try it free for 14 days.
Questions
Can Gmail save attachments to Google Drive automatically?
Not with a built-in setting. You can save one attachment by hovering over it and clicking Add to Drive, but nothing in Gmail does it for every new email. A Gmail filter plus a short Apps Script with an hourly trigger does it for free, as shown on this page.
How do I save attachments from one sender or one label only?
Use a Gmail filter to put a label on those emails, for example everything from your supplier's invoice address. The script only looks at emails with that label, so nothing else ends up in the folder.
Will the same attachment be saved twice?
No. After the script has saved the attachments from an email, it adds a second label (Invoices/Saved in the example) and skips those emails on the next run.
Can it save every type of file, not just PDFs?
Yes. Set ONLY_PDF to false in the script, and it saves Word files, spreadsheets, images and everything else attached. Pictures that sit inside the email text, such as logos, are skipped.
Can I save the email itself as a PDF, not just the attachments?
Not well with this script, because it only copies the attached files. MailFile, a Gmail add-on, saves the open email as a PDF with From, To and Date at the top, and puts its attachments in the same Drive folder. It is free for 25 emails a month.
Does this work with a free Gmail account?
Yes. Apps Script and time triggers work on free Gmail accounts and on Google Workspace accounts. Google limits how long scripts can run each day, and a few dozen emails an hour are far below that.
Written by AddonNordic ApS, Aalborg, Denmark (CVR 46495985), maker of MailFile for Gmail. Also see how to send a Google Sheet as a PDF. Gmail™, Google Drive™ and Google Apps Script™ are trademarks of Google LLC; this guide is not made or endorsed by Google.