Archive Gmail attachments to Google Drive with a Sheets log

By General Input

Auto-file every Gmail attachment into Google Drive folders by sender domain and month, and log each file to a Google Sheets tracker so nothing gets lost.

Integrations

  • Gmail
  • Google Drive
  • Google Sheets

Type

Deterministic Code

Categories

  • Personal Productivity
  • Operations

Build a code workflow that archives every Gmail attachment I receive into a tidy Google Drive folder structure and logs it in a Google Sheets tracker, so I can find any attachment later without searching my inbox.

Trigger: poll Gmail for new emails using the new_email event on my connected Gmail account. For each new message, only continue if the message has at least one non-inline attachment.

For every triggering message, use Gmail Get a Message (full format) to load the message and walk its parts. Treat a part as an attachment when it has a filename and an attachmentId, and skip parts that are inline (Content-Disposition inline) so signature logos and embedded images are ignored.

For each real attachment, call Gmail Get Attachment with the messageId and attachmentId to download the base64url-encoded bytes. Decode them in memory so they can be uploaded to Drive.

Derive the destination Drive folder path as senderDomain/YYYY-MM, where senderDomain is parsed from the From header (everything after the @, lowercased) and YYYY-MM is the year and month from the message internalDate in my local timezone. Under a configurable root folder ID, ensure both the domain folder and the year-month subfolder exist by listing children first and calling Google Drive Create Folder only when missing. Cache folder IDs across attachments in the same run so we do not recreate them.

Upload each attachment into the resolved year-month folder. If the file size is 5MB or less, use Google Drive Upload File (Multipart). If it is larger than 5MB, use Google Drive Upload File (Resumable). Keep the original filename, set the correct MIME type from the Gmail part headers, and capture the returned Drive file ID and webViewLink.

After each successful upload, append one row to a configurable Google Sheets tracker using Google Sheets Append Values. Columns, in order: received timestamp (ISO 8601, from internalDate), sender (full From address), subject, filename, file size in bytes, Drive file URL (webViewLink), and Gmail message link (https://mail.google.com/mail/u/0/#inbox/<messageId>). One row per attachment, so a single email with three attachments produces three rows.

Inputs the workflow should expose: the Gmail account to monitor, the Drive root folder ID where domain folders live, and the Google Sheets spreadsheet ID plus tab name for the tracker. Optionally allow a Gmail query filter (for example has:attachment -label:archived) to narrow which messages are processed.

Make this fully deterministic with no LLM calls. Handle errors per attachment so one failed upload does not block the rest of the message, and log enough context (messageId, filename) to retry.

Related prompts

Explore more prompts
Call overdue Xero customers with an AI collections agentLocal listing health board for every location you manageWin back LiveChat visitors whose chats went unansweredLet support send one-off Loops emails without an engineerStop cold emails to anyone with a live deal in PipedriveiMessage campaign console with pre-flight checks and delivery boardLinkedIn Ads budget pacing dashboard for every client accountFront desk appointment confirmation board for the next 3 daysGive your team Looker numbers without buying more seatsBuild audience segments from product usage and push to Loops