Archive Gmail attachments to Google Drive with a Sheets log
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.
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.
Additional information
What does this prompt do?
- Polls Gmail for new emails and processes any message that has one or more attachments.
- Builds a tidy Google Drive folder path for each file, organized by sender domain and year-month, creating folders the first time they are needed.
- Uploads each attachment to Drive using a multipart upload for files under 5MB and a resumable upload for anything larger.
- Appends one row per attachment to a Google Sheets tracker with the timestamp, sender, subject, filename, file size, Drive link, and a link back to the original Gmail message.
What do I need to use this?
- A connected Gmail account that can read messages and download attachments.
- A connected Google Drive account with permission to create folders and upload files in your target Drive.
- A connected Google Sheets account and a spreadsheet to use as the attachment tracker.
- A root Drive folder ID (where domain folders should be created) and the spreadsheet ID and tab name for the log.
How can I customize it?
- Change the folder naming scheme, for example sender domain plus year-quarter, sender email address, or a flat date-only structure.
- Filter which messages are archived by adding rules on Gmail label, sender, subject, or attachment file type.
- Adjust the Sheets tracker columns, for example add Gmail labels, attachment MIME type, or a checksum for deduplication.
Frequently asked questions
How does the workflow decide which Drive folder to use?
What happens with very large attachments?
Will it skip inline images and signature logos?
Can I find the original email later?
Is this a deterministic workflow or does it use an AI model?
Stop hunting for that attachment from last quarter.
Connect Gmail, Google Drive, and Google Sheets once, and Geni files every new attachment for you with a searchable log.