get_top_senders
Analyze email patterns by identifying the most frequent senders in your iCloud inbox using configurable sampling and result limits.
Instructions
Get the top senders by email count from a sample of the inbox
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| mailbox | No | Mailbox to analyze (default INBOX) | |
| sampleSize | No | Number of emails to sample (default 500) | |
| maxResults | No | Max number of senders/domains to return (default 20) |
Implementation Reference
- lib/imap.js:775-802 (handler)The implementation of the get_top_senders tool, which fetches recent emails from a mailbox, aggregates senders and domains by frequency, and returns the top results.
export async function getTopSenders(mailbox = 'INBOX', sampleSize = 500, maxResults = 20, creds = null) { const client = createRateLimitedClient(creds); await client.connect(); const mb = await client.mailboxOpen(mailbox); const total = mb.exists; const senderCounts = {}; const senderDomains = {}; const end = total; const start = Math.max(1, total - sampleSize + 1); const range = `${start}:${end}`; let count = 0; for await (const msg of client.fetch(range, { envelope: true })) { const address = msg.envelope.from?.[0]?.address; if (address) { senderCounts[address] = (senderCounts[address] || 0) + 1; const domain = address.split('@')[1]; if (domain) senderDomains[domain] = (senderDomains[domain] || 0) + 1; } count++; } await client.logout(); const topAddresses = Object.entries(senderCounts).sort((a, b) => b[1] - a[1]).slice(0, maxResults).map(([address, count]) => ({ address, count })); const topDomains = Object.entries(senderDomains).sort((a, b) => b[1] - a[1]).slice(0, maxResults).map(([domain, count]) => ({ domain, count })); return { sampledEmails: count, topAddresses, topDomains }; }