Skip to main content

Code

cookbook/tools/gmail_tools.py
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.gmail import GmailTools

agent = Agent(
    name="Gmail Agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[GmailTools()],
    description="You are an expert Gmail Agent that can read, draft, send and manage email labels.",
    instructions=[
        "You can read, draft, send and organize emails using Gmail.",
        "Summarize email contents and extract key details and dates.",
        "Show email contents in a structured markdown format.",
    ],
    markdown=True,
)

# Example: Read and summarize emails
agent.print_response(
    "Summarize my last 5 emails with dates and key details",
    markdown=True,
    stream=True,
)

# Example: Organize emails with labels
agent.print_response(
    "Apply the 'Newsletter' label to emails containing 'weekly update'",
    markdown=True,
    stream=True,
)

Usage

1

Create a virtual environment

Open the Terminal and create a python virtual environment.
python3 -m venv .venv
source .venv/bin/activate
2

Set up Google Cloud Project

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Gmail API for your project
  4. Create OAuth 2.0 credentials and download the client configuration file
3

Set your API keys

export GOOGLE_CLIENT_ID=your_client_id_here
export GOOGLE_CLIENT_SECRET=your_client_secret_here
export GOOGLE_PROJECT_ID=your_project_id_here
export GOOGLE_REDIRECT_URI=http://127.0.0.1:8080
export OPENAI_API_KEY=xxx
4

Install libraries

pip install -U google-api-python-client google-auth-httplib2 google-auth-oauthlib openai agno
5

Run Agent

python cookbook/tools/gmail_tools.py