Tutorials

Save Gmail Attachments to Google Drive Automatically Using Google Apps Script

In this tutorial, I will teach you how to Save Gmail Attachments to Google Drive Automatically Using Google Apps Script. Basically, I will give you a small code snippet that you have to copy/paste inside Google Apps Script editor to enable the transfer of Gmail Attachments (PDF & Images) to Google Drive.

Step 1:- Open Google Apps Script Website

First of all, visit the script.google.com URL and then click the “New Project” button to create a new project.

Google Apps Script – New Project

Within a few seconds, a new Google Apps Script project will be created for you.

Here’s how the next screen will look.

Google Apps Script – Untitled Project

Step 2:- Save Gmail Attachments to Google Drive Automatically

Here’s a source code that you just need to copy/paste inside the code.gs file. This file is already opened in the Google Apps Script editor (as you can see in the above screenshot).

code.gs

const saveGmailtoGoogleDrive = () => {
  const folderId = '1XsVZoyADqioGGMsFSuK27iqBSqbnaSY2';
  const searchQuery = 'has:attachment';
  const threads = GmailApp.search(searchQuery, 0, 10);
  threads.forEach(thread => {
    const messages = thread.getMessages();
    messages.forEach(message => {
      const attachments = message.getAttachments({
          includeInlineImages: false,
          includeAttachments: true
      });
      attachments.forEach(attachment => {
        Drive.Files.insert(
          {
            title: attachment.getName(),
            mimeType: attachment.getContentType(),
            parents: [{ id: folderId }]
          },
          attachment.copyBlob()
        );
      });
    });
  });
};

Note:- Here you need to replace the value of const folderId with the ID of your own Google Drive folder.

Step 3:- Run the Script

Now, it’s time to execute this script by clicking the “Run” button given in the editor.

After running the script, you will see that all the attachments inside your Gmail emails are automatically downloaded to Google Drive.

Gmail emails with attachments

Output Screenshot

Save Gmail Attachments to Google Drive Automatically
Furqan

Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.

Recent Posts

Obsidian vs Notion (2026): I tested both for 6 months

If you have been searching for the right note-taking or knowledge management app, you have…

May 31, 2026

AnyType Alternatives: 10 Best Tools for Knowledge Management in 2026

Looking for AnyType alternatives? You're not alone. AnyType has gained popularity as a privacy-focused, local-first…

May 31, 2026

Notion Alternatives – Best Note-taking & Wiki Tools

Notion is a popular all-in-one workspace, but many users seek alternatives for different needs (free…

May 31, 2026

Best Logseq Alternatives in 2026: Find Your Perfect Knowledge Management Tool

Logseq is a beloved tool in the personal knowledge management (PKM) community. It's free, open-source,…

May 30, 2026

Webshare Alternatives: 8 Best Proxy Providers to Use in 2026

Looking for a Webshare alternative? You're not alone. Webshare is a popular proxy service with…

May 30, 2026

Docker Alternatives in 2026: The Complete Guide to Container Tools

Docker changed software development forever. It made containers accessible, gave developers a simple workflow, and…

May 30, 2026