diff --git a/DEPRECATED_FUNCTIONS.md b/DEPRECATED_FUNCTIONS.md
index f389c850..cf4282e8 100644
--- a/DEPRECATED_FUNCTIONS.md
+++ b/DEPRECATED_FUNCTIONS.md
@@ -1,6 +1,46 @@
-# Deprecated Functions and Code
+# Deprecated Functions and Files
-This document tracks functions that have been marked as deprecated and should be removed in future releases.
+This document lists functions and files that have been deprecated and should not be used in new code.
+
+## Deprecated Files
+
+### 1. `lib/email-formatter.ts` (REMOVED)
+- **Status**: Removed
+- **Replacement**: Use `lib/utils/email-formatter.ts` instead
+- **Reason**: Consolidated email formatting to a single source of truth
+
+### 2. `cleanHtml` in `lib/mail-parser-wrapper.ts`
+- **Status**: Deprecated for email composition
+- **Replacement**: Use `cleanHtmlContent` from `lib/utils/email-formatter.ts` instead
+- **Reason**: Better handling of text direction and HTML sanitization
+
+## Deprecated Functions
+
+### 1. `formatEmailForReplyOrForward` in `lib/services/email-service.ts` (REMOVED)
+- **Status**: Removed
+- **Replacement**: Use `formatEmailForReplyOrForward` from `lib/utils/email-formatter.ts`
+- **Reason**: Consolidated email formatting to a single source of truth
+
+### 2. `formatSubject` in `lib/services/email-service.ts` (REMOVED)
+- **Status**: Removed
+- **Replacement**: None specific, handled by centralized formatter
+- **Reason**: Internal function of the email formatter
+
+### 3. `createQuoteHeader` in `lib/services/email-service.ts` (REMOVED)
+- **Status**: Removed
+- **Replacement**: None specific, handled by centralized formatter
+- **Reason**: Internal function of the email formatter
+
+## Centralized Email Formatting
+
+All email formatting is now handled by the centralized formatter in `lib/utils/email-formatter.ts`. This file contains:
+
+1. `formatForwardedEmail`: Format emails for forwarding
+2. `formatReplyEmail`: Format emails for replying or replying to all
+3. `formatEmailForReplyOrForward`: Compatibility function that maps to the above two
+4. `cleanHtmlContent`: Safely sanitize HTML content while preserving direction attributes
+
+Use these functions for all email formatting needs.
## Email Parsing and Processing Functions
diff --git a/README.md b/README.md
index eab1f3ec..1d42fcac 100644
--- a/README.md
+++ b/README.md
@@ -56,4 +56,23 @@ npm run dev
# Build for production
npm run build
-```
\ No newline at end of file
+```
+
+# Email Formatting
+
+## Centralized Email Formatter
+
+All email formatting is now handled by a centralized formatter in `lib/utils/email-formatter.ts`. This ensures consistent handling of:
+
+- Text direction (RTL/LTR)
+- HTML sanitization
+- Content formatting for forwards and replies
+
+### Key Functions
+
+- `formatForwardedEmail`: Format emails for forwarding
+- `formatReplyEmail`: Format emails for replying
+- `cleanHtmlContent`: Sanitize HTML while preserving direction attributes
+- `formatEmailForReplyOrForward`: Compatibility function for both
+
+This centralized approach prevents formatting inconsistencies and direction problems when dealing with emails in different languages.
\ No newline at end of file
diff --git a/lib/actions/email-actions.ts b/lib/actions/email-actions.ts
index 844b610f..2a8fb922 100644
--- a/lib/actions/email-actions.ts
+++ b/lib/actions/email-actions.ts
@@ -1,6 +1,7 @@
'use server';
-import { getEmails, formatEmailForReplyOrForward, EmailMessage, EmailAddress } from '@/lib/services/email-service';
+import { getEmails, EmailMessage, EmailAddress } from '@/lib/services/email-service';
+import { formatEmailForReplyOrForward } from '@/lib/utils/email-formatter';
/**
* Server action to fetch emails
@@ -20,6 +21,7 @@ export async function fetchEmails(userId: string, folder = 'INBOX', page = 1, pe
/**
* Server action to format email for reply or forward operations
+ * Uses the centralized email formatter
*/
export async function formatEmailServerSide(
email: {
@@ -71,7 +73,7 @@ export async function formatEmailServerSide(
contentFetched: true
};
- // Use the server-side formatter
+ // Use the centralized formatter
const formatted = formatEmailForReplyOrForward(serverEmail, type);
return {
diff --git a/lib/email-formatter.ts b/lib/email-formatter.ts
deleted file mode 100644
index 57ca9071..00000000
--- a/lib/email-formatter.ts
+++ /dev/null
@@ -1,225 +0,0 @@
-'use client';
-
-import DOMPurify from 'dompurify';
-
-/**
- * Client-side utilities for formatting email content
- * This file contains functions for formatting email content in the browser
- * without any server dependencies.
- */
-
-export interface EmailAddress {
- address: string;
- name?: string;
-}
-
-export interface FormattedEmail {
- subject: string;
- to?: EmailAddress[];
- cc?: EmailAddress[];
- bcc?: EmailAddress[];
- body: string;
-}
-
-export interface EmailMessageForFormatting {
- subject?: string;
- from?: EmailAddress | EmailAddress[];
- to?: EmailAddress | EmailAddress[];
- date?: Date | string;
- html?: string;
- text?: string;
- cc?: EmailAddress | EmailAddress[];
- bcc?: EmailAddress | EmailAddress[];
-}
-
-/**
- * Format an email for replying or forwarding
- * Client-side friendly version that doesn't depend on server modules
- */
-export function formatEmailForReply(
- originalEmail: EmailMessageForFormatting,
- type: 'reply' | 'replyAll' | 'forward' = 'reply'
-): FormattedEmail {
- // Format the subject with Re: or Fwd: prefix
- const subject = formatSubject(originalEmail.subject || '', type);
-
- // Initialize recipients based on reply type
- let to: EmailAddress[] = [];
- let cc: EmailAddress[] = [];
-
- if (type === 'reply' && originalEmail.from) {
- to = Array.isArray(originalEmail.from) ? originalEmail.from : [originalEmail.from];
- } else if (type === 'replyAll') {
- // To: original sender
- if (originalEmail.from) {
- to = Array.isArray(originalEmail.from) ? originalEmail.from : [originalEmail.from];
- }
-
- // CC: all other recipients
- if (originalEmail.to) {
- cc = Array.isArray(originalEmail.to) ? originalEmail.to : [originalEmail.to];
- }
-
- if (originalEmail.cc) {
- const existingCc = Array.isArray(originalEmail.cc) ? originalEmail.cc : [originalEmail.cc];
- cc = [...cc, ...existingCc];
- }
-
- // Remove duplicates and self from CC (would need user's email here)
- // This is simplified - in a real app you'd filter out the current user
- cc = cc.filter((value, index, self) =>
- index === self.findIndex((t) => t.address === value.address)
- );
- }
-
- // Create the quoted content with header
- const quoteHeader = createQuoteHeader(originalEmail);
-
- // Get the original content, preferring HTML over plain text
- let originalContent = '';
- if (originalEmail.html) {
- // Sanitize any potentially unsafe HTML
- originalContent = DOMPurify.sanitize(originalEmail.html);
- } else if (originalEmail.text) {
- // Convert text to HTML by replacing newlines with br tags
- originalContent = originalEmail.text.replace(/\n/g, '
');
- }
-
- // Combine the header with the original content, ensuring proper direction
- const body = `
-
- ${quoteHeader} --${originalContent || 'No content available'}-