#!/usr/bin/env node
import { FastMailClient } from './src/fastmail-client.js';
import dotenv from 'dotenv';
dotenv.config({ path: '../.env' });
async function checkSpamFolder() {
console.log('🔍 REVIEWING SPAM FOLDER FOR LEGITIMATE EMAILS\n');
const client = new FastMailClient(
process.env.FASTMAIL_API_TOKEN,
'clark@clarkeverson.com',
'clark@clarkeverson.com',
'clarkeverson.com',
'https://api.fastmail.com/jmap/session'
);
try {
await client.authenticate();
const mailboxes = await client.getMailboxes();
const spamMailbox = mailboxes.find(mb => mb.name === 'Spam');
if (!spamMailbox) {
console.log('❌ Spam folder not found');
return;
}
if (spamMailbox.totalEmails === 0) {
console.log('✅ Spam folder is empty');
return;
}
console.log(`📧 SPAM FOLDER CONTENTS (${spamMailbox.totalEmails} emails):`);
console.log('='.repeat(80));
// Get target folders for moving legitimate emails
const infoParent = mailboxes.find(mb => mb.name === 'Information');
const financialParent = mailboxes.find(mb => mb.name === 'Financial');
const commerceParent = mailboxes.find(mb => mb.name === 'Commerce');
const professionalParent = mailboxes.find(mb => mb.name === 'Professional');
const targets = {
'Information/Newsletters': mailboxes.find(mb => mb.parentId === infoParent?.id && mb.name === 'Newsletters'),
'Financial/Receipts': mailboxes.find(mb => mb.parentId === financialParent?.id && mb.name === 'Receipts'),
'Financial/Banking': mailboxes.find(mb => mb.parentId === financialParent?.id && mb.name === 'Banking'),
'Commerce/Orders': mailboxes.find(mb => mb.parentId === commerceParent?.id && mb.name === 'Orders'),
'Professional/GitHub': mailboxes.find(mb => mb.parentId === professionalParent?.id && mb.name === 'GitHub'),
'Professional/Security': mailboxes.find(mb => mb.parentId === professionalParent?.id && mb.name === 'Security')
};
let position = 0;
let legitimateFound = [];
let spamConfirmed = [];
while (position < spamMailbox.totalEmails) {
const emailResult = await client.getEmails(spamMailbox.id, 50, position);
if (!emailResult?.emails?.length) break;
for (const email of emailResult.emails) {
const sender = email.from?.[0]?.email?.toLowerCase() || '';
const subject = email.subject || '';
const senderDomain = sender.split('@')[1]?.toLowerCase() || '';
// Check for legitimate companies that might have been misclassified
const isLegitimate =
// Major companies
senderDomain.includes('amazon') || senderDomain.includes('apple') ||
senderDomain.includes('google') || senderDomain.includes('microsoft') ||
senderDomain.includes('paypal') || senderDomain.includes('stripe') ||
senderDomain.includes('github') || senderDomain.includes('salesforce') ||
senderDomain.includes('slack') || senderDomain.includes('zoom') ||
senderDomain.includes('adobe') || senderDomain.includes('dropbox') ||
// Financial institutions
senderDomain.includes('chase') || senderDomain.includes('schwab') ||
senderDomain.includes('fidelity') || senderDomain.includes('wellsfargo') ||
senderDomain.includes('bankofamerica') || senderDomain.includes('usbank') ||
// Airlines/Travel
senderDomain.includes('united') || senderDomain.includes('delta') ||
senderDomain.includes('southwest') || senderDomain.includes('american') ||
senderDomain.includes('expedia') || senderDomain.includes('booking') ||
// Major retailers
senderDomain.includes('walmart') || senderDomain.includes('target') ||
senderDomain.includes('costco') || senderDomain.includes('homedepot') ||
senderDomain.includes('bestbuy') || senderDomain.includes('staples') ||
// Professional services
senderDomain.includes('linkedin') || senderDomain.includes('indeed') ||
senderDomain.includes('glassdoor') || senderDomain.includes('upwork') ||
// Health/Medical
senderDomain.includes('mayoclinic') || senderDomain.includes('webmd') ||
senderDomain.includes('healthline') || senderDomain.includes('cvs') ||
senderDomain.includes('walgreens') ||
// Government/Official
senderDomain.includes('.gov') || senderDomain.includes('.edu') ||
// Known legitimate services
senderDomain.includes('mailchimp') || senderDomain.includes('constantcontact') ||
senderDomain.includes('sendgrid') || senderDomain.includes('mandrill') ||
// NOT spam if it's actual receipts/orders
(subject.toLowerCase().includes('receipt') ||
subject.toLowerCase().includes('order') ||
subject.toLowerCase().includes('confirmation') ||
subject.toLowerCase().includes('invoice')) &&
!subject.toLowerCase().includes('tea trick') &&
!subject.toLowerCase().includes('weight loss');
if (isLegitimate) {
legitimateFound.push({
id: email.id,
from: sender,
subject: subject,
domain: senderDomain
});
} else {
spamConfirmed.push({
from: sender,
subject: subject
});
}
}
position += emailResult.emails.length;
}
console.log('\n🟢 LEGITIMATE EMAILS FOUND IN SPAM:');
console.log('='.repeat(60));
if (legitimateFound.length === 0) {
console.log('✅ No legitimate emails found in spam folder');
} else {
let moved = 0;
for (const email of legitimateFound) {
console.log(`📧 FROM: ${email.from}`);
console.log(` SUBJECT: ${email.subject}`);
// Categorize and move legitimate email
let targetPath = null;
if (email.domain.includes('bank') || email.domain.includes('chase') ||
email.domain.includes('schwab') || email.domain.includes('fidelity') ||
email.subject.toLowerCase().includes('statement') ||
email.subject.toLowerCase().includes('balance')) {
targetPath = 'Financial/Banking';
} else if (email.subject.toLowerCase().includes('receipt') ||
email.subject.toLowerCase().includes('payment') ||
email.subject.toLowerCase().includes('invoice')) {
targetPath = 'Financial/Receipts';
} else if (email.domain.includes('amazon') || email.domain.includes('walmart') ||
email.subject.toLowerCase().includes('order') ||
email.subject.toLowerCase().includes('shipped')) {
targetPath = 'Commerce/Orders';
} else if (email.domain.includes('github') || email.domain.includes('google') ||
email.domain.includes('microsoft') || email.domain.includes('adobe')) {
targetPath = 'Professional/GitHub';
} else if (email.subject.toLowerCase().includes('security') ||
email.subject.toLowerCase().includes('login') ||
email.subject.toLowerCase().includes('password')) {
targetPath = 'Professional/Security';
} else {
targetPath = 'Information/Newsletters';
}
if (targetPath && targets[targetPath]) {
try {
await client.moveEmailsToMailbox([email.id], targets[targetPath].id);
console.log(` ✅ MOVED TO: ${targetPath}`);
moved++;
} catch (error) {
console.log(` ❌ Error moving: ${error.message}`);
}
}
console.log('');
}
console.log(`📊 SUMMARY: Moved ${moved} legitimate emails out of spam`);
}
console.log('\n🔴 CONFIRMED SPAM EMAILS (sample):');
console.log('='.repeat(60));
spamConfirmed.slice(0, 10).forEach((email, index) => {
console.log(`${index + 1}. ${email.subject} (${email.from})`);
});
if (spamConfirmed.length > 10) {
console.log(`... and ${spamConfirmed.length - 10} more spam emails`);
}
console.log(`\n📊 SPAM FOLDER REVIEW COMPLETE:`);
console.log(`✅ Legitimate emails rescued: ${legitimateFound.length}`);
console.log(`🗑️ Confirmed spam remaining: ${spamConfirmed.length}`);
} catch (error) {
console.log('❌ Error:', error.message);
}
}
checkSpamFolder().catch(console.error);