Have an email inbox you’ve neglected for so long that it’s now full to the brim and about to burst with messages? One option is to set aside an hour or two and start deleting them, page by page, or – you can use artificial intelligence (AI) agents to automate Gmail.
Indeed, this is exactly what happened to developer Tony Kipkemboi, known as tonykipkemboi on GitHub and The How-To Guy on YouTube, so he decided to deploy AI agents to categorize and carry out other tasks in his Gmail account, including generating drafts for certain emails.
And the best thing? He described the process in detail on his YouTube channel in a video streamed on March 7. On top of that, he posted the code alongside easy-to-understand instructions on GitHub, allowing pretty much anyone to copy his strategy with minimum coding experience.
Specifically, tonykipkemboi wanted the AI agents to read through the emails, classify and label them, and do some other work, like delete low-priority messages, in short – automate Gmail. So he gathered five AI agents into a ‘crew’ called CrewAI to create an intelligent email management system to categorize, organize, respond to, alert, and clean up his Gmail inbox automatically.
Setting up the system to automate Gmail
These agents included an email categorizer, email organizer, response generator, Slack notifier, and email cleaner, all with their own specific assigned functions. Together, these AI agents are able to automatically carry out the following actions in sequence:
- categorize emails into specific types (like newsletters, promotions, personal, etc.),
- assign priority levels (high, medium, or low) based on content and sender with strict classification rules,
- apply Gmail labels and stars based on categories and priorities,
- generate draft responses to important emails that need replies,
- send creative notifications for high-priority emails to Slack, Telegram, or WhatsApp,
- safely delete low-priority emails based on age and category,
- handle YouTube-related emails specifically,
- empty trash to free up storage space,
- recognize and properly handle email threads.
To help others, the developer has shared with his viewers the entire code repository that pretty much anyone can follow with the help of the video and a little bit of coding experience.
Installation
Copy and paste the following code:
# Clone the repository git clone https://github.com/tonykipkemboi/crewai-gmail-automation.git cd crewai-gmail-automation # Create and activate a virtual environment python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate # Install dependencies crewai install |
Configuration
Create a .env file in the root directory with the following variables:
# Choose your LLM provider # OpenAI (Recommended) MODEL=openai/gpt-4o-mini OPENAI_API_KEY=your_openai_api_key # Or Gemini # MODEL=gemini/gemini-2.0-flash # GEMINI_API_KEY=your_gemini_api_key # Or Ollama (Note: May have compatibility issues with tool calling) # Download the model from https://ollama.com/library # MODEL=ollama/llama3-groq-tool-use # use ones that have tool calling capabilities # Gmail credentials EMAIL_ADDRESS=your_email@gmail.com APP_PASSWORD=your_app_password # Optional: Slack notifications SLACK_WEBHOOK_URL=your_slack_webhook_url |
To create a Gmail app password for your procedure, you’ll need to:
- Go to your Google Account settings.
- Select ‘Security’ from the left navigation panel.
- Under ‘Signing in to Google,’ enable 2-Step Verification.
- Scroll to find ‘App passwords.’
- Select ‘Mail’ from the ‘Select app’ dropdown.
- Click on ‘Other (Custom name)’ in the ‘Select device’ dropdown.
- Enter ‘Gmail CrewAI’ as the name.
- Click ‘Generate.’
- Copy the 16-character password that appears.
- Paste the password in your .env file as the ‘APP_PASSWORD’ value.
- Press ‘Done.’
Furthermore, to create a Slack Webhook URL:
- Go to api.slack.com/apps.
- Click ‘Create New App.’
- Select ‘From scratch.’
- Enter ‘Gmail Notifications’ as the app name.
- Select your workspace and click ‘Create App.’
- In the left sidebar, press ‘Incoming Webhooks.’
- Turn on ‘Activate Incoming Webhooks.’
- Click ‘Add New Webhook to Workspace.’
- Select the channel where you want to receive notifications.
- Click ‘Allow.’
- Find the ‘Webhook URL’ section and copy the URL that begins with https://hooks.slack.com/services/.
- Paste this URL in your .env file as the ‘SLACK_WEBHOOK_URL’ value.
IMAP function
The developer has also used the IMAP (Internet Message Access Protocol) to securely connect to his Gmail account and manage his emails because it allows the app to work with emails while they remain on Google’s servers, unlike POP3, which downloads them to your device.
Usage
Run the application with:
crewai run |
You’ll be prompted to enter the number of emails to process (the default is 5, but you can go with a higher number if you feel comfortable).
And that’s pretty much it – your Gmail inbox is now automated and you can have a peace of mind that it’s not overwhelmed with emails preventing new messages from arriving.
Conclusion
All things considered, AI agents might be a new thing, but they’re becoming increasingly accessible to a wider audience regardless of their level of technical expertise. For instance, OpenAI has just launched its new AI building tools, making the development of proprietary AI agents easier than ever before.