Back to changelog
v2.1.3

Fix Email Threading - Complete Threading System Repair

Fixed two critical email threading issues - sent emails not appearing in thread listings and external replies creating new threads

Overview

Fixed two critical email threading issues that broke conversation continuity:

  1. Thread Listing Issue: Sent emails (replies) weren't appearing when retrieving thread messages
  2. External Reply Threading Issue: External replies to sent emails created new threads instead of continuing existing conversations

What Changed

  • Fixed Database Field Mismatch: Corrected processSentEmailForThreading to use the correct field reference
  • Enhanced References Chain Building: Implemented proper RFC 5322 compliant References header construction
  • Fixed Thread Continuity: External replies now correctly attach to existing threads
  • Improved Threading Headers: Both database storage and SES email headers now use complete Message-ID chains

Technical Details

Issue 1: Thread Listing Problem

Root Cause: Database field mismatch in processSentEmailForThreading

// ❌ BROKEN - Wrong field reference
eq(structuredEmails.id, originalEmailId)

// ✅ FIXED - Correct field reference  
eq(structuredEmails.emailId, originalEmailId)

The function was looking up emails by structuredEmails.id but resolveEmailId returns structuredEmails.emailId, causing sent emails to not be properly linked to threads.

Issue 2: References Header Problem

Root Cause: Incomplete References header construction

// ❌ BROKEN - Only immediate parent
References: threadingId  

// ✅ FIXED - Full conversation chain
// Parse existing references from original email
let references = [];
if (original.references) {
  references = JSON.parse(original.references)
    .map(ref => formatMessageId(ref));
}

// Add original Message-ID to chain
if (original.messageId && !references.includes(formattedId)) {
  references.push(formattedId);
}

References: references.join(' ')  // Complete chain

Flow Impact

Original Issues:

  1. Email received → New thread created ✅
  2. Send reply using thread ID → Reply added to thread ✅
  3. Reply doesn't show in thread list ❌NOW SHOWS IN THREAD LIST ✅
  4. External reply to sent email creates new thread ❌NOW ATTACHES TO EXISTING THREAD ✅

Files Modified

  • lib/email-management/email-threader.ts - Fixed database field mismatch in processSentEmailForThreading
  • app/api/v2/emails/[id]/reply/route.ts - Fixed References header construction

Impact

  • Backward Compatible: No breaking changes to API
  • Improved User Experience: Conversations now stay properly threaded
  • RFC Compliance: Email headers now follow proper standards
  • External Compatibility: Works correctly with all major email clients

Migration Guide

No action required - the fix is automatically applied to all new replies. Existing threads are unaffected and will continue to work normally.