Guide: Sending & Receiving Email
Guide: Sending & Receiving Email
Guide: Sending & Receiving Email
This guide walks you through the complete, practical workflow of an agent having a conversation. While the Core Concepts pages detail the individual API calls, this guide shows you how to stitch them together to create a functional conversational loop.
As a quick reminder from our Messages documentation, it’s a critical best practice to always provide both an html and a text version of your email. This ensures readability on all email clients and significantly improves deliverability.
A common task for an agent is to check for replies in an Inbox and then respond to them. While using Webhooks is the most efficient method for this, you can also build a simple polling mechanism.
Here’s the step-by-step logic for a polling-based conversational agent.
First, you need to identify which conversations have new messages that your agent hasn’t responded to. A great way to manage this is with Labels. You can list Threads in a specific Inbox that have an unreplied Label.
To reply to a conversation, you need to reply to the most recent message in the Thread. You can get a specific Thread by its ID, which will contain a list of all its Messages. You’ll then grab the ID of the last Message in that list.
Use last_message.extracted_text (or extracted_html) when you need
just the new reply content, without quoted history.
Now that you have the message_id to reply to, you can send your agent’s response. It’s also a best practice to update the Labels on the original Message at the same time, removing the unreplied Label and adding a replied Label to prevent the agent from replying to the same message twice.
For production applications, polling is inefficient. The best way to handle incoming replies is to use Webhooks. This allows AgentMail to notify your agent instantly when a new Message arrives, so you can reply in real-time.
Instead of sending immediately, you can schedule emails for a future time—perfect for delivering messages during business hours or spacing out outreach.
Create a Draft with the send_at field and AgentMail handles the rest. The email is automatically sent at the specified time.
For more details on scheduled sending—including how to cancel, reschedule, list scheduled drafts, and build conditional follow-up workflows—see the Drafts page.