diff --git a/src/content/docs/agents/concepts/agent-class.mdx b/src/content/docs/agents/concepts/agent-class.mdx index 4e3fa2c10f6..eca3a8714e2 100644 --- a/src/content/docs/agents/concepts/agent-class.mdx +++ b/src/content/docs/agents/concepts/agent-class.mdx @@ -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); } } ``` @@ -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` 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: diff --git a/src/content/docs/email-routing/email-workers/runtime-api.mdx b/src/content/docs/email-routing/email-workers/runtime-api.mdx index 65ef072a00e..eced6d3800e 100644 --- a/src/content/docs/email-routing/email-workers/runtime-api.mdx +++ b/src/content/docs/email-routing/email-workers/runtime-api.mdx @@ -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(""); + const result = await message.forward(""); + console.log("Email forwarded with ID:", result.messageId); }, }; ``` @@ -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; - reply(message: EmailMessage): Promise; + forward(rcptTo: string, headers?: Headers): Promise; + reply(message: EmailMessage): Promise; } ``` @@ -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. -* forward(rcptTostring, headersHeadersoptional) : Promise +* forward(rcptTostring, headersHeadersoptional) : Promise<EmailSendResult> * 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`. -* reply(EmailMessage) : Promise +* reply(EmailMessage) : Promise<EmailSendResult> * 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 @@ -143,4 +144,18 @@ An email message that can be sent from a Worker. * `to` string - * `Envelope To` attribute of the email message. \ No newline at end of file + * `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. \ No newline at end of file