Save Gmail invoice attachments to a Drive folder automatically
A Google Apps Script that runs every hour, searches Gmail for new emails with attachments (such as PDF invoices), saves them to a Drive folder and labels the email so it isn't saved twice.
The problem
Supplier invoices, account statements and price quotes reach you as email attachments, mixed in with dozens of other messages. When your accountant or a tax return needs them, you search by hand and download them one by one. What you want is a Drive folder where the important attachments collect on their own.
The tool
Google Apps Script, a free scripting environment tied to a Google account. According to the official docs, the GmailApp service lets you search your mail using the same syntax as Gmail search and returns the matching threads, you can read each message's attachments, and then store each attachment as a file in a folder through the DriveApp service. The script can run by itself with a time-driven trigger that repeats anywhere from every minute to once a month.
The steps
- In Google Drive create a folder with a unique name, such as "Supplier invoices". The code below looks the folder up by name, so don't create two folders with the same name.
- In Gmail create a new Label named "saved". We will put it on every message that has been processed so it isn't saved twice.
- Open script.google.com and create a New project. If you prefer, open a new Google Sheets spreadsheet and choose Extensions then Apps Script.
- Delete the existing code and paste the following code, replacing the folder name with yours:
function saveAttachments() {
const folder = DriveApp.getFoldersByName('Supplier invoices').next();
const label = GmailApp.getUserLabelByName('saved');
const threads = GmailApp.search('has:attachment filename:pdf newer_than:2d -label:saved', 0, 20);
threads.forEach(t => {
t.getMessages().forEach(m => m.getAttachments().forEach(a => folder.createFile(a)));
t.addLabel(label);
});
}
- Click Save, then choose the function saveAttachments from the menu and click Run. Google will ask for permission to access Gmail and Drive. Read the permissions, then approve them.
- Check the folder: PDF files from the messages you received in the last two days should appear. Also check in Gmail that those messages now carry the saved label.
- From the left menu click Triggers then Add Trigger. Choose the function saveAttachments, the event type Time-driven, repeat every hour, then Save.
- To change the file type, edit pdf in the search expression, for example filename:xlsx. To limit it to one supplier, add from: followed by their address. The search syntax here is the same one you type in the Gmail search bar, and it is listed on Gmail's official search operators page.
- After a week, compare the folder with your inbox to make sure nothing important was missed.
The real cost
Nothing. The limits on free accounts, according to the official quotas page, are 90 minutes a day of total trigger runtime (6 hours on Workspace) and 6 minutes per execution. The code processes 20 threads per run, which is enough for a small inbox. Storage counts against your account's Drive space. Check the numbers on the official page, as they may change.
The free alternative
The simplest alternative is to download attachments by hand once a week after searching has:attachment in Gmail. If you don't want to write code, you could look for a visual automation tool, but check the limits of its free plan on its official pricing page.
Notes
- We have not tried these steps ourselves yet; they are based on the current official docs. Button names may change, so if your interface differs, follow the docs.
- The code labels the whole thread. If a new reply with an attachment arrives inside a labeled thread, it will be skipped, because the search excludes the label.
- The permissions are broad because the script reads your mail. Don't share the project with anyone, and don't copy scripts from sources you don't trust.
- The name in the code must match the folder name exactly, or an error will appear when it runs.
Sources
Did you like this guide?
The weekly newsletter is being prepared. Leave your email and we'll send it when it launches.
We use your email only to send the newsletter. Privacy policy
