Services

Start a project
Gmail and Drive

Save Gmail attachments to Google Drive automatically.

Invoices, receipts, signed forms and photos arrive by email, and someone has to download them and file them. This free script does it for you every hour: it saves the attachments from the emails you choose into one Drive folder and labels each email so nothing is saved twice.

What you’ll end up with

  • A Drive folder that fills itself with attachments from the emails you pick, such as everything from your supplier or anything with “invoice” in the subject.
  • File names that start with the email’s date, like 2026-11-08 Invoice-1042.pdf, so the folder sorts in order.
  • A Gmail label, Saved to Drive, on every email it has handled. You can see at a glance what was filed.

It runs in your own Google account, costs nothing and needs no add-on. You’ll spend about 10 minutes on the setup.

Step 1: Make the Drive folder and copy its ID

In Google Drive, create a folder, for example Email attachments, and open it. The address bar shows something like drive.google.com/drive/folders/1AbCdEf.... The long part after folders/ is the folder ID. Copy it.

Step 2: Decide which emails to save from

The script uses the same search you type into Gmail’s search box, so test it there first. A few examples:

You wantSearch to use
Everything from one supplierfrom:billing@supplier.com has:attachment
Anything that looks like an invoicesubject:invoice has:attachment
Only PDFshas:attachment filename:pdf
Emails you label yourselflabel:to-file has:attachment

Paste your search into Gmail and check the results are the emails you mean. The script adds -label:saved-to-drive on its own, so it skips emails it has already done.

Step 3: Add the script

Go to script.google.com, click New project, delete what’s there and paste this. Put your folder ID and search in the first two lines:

const FOLDER_ID = 'PASTE_YOUR_FOLDER_ID_HERE';
const SEARCH = 'subject:invoice has:attachment';
const LABEL_NAME = 'Saved to Drive';
const MAX_THREADS = 20; // emails per run, keeps each run short

function saveAttachmentsToDrive() {
  const folder = DriveApp.getFolderById(FOLDER_ID);
  const label = GmailApp.getUserLabelByName(LABEL_NAME) ||
                GmailApp.createLabel(LABEL_NAME);
  // Gmail search writes label names in lowercase with dashes
  const skip = ' -label:' + LABEL_NAME.toLowerCase().replace(/\s+/g, '-');
  const threads = GmailApp.search(SEARCH + skip, 0, MAX_THREADS);

  threads.forEach(function (thread) {
    thread.getMessages().forEach(function (message) {
      if (message.isInTrash()) return;
      const date = Utilities.formatDate(
        message.getDate(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
      const files = message.getAttachments({
        includeInlineImages: false, // skip logos in email signatures
        includeAttachments: true,
      });
      files.forEach(function (file) {
        const name = date + ' ' + file.getName();
        if (folder.getFilesByName(name).hasNext()) return; // already saved
        folder.createFile(file.copyBlob().setName(name));
      });
    });
    thread.addLabel(label);
  });
}

Click Save, then Run. Google asks you to authorize the script: choose your account and review the permissions. It needs to read Gmail and write to Drive. Google may say the app isn’t verified. That’s normal for a script you wrote yourself in your own account: click Advanced, then Go to your project and Allow.

Open the Drive folder. The attachments from the first 20 matching emails should be there, and those emails now carry the Saved to Drive label.

Step 4: Make it run every hour

  1. In the script editor, click the clock icon (Triggers) on the left, then Add Trigger.
  2. Choose the function saveAttachmentsToDrive, the event source Time-driven, the type Hour timer and Every hour.
  3. Click Save.

That’s it. New matching emails are filed within the hour, even when your computer is off, because the script runs on Google’s side. If you have a big backlog, it works through 20 emails an hour until it catches up. You can also click Run a few times to speed that up.

Optional: one subfolder per sender

To keep each supplier’s files apart, replace the line folder.createFile(...) with these lines. They make a subfolder named after the sender’s address the first time it’s needed:

        const sender = message.getFrom().replace(/.*<|>.*/g, '');
        const found = folder.getFoldersByName(sender);
        const target = found.hasNext() ? found.next() : folder.createFolder(sender);
        if (target.getFilesByName(name).hasNext()) return;
        target.createFile(file.copyBlob().setName(name));

Also delete the earlier “already saved” line, because the check now happens in the subfolder.

Common problems

What you seeLikely cause
“No item with the given ID could be found”The folder ID is wrong or has extra characters. Copy only the part after folders/, without ?usp=....
Nothing gets savedYour search finds no emails. Paste SEARCH into Gmail’s search box and check it there.
Logos and tiny images appear in the folderSome senders attach images as normal files. Add filename:pdf to the search, or skip small files with if (file.getSize() < 20000) return;.
An email was labeled but one file is missingA file with the same date and name was already in the folder, so it was skipped as a repeat. Rename the older one if both are real.
“Exceeded maximum execution time”A run hit Google’s 6-minute limit. Lower MAX_THREADS to 10.

What this doesn’t do

  • It doesn’t open or read the files, so it can’t pull invoice numbers or totals into a sheet. That’s a bigger job and depends on how the documents look.
  • Files sent as Google Drive links instead of attachments aren’t copied, since there’s no attachment to save.
  • Each run looks at whole conversations. If a new reply with a new attachment arrives in a conversation that’s already labeled, remove the label from that conversation and the next run picks it up.

Want it filed into a sheet as well?

I build Gmail, Drive and Sheets automations for small businesses: attachments filed by customer or month, a log sheet with a link to every file, and alerts when something important arrives. You get clean code in your own account and a short how-to.