type
status
date
slug
summary
tags
category
icon
password
My inbox was a mess of newsletters and vague emails, so I used Google Apps Script and Gemini to sort and prioritize them automatically.
The Email Avalanche
My inbox has become a digital nightmare. With subscriptions to newsletters for work, hobbies, and industry updates, plus the usual flood of personal and professional emails, I’m buried under a mountain of messages. The real problem isn’t just the volume—it’s the lack of clarity. Many subject lines are so vague that I can’t tell if an email is critical or just another marketing pitch. I’ve spent too much time opening irrelevant emails or skimming ones that deserved more attention, and it’s exhausting. This inefficiency was draining my focus, so I set out to find a smarter way to manage my inbox.
A Smarter Solution
To tackle this, I turned to Google Apps Script and Gemini, since I’m already using Gmail. Google’s infrastructure makes automating tasks within Gmail straightforward, and I’d previously set up my account with Gemini, which gave me a leg up. My goal was to create a system that automatically classifies incoming emails based on their subject lines, sender, and content, helping me prioritize without needing to open each one. Using Google Apps Script, I could pull emails, process them with Gemini’s assistance, and assign labels like “Financial,” “Newsletter,” or “Promotional Spam.” This approach saves time and lets me focus my attention where it’s needed most.
Building the Classification System
The heart here is the prompt for Gemini to classify emails. These prompts include rules, like prioritizing the sender’s domain (e.g., “Amazon” for Purchase/Receipt or “LinkedIn” for Career) and then analyzing keywords in subject lines or content snippets for context. For example, a prompt might direct Gemini to label an email as “Newsletter” if it’s from a known subscription source like Substack or “Financial” if it contains terms like “invoice” or “transaction.” I used LLM to refine these prompts, tweaking the wording to boost accuracy. The system uses eight categories: Career, Entertainment, Financial, Newsletter, Purchase/Receipt, System, Travels, and Promotional Spam. These labels are applied automatically in Gmail, allowing me to filter my inbox with a quick glance.

How It All Works
The process is simple but effective, driven by a Google Apps Script that runs periodically. The script is structured around several key components:
- Configuration and Labels: The script defines the eight categories with detailed descriptions (e.g., “Financial” for bank statements, “Travels” for airline bookings) and stores them in objects. It also sets up a marker label, “LLM-Categorized,” to track processed emails.
- Helper Functions: Functions create Gmail labels if they don’t exist, extracts the sender, subject for analysis, sends prompts to Gemini’s API (using the gemini-1.5-flash-latest model) and retrieves the classification.
- Main Processing Loop: Functions fetches unread emails within a specified date range (e.g., “today” or “yesterday”) in batches of up to 300 threads to respect Gmail’s API limits. It constructs a prompt for each email, combining the category definitions with the email’s content, and sends it to Gemini. The returned category (e.g., “Newsletter”) is applied as a Gmail label, along with the “LLM-Categorized” marker. If Gemini returns an unrecognized category or fails, the script applies only the “LLM-Categorized” label to avoid reprocessing.
- Error Handling and Timing: The script includes robust error handling for API failures (e.g., rate limits or invalid keys) and monitors execution time to stay within Google Apps Script’s six-minute limit, pausing between requests to avoid overloading the API.
The process is straightforward: fetch emails, classify them with AI, and label them in Gmail, making the system easy to follow and maintain. For the only challenging part, I also received help from Gemini to resolve it, implementing everything in JavaScript to run on Google Apps Script.
A Game-Changer for My Inbox
This project has been a lifesaver for managing my inbox. By combining Google Apps Script’s automation with Gemini’s classification smarts, I’ve reclaimed time and mental energy. The script processes emails efficiently, typically handling hundreds in a single run, and the detailed category definitions ensure accurate labeling most of the time. It’s not flawless—occasional misclassifications happen, especially with ambiguous emails, and I’m still refining the prompts—but it’s a huge step forward. If you’re overwhelmed by emails, I highly recommend exploring Google Apps Script +LLM like Gemini. It’s a practical way to tame your inbox, and you don’t need to be a tech expert to make it work.