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
25 changes: 24 additions & 1 deletion src/content/docs/agents/concepts/agent-class.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,13 @@ class MyAgent extends Agent {
console.log("Raw email size:", raw.length);

// Reply to the email
await this.replyToEmail(email, {
const result = await this.replyToEmail(email, {
fromName: "My Agent",
subject: "Re: " + email.headers.get("subject"),
body: "Thanks for your email!",
contentType: "text/plain"
});
console.log("Email sent with ID:", result.messageId);
}
}
```
Expand All @@ -387,6 +388,28 @@ export default {
};
```

The `AgentEmail` interface provides direct access to the underlying email message methods:

```ts
class MyAgent extends Agent {
async onEmail(email: AgentEmail) {
// Forward the email to another address
const forwardResult = await email.forward("destination@example.com");
console.log("Forwarded with message ID:", forwardResult.messageId);

// Reply to the email
const replyResult = await email.reply({
from: email.to,
to: email.from,
raw: rawEmailContent
});
console.log("Replied with message ID:", replyResult.messageId);
}
}
```

Both `email.forward()` and `email.reply()` return a `Promise<EmailSendResult>` which contains a `messageId` field that can be used for tracking sent emails.

### 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
31 changes: 23 additions & 8 deletions src/content/docs/email-routing/email-workers/runtime-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ An `EmailEvent` is the event type to programmatically process your emails with a
```js
export default {
async email(message, env, ctx) {
await message.forward("<YOUR_EMAIL>");
const result = await message.forward("<YOUR_EMAIL>");
console.log("Email forwarded with ID:", result.messageId);
},
};
```
Expand Down Expand Up @@ -84,8 +85,8 @@ addEventListener("email", async (event) => {
public constructor(from: string, to: string, raw: ReadableStream | string);

setReject(reason: string): void;
forward(rcptTo: string, headers?: Headers): Promise<void>;
reply(message: EmailMessage): Promise<void>;
forward(rcptTo: string, headers?: Headers): Promise<EmailSendResult>;
reply(message: EmailMessage): Promise<EmailSendResult>;
}
```

Expand Down Expand Up @@ -115,15 +116,15 @@ An email message that is sent to a consumer Worker and can be rejected/forwarded

* Reject this email message by returning a permanent SMTP error back to the connecting client, including the given reason.

* <code>forward(rcptTostring, headersHeadersoptional)</code> : Promise
* <code>forward(rcptTostring, headersHeadersoptional)</code> : Promise&lt;EmailSendResult&gt;

* Forward this email message to a verified destination address of the account. If you want, you can add extra headers to the email message. Only `X-*` headers are allowed.
* When the promise resolves, the message is confirmed to be forwarded to a verified destination address.
* When the promise resolves, the message is confirmed to be forwarded to a verified destination address and returns an `EmailSendResult` object containing a `messageId`.

* <code>reply(EmailMessage)</code> : Promise
* <code>reply(EmailMessage)</code> : Promise&lt;EmailSendResult&gt;

* Reply to the sender of this email message with a new EmailMessage object.
* When the promise resolves, the message is confirmed to be replied.
* When the promise resolves, the message is confirmed to be replied and returns an `EmailSendResult` object containing a `messageId`.


## `EmailMessage` definition
Expand All @@ -143,4 +144,18 @@ An email message that can be sent from a Worker.

* `to` string

* `Envelope To` attribute of the email message.
* `Envelope To` attribute of the email message.

## `EmailSendResult` definition

```ts
interface EmailSendResult {
readonly messageId: string;
}
```

The result of sending an email via `forward()` or `reply()`.

* `messageId` string

* A unique identifier for the sent email message that can be used for tracking and logging purposes.
Loading