Email is the single biggest time sink for most indie hackers. You check it first thing in the morning, and before you know it, an hour is gone. The fix isn’t a better inbox or a productivity app — it’s a script that does the repetitive parts for you.
In this post, I’ll walk through a Python script that connects to Gmail’s API, applies labels based on sender or subject, archives newsletters, and even drafts a simple response for common questions. It’s not a magic bullet, but it can cut your inbox time in half.
## Why a script beats a rule set
Most email clients let you create rules, but those are limited. You can’t chain conditions, call external APIs, or make decisions based on the content of the email. A script gives you full control. For example, you can automatically label any email from a client containing the word ‘invoice’ and then send a Slack notification.
## The core script
Here’s a simplified version of the script. It uses the `google-api-python-client` and `oauth2client` libraries. You’ll need to create a project in Google Cloud Console and enable the Gmail API.
“`python
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
# … (setup code)
def process_emails():
service = build(‘gmail’, ‘v1′, credentials=creds)
results = service.users().messages().list(userId=’me’, q=’in:inbox newer_than:1d’).execute()
for msg in results.get(‘messages’, []):
message = service.users().messages().get(userId=’me’, id=msg[‘id’]).execute()
# Your logic here
“`
## Real use case: newsletter sorting
I get a lot of newsletters. I wanted them out of my inbox but didn’t want to unsubscribe from all of them. The script labels them as ‘Newsletters’ and archives them automatically. It also adds a label ‘Read Later’ for ones from specific senders I actually want to read.
## Taking it further
Once you have the basics, you can add more: a daily digest of important emails, auto-responding to common queries, or even categorizing emails by project. The script runs on a server (or your local machine) using cron or Task Scheduler.
## The payoff
Since I set this up, I spend maybe 10 minutes a day on email instead of 40. That’s 2.5 hours a week, which is 130 hours a year. That’s worth a few hours of setup.
## Wrap up
Automating your inbox isn’t just about saving time — it’s about reclaiming focus. Try the script, tweak it to your needs, and see how much brain space you free up.
If you want a ready-to-run version, check out my ‘Email Auto-Pilot’ script in the library. It includes all the setup instructions and handles the OAuth flow for you.
