Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/content/docs/agents/concepts/agent-class.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -351,25 +351,20 @@ class MyAgent extends Agent {
}
```

### Email Handling
### Email handling

Agents can receive and reply to emails using Cloudflare's [Email Routing](/email-routing/email-workers/).
Agents can receive and reply to emails using Cloudflare [Email Routing](/email-routing/email-workers/).

```ts
class MyAgent extends Agent {
async onEmail(email: AgentEmail) {
console.log("Received email from:", email.from);
console.log("Subject:", email.headers.get("subject"));

const raw = await email.getRaw();
console.log("Raw email size:", raw.length);

// Reply to the email
await this.replyToEmail(email, {
fromName: "My Agent",
subject: "Re: " + email.headers.get("subject"),
body: "Thanks for your email!",
contentType: "text/plain"
body: "Thanks for your email!"
});
}
}
Expand All @@ -387,6 +382,8 @@ export default {
};
```

For more details on email routing, resolvers, secure reply flows, and the full API, refer to [Email routing](/agents/guides/email-routing/).

### Context Management

`agents` wraps all your methods with an `AsyncLocalStorage` to maintain context throughout the request lifecycle. This allows you to access the current agent, connection, request, or email (depending of what event is being handled) from anywhere in your code:
Expand Down
Loading
Loading