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.
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.
Start a new script
Go to script.google.com, click New project and name it Attachments to Drive.
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); } }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.
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.
Paste this into Claude Code, Codex or any coding agent. The agent writes and sets up the automation, then tells you what to click.
Write a Google Apps Script for me that saves Gmail attachments into a Google Drive folder. - Ask me for the Drive folder ID, or create a folder named "Email attachments" if I don't have one. - Process only threads that have attachments and don't yet carry the Gmail label "saved-to-drive"; create that label if it is missing, and add it to each thread once its files are saved. - Skip inline images under 20 KB (signatures and logos). - Name each file "YYYY-MM-DD sender - original name". - Add a time-driven trigger that runs it every hour, and a function to remove the trigger. - Then tell me exactly where to paste it on script.google.com and what I need to click.
You still need to
- Sign in to Google and click Allow when the script first runs.
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.