Runs: Every hour

Save Gmail attachments to Google Drive

Every invoice, ticket and photo someone emails you lands in a Drive folder on its own, so you never dig through old mail for it again.

Most people end up with a mail search like has:attachment from:landlord that they run every few months. This bot does that search for you every hour and files what it finds, then labels the email so it never saves the same file twice.

  1. Make a folder and a label

    In Google Drive, create a folder called Email attachments. Open it and copy the long ID at the end of the address bar, after folders/.

    In Gmail, create a label called saved-to-drive. The bot uses it to remember which emails it has already handled.

  2. Start a new script

    Go to script.google.com, click New project and name it Attachments to Drive.

  3. Paste the script

    Replace everything in the editor with this, and put your folder ID between the quotes on the first line:

    const FOLDER_ID = 'paste-your-folder-id-here';
    
    function saveAttachments() {
      const folder = DriveApp.getFolderById(FOLDER_ID);
      const label = GmailApp.getUserLabelByName('saved-to-drive');
      const threads = GmailApp.search('has:attachment -label:saved-to-drive newer_than:7d');
      for (const thread of threads) {
        for (const message of thread.getMessages()) {
          for (const file of message.getAttachments()) {
            folder.createFile(file.copyBlob());
          }
        }
        thread.addLabel(label);
      }
    }
    
  4. Run it once and allow access

    Click Run. Google asks you to allow the script to read your mail and write to Drive; this is your own script in your own account, so allow it. Check the folder: recent attachments should appear.

  5. Make it run every hour

    Open Triggers (the clock icon on the left), click Add trigger, choose saveAttachments, Time-driven, Hour timer, Every hour, and save.

Things that trip people up

  • Email signatures with logos count as attachments too. The agent version skips small images; the by-hand version saves everything.
  • Files count against your Google storage, which Gmail, Drive and Photos share.