Fixed two critical email threading issues - sent emails not appearing in thread listings and external replies creating new threads
Fixed two critical email threading issues that broke conversation continuity:
processSentEmailForThreading
to use the correct field referenceRoot 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.
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
Original Issues:
lib/email-management/email-threader.ts
- Fixed database field mismatch in processSentEmailForThreading
app/api/v2/emails/[id]/reply/route.ts
- Fixed References header constructionNo action required - the fix is automatically applied to all new replies. Existing threads are unaffected and will continue to work normally.