Feature: Email Digest & Summaries
Relevant source files
The following files were used as context for generating this wiki page:The Email Digest and Summaries feature provides users with a consolidated, AI-generated overview of their inbox activity. It works by summarizing individual emails, grouping them by category, and sending a single digest email to the user. This allows users to quickly catch up on important emails without having to read each one individually.
The system is composed of several key parts: an AI-powered summarization service for individual emails, a dynamic email template for rendering the digest, and a service for dispatching the final email. A separate, more generic summarization API is also available for on-demand text summarization.
Digest Generation and Sending
The process of generating and sending a digest email is handled by the sendDigestEmail function. This function orchestrates the creation of the email's subject and body, and then dispatches it using the Resend service.
Sources: packages/resend/src/send.tsx:112-129
Data Flow
The following diagram illustrates the high-level flow of sending a digest email.
Key Functions
sendDigestEmail: The main function that takesDigestEmailPropsand initiates the sending process. It callsgenerateDigestSubjectand renders theDigestEmailcomponent.
Sources: packages/resend/src/send.tsx:112-129generateDigestSubject: Creates a dynamic email subject line based on the top 3 categories with the highest email counts in the digest. For example, "Summary of 8 newsletter, 10 receipt and 3 marketing emails".
Sources: packages/resend/emails/digest.tsx:82-109sendEmail: A generic email sending utility that renders the React email component to HTML, adds necessary headers likeList-Unsubscribe, and sends the email via the Resend client.
Sources: packages/resend/src/send.tsx:13-46
AI-Powered Email Summarization for Digest
The core of the digest feature is its ability to summarize individual emails. This is handled by the aiSummarizeEmailForDigest function, which uses an LLM to generate a concise summary tailored for inclusion in the digest.
Sources: apps/web/utils/ai/digest/summarize-email-for-digest.ts
Summarization Process
The sequence diagram below shows the steps involved in summarizing an email for the digest.
Function and Prompts
The aiSummarizeEmailForDigest function is designed to produce context-aware summaries.
| Parameter | Type | Description |
|---|---|---|
ruleName | string | The category of the email (e.g., "newsletter"). |
emailAccount | EmailAccountWithAI | The user's account details, including AI settings. |
messageToSummarize | EmailForLLM | The parsed email object to be summarized. |
Sources: apps/web/utils/ai/digest/summarize-email-for-digest.ts:22-26
The system prompt provides specific guidelines to the LLM:
- Summarize direct messages in the second person (e.g., "You are invited...").
- Use a "Key: Value" list format for emails with extractable data like order details.
- Exclude technical identifiers and irrelevant promotional content.
- Return "null" for spam or irrelevant emails.
Sources: apps/web/utils/ai/digest/summarize-email-for-digest.ts:29-41
The function expects a structured JSON object as a response from the LLM, defined by the following Zod schema:
const schema = z.object({
content: z.string().describe("The content of the summary text"),
});
Sources: apps/web/utils/ai/digest/summarize-email-for-digest.ts:15-17
Digest Email Template
The visual presentation of the digest is managed by the DigestEmail React component, which uses @react-email/components and Tailwind CSS.
Sources: packages/resend/emails/digest.tsx
Component Props
The DigestEmail component is configured through DigestEmailProps.
| Prop | Type | Description |
|---|---|---|
baseUrl | string | The base URL for links in the email. |
unsubscribeToken | string | Token for the one-click unsubscribe link. |
emailAccountId | string | The user's email account ID for generating settings links. |
ruleNames | Record<string, string> | A map of category keys to their display names (e.g., { coldEmail: "Cold Email" }). |
[categoryKey] | DigestItem[] | NormalizedCategoryData | Dynamic keys representing email categories (e.g., newsletter, receipt) containing summarized items. |
Sources: packages/resend/emails/digest.tsx:68-78
A DigestItem represents a single summarized email:
type DigestItem = {
from: string;
subject: string;
content: string; // The AI-generated summary
};
Sources: packages/resend/emails/digest.tsx:10-14
Structure and Layout
- Header: Contains the Inbox Zero logo, a "Your Digest" heading, and an introductory sentence.
- Category Sections: Emails are grouped into sections based on their category. Each section has a distinct color code for its left border.
- Email Items: Within each section, individual summarized emails are displayed with their original sender, subject, and the AI-generated summary content.
- Empty State: If there are no emails to summarize, a message "No emails to summarize in this digest" is displayed.
- Footer: Includes links to unsubscribe from the digest and to customize email settings.
The component uses a normalizeCategoryData helper to ensure that data for each category is in a consistent NormalizedCategoryData format, which includes count, senders, and items.
Sources: packages/resend/emails/digest.tsx:111-135, 218-301
Generic Summarization API
In addition to the digest-specific summarization, the application provides a generic summarization capability via an API endpoint, likely /api/ai/summarise.
Sources: apps/web/app/api/ai/summarise/controller.ts
summarise Controller
The summarise function handles on-demand text summarization.
- System Prompt: It uses a simpler prompt:
You are an email assistant. You summarise emails. Summarise each email in a short ~5 word sentence. If you need to summarise a longer email, you can use bullet points. Each bullet should be ~5 words. - Streaming: It uses
chatCompletionStreamto provide a streaming response to the client. - Caching: Upon completion, the summary is cached in Redis for 24 hours using the original text as the key.
Sources: apps/web/app/api/ai/summarise/controller.ts:7-29
This generic endpoint is distinct from the digest summarizer as it is designed for immediate, on-demand use and has different formatting requirements and no user-specific context.
Downstream Usage of Summaries
The summaries generated by these systems are also used as inputs for other analytical features. For example, the aiGenerateExecutiveSummary function consumes an array of EmailSummary objects to identify a user's professional persona and provide high-level insights about their email behavior.
This demonstrates that the summarization logic is a foundational component for broader AI-driven email analysis within the application.
Sources: apps/web/utils/ai/report/generate-executive-summary.ts:8-9, 42-50