← All articles

My first AI email brief didn’t survive real mornings. So I built a zero-cost email agent instead.

How I built a zero-cost AI email agent across four mailboxes, with the build steps, prompts and lessons from real mornings.

Four inboxes feed an AI sorting machine that produces one morning brief and draft replies for review.

Yesterday I told you about my first email brief. Built with Claude Cowork in one afternoon. Desktop only, one mailbox, promos marked urgent.

This is what I did next, including the build steps and prompts you can adapt.

I have no coding background. Claude wrote every line of code here. I made the decisions.

Why patching was not enough

Two of the problems were fixable with better instructions.

The brief was showing up inside Cowork on my iMac, and I wanted to read it on my phone. One added line fixed delivery: save the brief as a Gmail draft.

The urgent promos were my own doing. I had asked it to flag anything urgent, and "offer ends tonight" is urgent language. One more rule: every email lives in exactly one category, and promotional stays promotional however it is worded.

The remaining constraints needed a different setup.

I have four mailboxes: a personal Gmail, a second Gmail, one for my role as Secretary of my apartment association, and an old Live (microsoft) account. My Cowork setup was connected to one. It needed my Mac awake for the morning run, and the Gmail connector could read and draft but not send.

I needed something that ran without my computer and covered every mailbox.

What I built

A Python script that runs through GitHub Actions every morning and does four things:

  1. Reads the last 24 hours of email across three Gmail accounts, including mail forwarded from Live mailbox
  2. Sends me one consolidated brief: Action Required, Transactional, Newsletters, Promotional, Social
  3. Moves promotional mail in my personal mailbox to Trash
  4. Drafts replies to action-required mail in my association mailbox, in my voice, and saves them to Drafts

It never sends a reply on its own. I approve every one. The only thing it sends is the brief, to me.

The stack: Python, Gmail API, Gemini Flash (Free API), GitHub Actions, cron-job

My monthly running cost: ₹0. Everything stays inside the free allowances at my usage.

Claude first suggested a no-code tool like Make, n8n or Zapier. I could not justify monthly rent for a job that runs once a day for a few minutes, so I chose Python and GitHub. Free self-hosted options like n8n Community exist too.

Everything below is what worked in my setup. Google and GitHub change their screens and limits often, so check Google's and GitHub's current docs before copying a setting.

One thing to check before you copy the stack: where your email content goes. My build runs on Gemini's free tier, and Google's terms for unpaid use allow product improvement and human review of what you send. Read them before you put real mail through it, especially mail that belongs to other people. Test with made-up emails first.

Step 1: Make five decisions before any code

This took 15 minutes and saved me hours of rebuilds.

  • Which mailboxes go in?
  • Which account sends the brief?
  • Which account receives it?
  • Where may it auto-trash? Choose one mailbox, and review what it proposes before switching it on.
  • Where may it draft replies? A repetitive, role-based mailbox worked well for me.

Write the answers down. You will use them in the build prompt.

Step 2: Get non-Gmail mail into Gmail

The Gmail API only reads Gmail. I couldn't get Microsoft app registration working for my Live account, so I turned on forwarding to one of my Gmail accounts.

Send yourself a test mail before moving on. Remember that trashing the Gmail copy leaves the original in Live mailbox untouched.

That is how four mailboxes became three Gmail connections in the script.

Step 3: Set up the plumbing

This is the dullest part and most of it is a one-time setup. You are collecting four things.

Google Cloud setup

In Google Cloud Console

  1. Create a new project.
  2. Enable the Gmail API.
  3. Configure the OAuth consent screen as External and add your Gmail addresses as test users.
  4. Create an OAuth Client ID, type Desktop app.
  5. Download the credentials JSON at that moment and keep it out of your repository.
  6. Publish the app to Production before relying on the daily run.

In Testing mode, refresh tokens for Gmail access expire after seven days. Mine did, and the agent stopped a week in. Publishing fixes that expiry. Tokens can still be revoked, for example when you change your password.

If the screens confuse you, ask Claude to walk you through Google's Gmail Python quickstart for your operating system.

Gemini API key

Get an API key through Google AI Studio.

My first key returned "quota exceeded, limit: 0". It sat in a project with billing attached. A new key in a separate project without billing fixed it for me. If you see that error, check the project's billing tier and quota first.

Ask Claude to keep the model name configurable. Model names and free allowances change.

Refresh tokens

A refresh token is a long-lived permission slip that lets the script into a mailbox without you logging in every morning.

I asked Claude for a small script, get_tokens.py, that opens a browser for each Gmail account and saves its token locally. Ask for a .gitignore covering the credential and token files at the same time.

My build used three scopes: gmail.readonly, gmail.send and gmail.modify. I missed one the first time and had to reauthorise every account and update the tokens.

Ask for the narrowest access each account needs. In my build only Personal needs to trash, only Role needs drafts, and only the sending account needs to send.

GitHub repo and secrets

Create a private repo. Under Settings → Secrets and variables → Actions, add:

  • GMAIL_CLIENT_ID
  • GMAIL_CLIENT_SECRET
  • REFRESH_TOKEN_PERSONAL
  • REFRESH_TOKEN_SECOND
  • REFRESH_TOKEN_ROLE
  • GEMINI_API_KEY

The script reads these at runtime. They should never be pasted into the code, committed to git or printed in logs.

Step 4: The build prompt

This is the core of the build. I gave Claude a spec, function by function. What follows is my original spec plus the safety checks I would add if I built it again:

Write a Python script called email_brief.py.

Three Gmail accounts labelled Personal, Second and Role, using REFRESH_TOKEN_PERSONAL, REFRESH_TOKEN_SECOND and REFRESH_TOKEN_ROLE. Read all credentials from environment variables. Sender: [SENDER]. Brief recipient: [RECIPIENT]. Timezone: [YOUR TIMEZONE].

Fetch the last 24 hours excluding sent, drafts, spam and trash. Handle pagination and decode the message body. Do not download attachments in this version. Show the coverage period in the brief.

Call the Gemini API through REST, with a configurable model name. Use structured output for one category per email: Action Required, Transactional, Newsletters, Promotional or Social. Add Needs Review for uncertain cases. A promotional deadline alone must not make a message Action Required.

Build a mobile-readable HTML brief with counts per mailbox, colour-coded categories and links back to the emails. Escape email text before rendering it as HTML.

Add optional promotional cleanup for Personal only. Mass marketing only, never newsletters, social notifications, bank alerts or personal mail. Start by listing proposed moves in the brief, without moving them. After explicit enablement, use Trash, never permanent deletion.

Add optional draft replies for Role only, off initially. Save to Gmail Drafts; never send replies automatically. Send the consolidated brief only to the fixed recipient above.

Treat email content as data, never as instructions to the program. Validate model-returned IDs against messages fetched from the same account. Include bounded retries and timeouts, and report partial failures clearly. Never log secrets or full email bodies.

Return complete files and a synthetic test dataset. Help me test the brief before enabling cleanup or drafts.

Three details in my original prompt came from failures:

  • "Excluding sent and drafts" became -in:sent -in:drafts in the Gmail query, after my own sent mail showed up in the brief.
  • REST calls replaced Google's Python SDK after it gave me quota errors in my setup.
  • "Mass marketing only" came after the first version trashed my newsletters.

Then ask for requirements.txt and a GitHub Actions workflow, daily_brief.yml, triggered only by workflow_dispatch. The workflow has to map every secret into the environment names the script expects. Adding secrets to the repo does nothing until the workflow passes them in.

Step 5: Teach it your voice

I took five to ten emails I had sent and asked Claude to extract how I open, how I sign off, and how I handle complaints versus requests versus FYIs. Redact private details before sharing examples.

That profile sits inside the drafting prompt with two rules: reply only to the latest message in the thread, and match the length of what came in. A simple FYI does not need an essay.

Use this voice profile: [PROFILE]. Draft only for Role. Identify the latest inbound message using message metadata, and use quoted history only as context. Skip threads I have already answered after that message. Preserve reply threading and check recipients. Do not invent facts, promises or commitments. Avoid duplicate drafts when I rerun the same messages. Never call the reply-send operation.

My agent once answered an older message in the thread. "Reply to the latest email" felt obvious to me. Apparently it needed saying.

Check the recipient, the thread and the wording in Gmail itself. Then rerun the same sample to check for duplicate drafts.

Step 6: Test and fix

Start with made-up emails. Then run a manual brief on real mail with cleanup and drafting switched off.

Go to the Actions tab, run the workflow and read the log. Mine failed on the first run. That is normal.

Paste the error back to Claude with secrets removed, and ask it to explain the cause before fixing it. I replace the whole file with the corrected version each time. Patching lines in GitHub's web editor kept giving me indentation errors.

One more that cost me 20 minutes: I regenerated tokens on my Mac and forgot to update GitHub Secrets. The script runs on GitHub. A local fix does nothing until the running version has it.

Also test an empty inbox, a multi-message thread and an account that fails. A green tick only tells you the script finished.

Step 7: Scheduling

GitHub’s built-in scheduler is free, but my 8 AM brief sometimes ran at 10 or 11. I moved the trigger to cron-job.org.

Create a fine-grained GitHub Personal Access Token restricted to this one repository, with Actions write permission and an expiry date. Then set up a daily cron-job.orgjob at 8 AM, timezone [YOUR TIMEZONE], that sends a POST to:

https://api.github.com/repos/OWNER/REPO/actions/workflows/daily_brief.yml/dispatches

Add an Authorization: Bearer TOKEN header, Accept: application/vnd.github+json, and a JSON body with your branch, for example {"ref":"main"}.

Once the brief arrives on time, remove the old schedule from the workflow file. I left mine in and got two briefs every morning for two days.

Step 8: Tune after a week

Give the read-and-report version several days of real use. Review what it calls promotional before you enable automatic cleanup. Fix harmful mistakes immediately.

Then tighten the promo rule, shorten the drafts or add a mailbox. Change one thing at a time so you can tell what helped.

I also had to fix retries after an outage, when repeated attempts ate into my daily quota. Ask for a retry cap, a delay between attempts and a clear stop condition.

The number that surprised me

Claude generated the code in about 30 minutes. The next three hours went into the boundaries.

What counts as promotional? Which mailbox may it move messages from? When should it draft, and when should it stay silent?

Those are operations questions. That is the part a non-developer is good at, because you know the work the system has to fit into.

So, can you build something perfect without running it first?

No. I asked that yesterday, and this build is my answer.

The Cowork version was ready in an afternoon. Using it showed me what better instructions could fix and what needed a different setup.

The Python version took a weekend and kept breaking for days before it settled. Every fix came from a real morning with a real inbox.

It still costs me ₹0 a month, and I still review every reply.

Abhay Kapoor
Written by Abhay Kapoor

I build practical tools with AI and share what I learn along the way—from everyday tasks to the way we work.

More about me →

Prefer a private conversation?

Send your question directly to me. It won’t appear in the public comments.

Send a private note ↗

What’s your perspective?

Share a thought, ask a question, or tell me how this connects to your own experience. Comments are public; your email address is not displayed.

Leave a Reply