Auto-Reply Email Agent
Auto-Reply Email Agent
Auto-Reply Email Agent
Learn how to build an email auto-reply agent that automatically responds to incoming emails. This beginner-friendly example demonstrates the core concepts of building with AgentMail: receiving webhooks, processing email events, and sending automated replies.
By the end of this guide, you’ll have a working auto-reply agent that:
Here’s what the user experience looks like:
Before you begin, make sure you have:
Required:
Create a new directory for your agent:
Create a file named agent.py and paste the following code:
Create a file named requirements.txt:
Install the required Python packages:
Create a .env file with your credentials:
Let’s understand how the agent works by breaking down the key components.
Key points:
.env variables before importing AgentMailAGENTMAIL_API_KEY from environmentThe setup_agentmail() function creates your inbox and webhook:
Why client_id?
The client_id parameter makes this operation idempotent - you can run it multiple times without creating duplicates. If the inbox already exists, AgentMail returns the existing one.
What’s happening:
localhost:8080The webhook endpoint receives incoming email notifications:
Why ignore message.sent?
When your agent sends a reply, AgentMail triggers a message.sent webhook. If we don’t filter this out, the agent would treat its own replies as new emails and respond to itself infinitely!
By returning 200, we tell AgentMail “I received this webhook successfully, but I’m choosing not to process it.”
Webhook payload structure:
Email addresses can come in different formats. We handle both:
Examples:
"John Doe <john@example.com>" → name: “John Doe”, email: “john@example.com”"john@example.com" → name: “John”, email: “john@example.com”"Last, First <name@example.com>" → name: “Name”, email: “name@example.com”This simple template-based approach requires no AI or external APIs. The reply is personalized with the sender’s name.
Important details:
to parameter must be a list of email addressesmessage_id links the reply to the original email (threading)200 status to acknowledge the webhookWhy always return 200?
Even if sending the reply fails, we return 200 to AgentMail. This tells AgentMail “I received and processed this webhook.” If we returned an error status, AgentMail would retry sending the webhook multiple times, which isn’t helpful for application errors.
Start the agent:
You should see output like this:
Success! Your agent is now running and ready to receive emails.
Leave this terminal window open - closing it will stop the agent.
Let’s verify everything works by sending a test email.
Open your personal email (Gmail, Outlook, etc.)
Compose a new email:
Send the email
In your terminal, you should see:
Within seconds, you should receive an automated reply:
It works! You just built and tested your first AgentMail agent.
The agent extracted your name, personalized the message, and sent an instant reply.
You can customize the auto-reply message by editing the generate_reply() function in agent.py.
The function has access to:
sender_name - The sender’s name extracted from their emailsubject - The subject line of the emailSimply modify the text in the return statement to change what your agent replies with.
Problem: Python dependencies not installed.
Solution:
If using a virtual environment, make sure it’s activated first:
Problem: Invalid or missing API key.
Solutions:
.env file has the correct AGENTMAIL_API_KEY.env is in the same directory as agent.pyTest your API key:
Problem: Invalid or missing ngrok auth token.
Solutions:
NGROK_AUTHTOKEN in .envAlternatively, configure ngrok globally:
Checklist:
python agent.py should show “Waiting for incoming emails…”)curl https://your-domain.ngrok-free.app/webhook/agentmailDebug steps:
Check ngrok dashboard for webhook requests:
Verify webhook is registered:
Problem: Another process is using port 8080.
Solution 1: Kill the process using the port
Solution 2: Use a different port
Problem: Webhook received but no reply sent.
Debug steps:
to is a list (common mistake):Congratulations! You’ve built your first AgentMail agent.
Want to upgrade your agent with intelligent, context-aware responses? You can add AI-powered replies using OpenAI.
Step 1: Install OpenAI
Step 2: Add your OpenAI API key to .env
Step 3: Add thread history and AI reply functions to agent.py
After the generate_reply() function, add:
Step 4: Update the webhook handler
In the receive_webhook() function, add thread_id extraction and replace the reply generation section:
How it works:
client.threads.get(thread_id)Result:
Your agent now has conversation memory. When replying to follow-up emails, the AI sees the entire conversation history and can provide contextual, intelligent responses that reference previous exchanges instead of generic auto-replies.
If you build something cool with AgentMail, we’d love to hear about it. Share in our Discord community!