#!/usr/bin/env node
import { FastMailClient } from '../src/fastmail-client.js';
import dotenv from 'dotenv';
dotenv.config({ path: '../.env' });
async function organizeTodaysInbox() {
console.log('🔥 ORGANIZING TODAY\'S INBOX 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 inboxMailbox = mailboxes.find(mb => mb.name === 'Inbox');
if (!inboxMailbox) {
console.log('❌ Inbox not found');
return;
}
// Get target folders
const infoParent = mailboxes.find(mb => mb.name === 'Information');
const financialParent = mailboxes.find(mb => mb.name === 'Financial');
const targets = {
'Information/Newsletters': mailboxes.find(mb => mb.parentId === infoParent?.id && mb.name === 'Newsletters'),
'Information/News': mailboxes.find(mb => mb.parentId === infoParent?.id && mb.name === 'News'),
'Financial/Banking': mailboxes.find(mb => mb.parentId === financialParent?.id && mb.name === 'Banking')
};
// Get today's emails from inbox
const today = new Date();
today.setHours(0, 0, 0, 0);
const emailResult = await client.getEmails(inboxMailbox.id, 20, 0);
if (!emailResult?.emails?.length) {
console.log('✅ Inbox is empty!');
return;
}
const todaysEmails = emailResult.emails.filter(email => {
if (!email.receivedAt) return false;
const emailDate = new Date(email.receivedAt);
emailDate.setHours(0, 0, 0, 0);
return emailDate.getTime() === today.getTime();
});
if (todaysEmails.length === 0) {
console.log('✅ No emails received today in inbox!');
return;
}
console.log(`📧 Processing ${todaysEmails.length} emails from today:`);
console.log('='.repeat(80));
let moved = 0;
let kept = 0;
for (const email of todaysEmails) {
const sender = email.from?.[0]?.email?.toLowerCase() || '';
const subject = email.subject || '';
const senderDomain = sender.split('@')[1]?.toLowerCase() || '';
console.log(`\n📧 "${subject.substring(0, 50)}..." from ${sender}`);
let targetPath = null;
let reason = '';
// WSJ Newsletter
if (sender.includes('@wsj.com') || sender.includes('@interactive.wsj.com')) {
targetPath = 'Information/News';
reason = 'WSJ News';
}
// Sunday lawn care promotional
else if (sender.includes('@getsunday.com')) {
targetPath = 'Information/Newsletters';
reason = 'Lawn care promotional';
}
// HSN shopping deals
else if (sender.includes('@emailhsn.com')) {
targetPath = 'Information/Newsletters';
reason = 'Shopping deals';
}
// Self-care newsletter
else if (sender.includes('@readnotetoself.com')) {
targetPath = 'Information/Newsletters';
reason = 'Self-care newsletter';
}
// Mortgage/banking
else if (sender.includes('mortgage') || sender.includes('loanadministration') ||
subject.toLowerCase().includes('mortgage') ||
subject.toLowerCase().includes('document center')) {
targetPath = 'Financial/Banking';
reason = 'Mortgage/banking communication';
}
// Personal delivery notification (might be actionable)
else if (sender.includes('@protonmail.com') &&
subject.toLowerCase().includes('delivery')) {
reason = 'Personal delivery notification - keeping in inbox';
kept++;
}
// Default case
else {
reason = 'Keeping for manual review';
kept++;
}
if (targetPath && targets[targetPath]) {
try {
await client.moveEmailsToMailbox([email.id], targets[targetPath].id);
console.log(` ✅ MOVED TO: ${targetPath} (${reason})`);
moved++;
} catch (error) {
console.log(` ❌ Error moving: ${error.message}`);
}
} else {
console.log(` 📥 KEPT IN INBOX (${reason})`);
}
}
console.log('\n🎉 TODAY\'S INBOX ORGANIZATION COMPLETE!');
console.log('='.repeat(60));
console.log(`📧 Emails moved to categories: ${moved}`);
console.log(`📥 Emails kept in inbox: ${kept}`);
console.log(`📊 Total processed: ${moved + kept}`);
if (kept <= 2) {
console.log('\n✅ EXCELLENT! Inbox contains minimal actionable emails');
} else {
console.log('\n📧 Review remaining inbox emails for further action');
}
} catch (error) {
console.log('❌ Error:', error.message);
}
}
organizeTodaysInbox().catch(console.error);