Docento.app
Cloud storage concept with floating documents
All Posts

How to Automatically Save Gmail PDF Attachments to Google Drive

By The Docento.app TeamPublished 5 min read
Try Docento's free PDF editor — No sign-up, 100% private — sign, annotate, and stamp PDFs in your browser.Open the editor

If invoices, receipts, statements and signed contracts arrive in Gmail as PDF attachments, you probably spend a few minutes each week downloading them and dragging them into Google Drive. Gmail filters can label and forward mail, but they cannot save an attachment to Drive. A short Google Apps Script can, for free, on a schedule, using only your own Google account. This guide walks through the script, how to set it up, and the limits worth knowing.

What you will build

The script does four things each time it runs:

  1. Searches Gmail for messages with PDF attachments that have not been processed yet.
  2. Copies each PDF into a Google Drive folder you choose, with the email date added to the file name.
  3. Adds a Gmail label such as saved-to-drive to the thread so it is skipped next time.
  4. Runs again automatically every hour, or at whatever interval you pick.

Nothing passes through a third-party service. The script runs inside your Google account, which matters if the attachments are financial or personal documents.

Step 1: Create the destination folder

Create a folder in Google Drive, for example "Inbox PDFs". Open it and copy the folder ID from the address bar. It is the long string after /folders/ in the URL. You will paste it into the script.

Step 2: Add the script

Go to script.google.com, create a new project, delete the placeholder code, and paste this:

const FOLDER_ID = 'PASTE_YOUR_FOLDER_ID_HERE';
const LABEL_NAME = 'saved-to-drive';
const QUERY = 'has:attachment filename:pdf newer_than:30d -label:' + LABEL_NAME;

function savePdfAttachments() {
  const folder = DriveApp.getFolderById(FOLDER_ID);
  const label = GmailApp.getUserLabelByName(LABEL_NAME) || GmailApp.createLabel(LABEL_NAME);
  const threads = GmailApp.search(QUERY, 0, 50);

  threads.forEach(function (thread) {
    thread.getMessages().forEach(function (message) {
      const date = Utilities.formatDate(message.getDate(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
      message.getAttachments().forEach(function (attachment) {
        if (attachment.getContentType() === 'application/pdf') {
          const file = folder.createFile(attachment.copyBlob());
          file.setName(date + ' ' + attachment.getName());
        }
      });
    });
    thread.addLabel(label);
  });
}

Replace the folder ID, save, and click Run. The first run asks you to authorize access to Gmail and Drive. Because this is your own unverified script, Google shows a warning screen. Choose to continue only if you wrote or pasted the script yourself and understand what it does.

Step 3: Run it on a schedule

In the Apps Script editor, open Triggers (the clock icon), add a trigger for savePdfAttachments, choose a time-driven event, and set it to run every hour. From then on, new PDF attachments land in your Drive folder without you touching them.

Customizing the search

The QUERY line uses ordinary Gmail search operators, documented on Google's Refine searches in Gmail page, so you can test any change in the Gmail search box first.

  • Only one sender: add from:billing@example.com to save invoices from a single supplier.
  • Only certain subjects: add subject:invoice or subject:statement.
  • A longer backlog: change newer_than:30d to newer_than:1y for a one-time catch-up, then set it back.
  • Separate folders per supplier: copy the function, give each copy its own FOLDER_ID and query, and add a trigger for each.

The date prefix keeps files in chronological order when sorted by name. If you want a stricter scheme, our guide to naming and versioning shared files covers conventions that hold up over time.

Limits to know

Apps Script is free, but it has quotas. According to Google's Apps Script quotas page:

  • A single run can last at most 6 minutes.
  • Triggers have a total runtime of 90 minutes per day on a personal Google account and 6 hours per day on a Google Workspace account.
  • Google says quotas can change without notice.

That is why the script processes at most 50 threads per run. For a normal inbox, an hourly run finishes in seconds. If you are importing years of mail, run it manually a few times rather than raising the batch size.

Two other things to watch:

  • Duplicates: the label prevents the same thread from being processed twice. If you remove the label, the PDFs will be saved again.
  • Replies in a labeled thread: a new PDF in a thread that already has the label is skipped. If that happens often, a no-code tool that triggers on each new email may suit you better.

When a no-code tool is the better choice

If you would rather not maintain code, the same job can be done with Zapier, Make or n8n. Those tools are easier to extend, for example to rename files from the invoice number or post a message to a team channel, but free plans have task limits and your attachments pass through another service. Apps Script stays inside Google and costs nothing for typical volumes.

What to do with the PDFs once they are in Drive

Once the files are collecting in one folder, the rest of the workflow gets easier. Our guide to using PDFs with Google Drive covers search, sharing and previewing. For invoices specifically, see invoice management with PDF for filing and retention habits.

Takeaway

A short Apps Script and an hourly trigger are enough to move every PDF attachment from Gmail into a Drive folder automatically, with no extra subscription. When you need to do more with those files, such as merging a month of receipts, annotating a statement or redacting an account number before sharing, Docento.app handles it in your browser, with files staying on your device.

Try Docento's free PDF editor

No sign-up, 100% private — sign, annotate, and stamp PDFs in your browser.

Open the editor

Related Posts